Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Question regarding handling exceptions in subroutine

by space_monk (Chaplain)
on May 12, 2013 at 11:28 UTC ( #1033186=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Question regarding handling exceptions in subroutine
in thread Question regarding handling exceptions in subroutine

The obvious answer is to remove the croak (or perhaps change it to a carp/warn).

You can run a command in backticks to trap both the output and the status code it returns. So you should be able to do something like:

my $response=`useradd fred ....`; #check the status code (0 = success) if ($? >>8) { # we could not add the user }
If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)


Comment on Re^3: Question regarding handling exceptions in subroutine
Download Code
Re^4: Question regarding handling exceptions in subroutine
by karlgoethebier (Curate) on May 12, 2013 at 12:23 UTC

    Shouldn't it be:

    my $result = qx($command); if ( $? >> 8 != 0 ) { if ($@) { die $@; } else { die qq(Something really weird happened!); } }

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re^4: Question regarding handling exceptions in subroutine
by karlgoethebier (Curate) on May 12, 2013 at 12:24 UTC

    Shouldn't it be something like this?

    Update:

    OK, eval...

    my $result; eval { $result = qx($command); }; if ( $? >> 8 != 0 ) { if ($@) { die $@; } else { die qq(Something really weird happened!); } } # do stuff with $result...

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Thanks, you're right about the other issues, I was simply in a hurry to outline a way forwardat least that's the excuse I'm using :-)

      Question: Why 'eval' this instead of just running this in backticks? I understand the command can fail, but in those cases it simply returns an error code.

      If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
        "Why 'eval' this..."

        Good question. Because:

        1. I'm paranoid
        2. I didn't think
        3. I was in a hurry

        Perhaps all items match ;-)

        My posts aren't correct: it would only make (some) sense to eval if there is a die in the block that sets $@ (perhaps something wrong with $command, like a non-existing file etc). But this could be also trapped before with a file test operator...a.s.o

        I just wanted to say that you forgot to right shift, as you already noticed. That would have been enough. Sorry :-(

        Update:

        To tell the truth: from time to time i need to take a look again at Perl documentation about this issues. I think it can be tricky - depending on the situation. And even if it isn't really tricky, i get a little confused about it from time to time...

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-07-30 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls