Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Defined and Exists, but what about neither?

by c (Hermit)
on Aug 21, 2002 at 13:27 UTC ( [id://191725]=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Defined and Exists, but what about neither?
by Abigail-II (Bishop) on Aug 21, 2002 at 13:42 UTC
    No, that is not possible. If there is a key present there is also a value. The value could be undefined though.
    exists $hash {key} && print "Key is present, value may be undefined." +; defined $hash {key} && print "Key is present, and its value is defined +."; $hash {key} && print "Key is present, its value is defined and + true.";
    Of course, this is also documented in the manual: perldoc -f exists.

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://191725]
Approved by rattusillegitimus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-29 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found