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") };
| [reply] [d/l] [select] |
|
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. | [reply] [d/l] [select] |
|
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:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] [d/l] |
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).
| [reply] [d/l] [select] |
|
$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.
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
|
|
|
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
| [reply] [d/l] |
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 .. | [reply] |
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 | [reply] [d/l] |
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. |
| [reply] |
Re: How to tell if a variable is blessed ?
by wfsp (Abbot) on Mar 02, 2006 at 14:41 UTC
|
| [reply] |