Re: Using Scalar::Util

[prev] [thread] [next] [Date index for 2004/06/23]

From: Drew Taylor
Subject: Re: Using Scalar::Util
Date: 17:51 on 23 Jun 2004
Takes Tea at Half Past Three wrote:

> At 9:48 AM -0400 6/23/04, Drew Taylor wrote:
> 
>> While attending YAPC::NA last week, a talk brought the Scalar::Util 
>> module to my attention. Specifically, the blessed() function. This is 
>> a neat way to tell if a particular scalar is a blessed object.
>>
>> How does this relate to Class::DBI? There are LOTS of checks in method 
>> calls to make sure it is called either as an instance or class method. 
>> They all use something like
>>
>> sub foo {
>>   my $self = shift;
>>   # only called as instance method
>>   return unless ref $self;
>> }
>>
>> I propose that C::DBI should use blessed() instead since it's a better 
>> check. If Tony agrees, I'd be willing to submit a patch. The relevant 
>> docs are below. The changes should be pretty easy to make.
> 
> 
> In what way is it a "better" check? What case are you worried about 
> ref() getting wrong?

It's a better check because it actually looks at the perl internals to 
see if it's a blessed variable. AFAIK, there are no other methods to 
determine if a variable has the blessed magic set. It's not that I'm 
worried about ref() getting it wrong, rather just that it's a more 
complete check. If you happened to do this:

my $obj = [];
$obj->foo();

it will pass the ref() check, but blow up when it tries to do any method 
calls. I don't expect that this will happen in real life (if ever), but 
it's just a more complete check. And since the module is in a 
distribution already that is a dependency it's not any more effort.

Drew

> The code is trying to make sure you call MyPackage->foo() or 
> $my_obj->foo() appropriately, when both of those are reasonable things 
> to try.
> 
> It seems to me that the only way to call foo() with a reference as the 
> first argument that is not an instance of an appropriate class is to 
> explicitly do something like MyPackage::foo($randomref), which seems to 
> me to be deliberately shooting yourself in the foot, rather than 
> something that might accidentally happen.
> 
> Am I just missing something?
> 
> 


        -- 
        ----------------------------------------------------------------
Drew Taylor                 *  Web development & consulting
Email: drew@xxxxxxxxxx.xxx  *  Site implementation & hosting
Web  : www.drewtaylor.com   *  perl/mod_perl/DBI/mysql/postgres
----------------------------------------------------------------

Using Scalar::Util
Drew Taylor 13:48 on 23 Jun 2004

Re: Using Scalar::Util
Perrin Harkins 14:10 on 23 Jun 2004

Re: Using Scalar::Util
Drew Taylor 14:32 on 23 Jun 2004

Re: Using Scalar::Util
merlyn (Randal L. Schwartz) 15:53 on 23 Jun 2004

Re: Using Scalar::Util
Drew Taylor 16:43 on 23 Jun 2004

Re: Using Scalar::Util
Tony Bowden 16:53 on 23 Jun 2004

Re: Using Scalar::Util
Perrin Harkins 16:59 on 23 Jun 2004

Re: Using Scalar::Util
Drew Taylor 17:35 on 23 Jun 2004

Re: Using Scalar::Util
Tony Bowden 18:36 on 23 Jun 2004

Re: Using Scalar::Util
Drew Taylor 19:01 on 23 Jun 2004

Re: Using Scalar::Util
Takes Tea at Half Past Three 17:40 on 23 Jun 2004

Re: Using Scalar::Util
Drew Taylor 17:51 on 23 Jun 2004

Re: Using Scalar::Util
Takes Tea at Half Past Three 18:02 on 23 Jun 2004

Re: Using Scalar::Util
Drew Taylor 18:27 on 23 Jun 2004

Re: Using Scalar::Util
Edward J. Sabol 18:27 on 23 Jun 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52