Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to tell if a variable is blessed ?

by jeanluca (Deacon)
on Mar 02, 2006 at 14:35 UTC ( [id://533892]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

How can I tell if a variable is an instance of a module (blessed variable)?

Thanks a lot
Luca

Replies are listed 'Best First'.
Re: How to tell if a variable is blessed ?
by monkfan (Curate) on Mar 02, 2006 at 14:42 UTC
    To tell you if the contents of the variable are a reference, and if so, the nature of that reference, use the function ref.
    ref $var;
    The other alternative is this:
    eval { $ref->isa("Some::Package") };

    Regards,
    Edward
      How about a combination of the two?
      if ( ref($var) && $var->isa("Some::Package") ) { $var->someMethod(); }
      Or better yet:
      my $sub; if ( ref($var) && ($sub = $var->can("someMethod")) ) { $sub->(); } else { die "Method someMethod not found." }
      Of course you don't have to capture the return of "can" but it can be convenient.
      Better is
      eval { local $SIG{__DIE__}; $ref->isa('Some::Package'); }
      You might trigger a die handler, which could be bad. For example, one of Test::More's dependencies has one. See eval-blocks and Test::Builder for more on the topic.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: How to tell if a variable is blessed ?
by Fletch (Bishop) on Mar 02, 2006 at 14:40 UTC

    See ref. And variables aren't blessed, referenced values (SV*, AV*, et al) are blessed (not the variable containing the reference or the reference itself; it's the thing that is blessed or not, not the pointer to the thing).

      A better answer may be Scalar::Util and blessed.

      $scalar = "foo"; $class = blessed $scalar; # undef $ref = []; $class = blessed $ref; # undef $obj = bless [], "Foo"; $class = blessed $obj; # "Foo"

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        What does this buy you that a simple ref won't?
        if ( ref($obj) eq "Foo" ) { # We have a Foo object }
        Simple, sweet, and gets the job done. Why over engineer? Of course, if you were already using Scalar::Util for some of its other uses, then I'd say great, but to use a module for something as simple as this, is overcomplicating matters.
      I tried to do it with ref, but than the only solution I can think of is:
      if ( ref($a) && ref($a) !~ /HASH|ARRAY/ ) { $a->blabla(...) ; }
      According to the answers to this post I assume this is the answer ?!

      Thanks
      Luca
Re: How to tell if a variable is blessed ?
by davidrw (Prior) on Mar 02, 2006 at 14:44 UTC
    Also, for debugging purposes, you can just Dumper it w/Data::Dumper .. that will show you the package name ..
Re: How to tell if a variable is blessed ?
by jeanluca (Deacon) on Mar 02, 2006 at 16:18 UTC
    Thanks a lot. I was not really clear about all the details, but I don't know what instance I can expect. So, things like
    $var->isa("Some::Package")
    don't work (in this particular case).
    In this case Scalar::Util::blessed seems to me like a good perl solution :)

    Luca
Re: How to tell if a variable is blessed ?
by PodMaster (Abbot) on Mar 03, 2006 at 04:35 UTC
    Always search first, you'll save yourself time.

    Example blessed => a direct hit, blessed

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: How to tell if a variable is blessed ?
by wfsp (Abbot) on Mar 02, 2006 at 14:41 UTC

    Have a look at ref

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found