How to tell if a variable is blessed ?
jeanluca
created: 2006-03-02 09:35:54
Dear Monks

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

Thanks a lot
Luca
Re: How to tell if a variable is blessed ?
created: 2006-03-02 09:40:47

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).

Re^2: How to tell if a variable is blessed ?
created: 2006-03-02 09:51:54
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^2: How to tell if a variable is blessed ?
xdg
created: 2006-03-02 09:57:02

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 [http://creativecommons.org/licenses/publicdomain|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.

Re^3: How to tell if a variable is blessed ?
created: 2006-03-02 10:13:33
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.
Re^4: How to tell if a variable is blessed ?
created: 2006-03-02 10:17:16

Because if you're merely interested in finding out if the reference is to an instance of some class (as opposed to a hashref, arrayref, ...), what do you put on the other side of the eq? Not to mention the problems with that if you want to gracefully handle subclasses (in which case you really want UNIVERSAL::isa()).

And ++ to xdg; I'd forgotten all about Scalar::Util::blessed. Much better answer than mine.

Re^5: How to tell if a variable is blessed ?
xdg
created: 2006-03-02 10:34:09

Thanks!

If you want to see some interesting complexity on what should be such a simple topic, see Why reftype and blessed are making my life harder than it needs to be.

-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.

Re^4: How to tell if a variable is blessed ?
created: 2006-03-02 14:14:31

Because the question was how to tell if the reference was blessed, not how to tell if it was blessed into a specific package, ref won't tell you that unless you do a lot of && ref($obj) !~ /HASH/ && ref($obj) !~ /ARRAY/...


We're not surrounded, we're in a target-rich environment!
Re: How to tell if a variable is blessed ?
created: 2006-03-02 09:41:16

Have a look at ref

Re: How to tell if a variable is blessed ?
created: 2006-03-02 09:42:20
To tell you if the contents of the variable are a reference, and if so, the nature of that reference, use the function "[perldoc://ref]".
ref $var
foo bar

Regards,
Edward
Re^2: How to tell if a variable is blessed ?
created: 2006-03-02 10:07:05
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.
Re^2: How to tell if a variable is blessed ?
created: 2006-03-02 10:42:44
Better is
eval { local $SIG{__DIE__}; $ref->isa('Some::Package'); }
You might trigger a die handler, which could be bad. For example, one of [cpan://Test::More]'s dependencies has one. See [id://532653] 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 ?
created: 2006-03-02 09:44:26
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 ?
created: 2006-03-02 11:18:24
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 ?
created: 2006-03-02 23:35:51
Always search first, you'll save yourself time.

Example blessed => a direct hit, node 217784

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.

perlmonks.org content © perlmonks.org and davidrw, dragonchild, Fletch, jasonk, jeanluca, linux454, monkfan, PodMaster, wfsp, xdg

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03