* John Peacock <john.peacock@havurah-software.org> [2009-12-28 19:40]:
> Aristotle Pagaltzis wrote:
> > sub _isa {
> > my ( $val, $class ) = @_;
> > UNIVERSAL::can( $val, 'can' )
> Did you mean ------------------>'isa'
> > ? $val->isa( $class )
> > : $class eq ref $val;
> > }
No, as the following paragraph should have indicated.
I am not checking whether `$val` responds to `isa`. I am merely
checking whether `$val` is an object at all. Both `isa` and `can`
would work for this purpose as they’re methods that every object
should respond to in some way or other (and for testing object-
hood, you only need to know whether the thing responds to *any*
methods, not *which* or *how*).
But in this particular instance, the code then calls `isa` right
afterwards, and I don’t want to suggest that this is the pattern
of using `UNIVERSAL::can` to check if the object responds to the
method I want to call and then calling it.
Objects should be free to override `can`, which is is necessary
if they use `AUTOLOAD` (or maybe other magic). So if you want to
check whether an object responds to a particular method, you call
`$obj->can('method')`, not `UNIVERSAL::can($obj,'method')`.
This is *not* what I am doing. I don’t want to know whether the
object responds to some particular method. I want to know if the
thing is an object at all.
So I specifically only ever call `UNIVERSAL::can` to check if
something responds to `can`, and I only use the return value as
a boolean (even though it’s a reference if it’s a true value) –
ie. it’s a replacement for `Scalar::Util::blessed` that doesn’t
require loading Scalar::Util.
(Having to explain all this at all is another argument for having
a routine that just Does The Right Thing so no one has to think
about it in daily use.)
Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>
Thread Previous
|
Thread Next