Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
TL;DR:
  • print to print to STDOUT and therefore the browser
  • die to print to STDERR and therefore the error log
By using die you're still saying "I want this in the error log, not sent to the browser".

You want to say this:

if (my $output = `mkdir -p /xyz/abc/test 2>&1`) { chomp $output; my $message = "Unable to create directory for (whatever the purpose +was) /xyz/abc/test: $output ($!)"; print $message,"\n"; die $message; exit; }
This runs your mkdir in a way that captures the output from mkdir's STDERR into $output (if the mkdir works, it doesn't print anything). If there was output from the invoked command, then we chomp the output to remove the trailing newline, and print it to STDOUT - for a CGI program, you must print stuff to STDOUT if you want to see it in the browser.

Note that we printed a full report on the error, following the old journalistic standard of who, what, when, where, and why:

  1. A descriptive message: why this matters and approximately when it happened during execution
  2. The name of the directory: where we were trying to do the operation
  3. The error message from mkdir and the error code: what the error was
"who" is the CGI program; if you just have one script, you probably don't need the who; if you have multiple scripts in use by your server, you could print the name of your program (in $0; note that mkdir does this to tell you who failed when it can't create the directory).

Note that I built the message, printed it with a newline, and then did a die with the message sans newline. This means that the message logged in the error log will have the line number and script name supplied automatically and the one shown in the browser won't. Why? Because the final user of the program does not care about scripts and line numbers, only results. The log gets the script name and line number because only developers read logs, not users, and the developers need to know this stuff, unlike the users.


In reply to Re^3: trapping system error by pemungkah
in thread trapping system error by anu_1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-24 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found