I have a method of an ssh object that I have created and this method adds an account to a remote unix box via an ssh connection. Now, what I basically want is to deal with errors in the best way possible here, so I am asking for some assistance from my fellow monks on the matter.

I'm sorry if my wording here is not that great, but it is somewhat difficult for me to figure out how to word this question. So, I am just going to give some code, and basically an explanation of what I am trying to do...

This is the first method which I don't really like. Basically I just return success or failed with an error message into a scalar. I then interpret the scalar in my main script...
#!/usr/bin/perl my $add_user=$ssh->add_account(login=>"bob",uid=123); # I want output printed (fail or success). print "$add_user\n"; die if ($add_user !~ /success/); sub add_account { ..... if (condition) { return "success: user added"; } elsif (condition { return "failed: uid not unique"; } else { return "failed: unknown error"; }


This is a better version, but I still know that I am not handling success/failures correctly. I know there is a better way to do this, but just not sure how nor where to look to find out.
#!/usr/bin/perl $add_user=$ssh->add_account(login=>"bob",uid=123); $ssh->print_error and die if (!$add_user); print "$add_user\n"; sub add_account { ..... if (condition) { return "success: user added"; } elsif (condition { $ssh->{error} = "failed: uid not unique"; } else { $ssh->{error} = "failed: unknown error"; } return; }
Basically, I want to say that if ($ssh->error_exists) { print "$error\n" and die; } else { print "$add_user\n" }, or if $add_user is not defined then print error and die, else print $add_user and move on. I am having a difficult time figuring out how to deal with errors... and sometimes I don't want them to stop my code, so just print but do not die. For example, if I am adding 100 users I do not want a failure on user #2 to stop my code, so let's not croak.

Anyway, hope this makes sense... any help is greatly appreciated! Thank you in advance monks!

In reply to Dealing with errors in subroutines by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.