Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Question regarding handling exceptions in subroutine

by choroba (Abbot)
on May 12, 2013 at 10:05 UTC ( #1033180=note: print w/ replies, xml ) Need Help??


in reply to Question regarding handling exceptions in subroutine

Please, be more specific. You want to return an error or success, but still continue the loop. That is not possible - once you return, the loop is done. Probably, you can return two array references: the first array would contain successful users, the second one the failed users.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Question regarding handling exceptions in subroutine
Re^2: Question regarding handling exceptions in subroutine
by walkingthecow (Friar) on May 12, 2013 at 10:12 UTC
    The loop is in script.pl and calls the subroutine from Script.pm. When I croak in Script.pm it makes script.pl exit, thus breaking the loop in script.pl. I'd rather have the subroutine in Script.pm return the error to a variable in script.pl. Hopefully that clears things up. I'm terrible at explaining things.

      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! :-)

        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»

        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»

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-11-27 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (187 votes), past polls