|Welcome to the Monastery|
Question regarding handling exceptions in subroutineby walkingthecow (Friar)
|on May 12, 2013 at 08:29 UTC||Need Help??|
walkingthecow has asked for the
wisdom of the Perl Monks concerning the following question:
My script runs a command on a Linux system. For example:
Now, in my subroutine I want to run useradd $login and grab the output from the system. If the output is an error (e.g., user already exists), I want to return something like, "ERROR: $login already exists\n", or "ERROR: $login is not a valid UNIX username\n"... If it worked, it should return something like, "SUCCESS: $login added to system" Currently I am croaking if it's not a success, but that destroys my while loop.
Basically, what's the best way to return an error message from a subroutine, and a success message if it succeeds?I've read the following:
While interesting, I know Perl has changed a lot in the past few years, so trying to get an updated opinion on the matter. Thanks!