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.