Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: trapping system error

by Khen1950fx (Canon)
on Aug 28, 2010 at 11:11 UTC ( [id://857833]=note: print w/replies, xml ) Need Help??


in reply to trapping system error

I used CGI::Carp and CGI::Debug. You can customize all your error messages and go from there. The BEGIN block redirects the error message to the browser and prints out the message with a little extra from CGI::Debug.
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Debug; use CGI::Carp qw( fatalsToBrowser ); BEGIN { sub carp_error { my $error_message = shift; my $q = new CGI; $q->start_html( "Not able to create directory" ), $q->h1( "Error" ), $q->p( "Not able to create directory" ), $q->p( $q->i( $error_message ) ), $q->end_html; } CGI::Carp::set_message( \&carp_error ); } system("mkdir -p test"); if ($@) { print "Not able to create directory\n"; }

Replies are listed 'Best First'.
Re^2: trapping system error
by anu_1 (Acolyte) on Aug 28, 2010 at 11:38 UTC
    Thanks for your help..
Re^2: trapping system error
by thargas (Deacon) on Aug 31, 2010 at 13:19 UTC
    The answer is almost always: don't use system. If your question is using mkdir as an example, then the answer is use backticks like:
    my $output = `mkdir -p /what/ever 2>&1`;

    If the command to be run will produce output on stdout as well as errors on stderr, then you'll be better off using one of the modules which will allow you to run a command and receive stderr and stdout separately, like IPC::Run, ...

    If you really meant mkdir specifically, why not use perl's mkdir, or File::Path's mkpath? You won't have to do any error-trapping; you can just ask perl what happened.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://857833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found