Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Defined and Exists, but what about neither?

by c (Hermit)
on Aug 21, 2002 at 13:27 UTC ( #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...


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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-03-22 11:59 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.