Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Definedness of an Object

by PerlingTheUK (Hermit)
on Mar 14, 2006 at 13:57 UTC ( [id://536577]=perlquestion: print w/replies, xml ) Need Help??

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

All,

I have a matcher that looks through a number of objects and returns the first that matches certain criteria or - if none are found - it returns undef.

Now all I need to know is if the resulting variable is defined or not. As I expect the object to be defined, I croak if no object was found.

So far the theory. Here is the code:

my $ls = Obj::LocationSource->new(); my $dep_tip = $ls->match( { location => $dep_name } ); print Dumper( $dep_tip ); if ( ! defined $dep_tip ){ croak ( "DATA ERROR: Could not find any location for '$dep_name'" ); } OUTPUT: $VAR1 = bless( { '_location' => 'Salisbury', '_id' => 259, '_key' => 'SLSBRY' }, 'Object::Location' ); DATA ERROR: Could not find any Location for 'Salisbury' at ***.pl line + 45

I do not understand why ! defined $dep_tip seems to evaluate to true.


Cheers,
PerlingTheUK

Considered by PerlingTheUK: Please delte as I found the error in a later line in the code.
Unconsidered by planetscape: keep (and edit) votes prevented reaping

Replies are listed 'Best First'.
Re: Definedness of an Object
by diotalevi (Canon) on Mar 14, 2006 at 16:29 UTC

    Devel::DProf breaks defined() in one case I'm aware of (it causes overload's stringification to call when it shouldn't). Are you using that?

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Definedness of an Object
by Roy Johnson (Monsignor) on Mar 14, 2006 at 15:43 UTC
    I don't know why you get that result. It doesn't happen when I try this bit that should be equivalent:
    use strict; use warnings; my $dep_name = 'Salisbury'; my $dep_tip = bless( { '_location' => $dep_name, '_id' => 259, '_key' => 'SLSBRY' }, 'Object::Location' ); if ( ! defined $dep_tip ){ use Carp; croak ( "DATA ERROR: Could not find any location for '$dep_name'" ); }

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-24 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found