Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Control Structures

by ghenry (Vicar)
on Apr 29, 2005 at 10:48 UTC ( #452568=perlquestion: print w/ replies, xml ) Need Help??
ghenry has asked for the wisdom of the Perl Monks concerning the following question:

Dear Master Monks,

In Permissions for a cgi script, I have a simple if/else structure. I have a line where I am burning a CD, and I use die to produce a error if something bad happens with that command.

I was wondering if I could jump to the else structure straight after die, as this is where all the error logs and e-mails are handled?

I thought of next and last, but they are for loops. Any ideas?

Thanks,

Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Comment on Control Structures
Select or Download Code
Re: Control Structures
by gellyfish (Monsignor) on Apr 29, 2005 at 11:06 UTC

    I would suggest changing the structure of the program slightly - factor out the code from the else condition and then rather than die you can use an additional if ... else in the first condition. If the condition is true continue to execute the existing code else go the error handling subroutine.

    /J\

      I am testing if the iso exists and is small enough, I don't understand what the next condition to test against would be?

      Maybe something like:

      system (burning command); if (my $cdburn = $?;) { carry on with code } else { errors }

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
Re: Control Structures
by zentara (Archbishop) on Apr 29, 2005 at 11:13 UTC
    I like using a goto in those situations. :-) But others will surely beat me down for that. :-)

    Or how about a DIE signal handler?

    #!/usr/bin/perl $SIG{__DIE__} = sub { open my $h, ">>", "fatal.log"; print $h scalar localtime, ": @_"; close $h; die @_; }; die "ouch $!\n";

    I'm not really a human, but I play one on earth. flash japh

      I think I'll take the simple approach and restructure my program like gellyfish recommends, but thanks though.

      The above looks a little tricky, as I am struggling with a simple if/else ;-)

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
      I've dealt with code that uses goto for error handling.

      Suffice it to say that there is a reason that exception systems were invented...

Re: Control Structures
by tcf03 (Deacon) on Apr 29, 2005 at 12:04 UTC
    I use something similar to the following in most of my cgi scripts when making system calls.
    use CGI::Carp qw(fatalsToBrowser); # rest of script my $CMD="some command"; system("$CMD"); ( $? == 0 ) ? print "job success\n" : print "job failure\n";
    Ted
    --
    "Men have become the tools of their tools."
      --Henry David Thoreau

      I love the look of this, because I really wanted to make the code look cleaner and I have been looking for a chance to use the Ternary Conditional Operator.

      Could I swap out the print statements for subroutine calls and move the success and failure things to something like &failure and &success?

      Something like:

      my $CMD="some command"; system("$CMD"); ( $? == 0 ) ? &success($time, $hostname) : &failure($time, $hostname) +;

      and then shift these scalars inside the subroutines?

      Thanks.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!

        I'll answer my own question ;-)

        I tried it and it worked!

        For your info:

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://452568]
Approved by polettix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-09-18 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (113 votes), past polls