I have a subroutine that a hash reference passed to it...

&log_action(\%args);

In my mains code, I test for an error and then stuff the value into $args{err} (This is being done through a Net::SNMP function)

$args{err} = $s->error();

Now, within the log_action subroutine, I pull in my hash reference

sub log_action { my $args = shift;

And I test to see if there is was an error message

if ($args->{err}) { # do something...

Is this a suitable method? I read through defined and exists and it seems as though

if (defined $args->{err}) {

may be more appropriate. How does perl judge if (args->{err}) when neither is used?

And as a follow up question regarding whether defined or exists is the better to use... If there is no error returned via the Net::SNMP call, according to its documentation, it "returns an empty string". Please correct me if I am wrong, but this means that there will be a hash key present called $args{err} but there will be no value associated with it?

Thanks -c


In reply to Defined and Exists, but what about neither? by c

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.