Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.
I get awfully tired of writing this:
use Scalar::Util 'blessed';
if ( blessed($thing) && $thing->can('do_something') ) {... }
Which is why I'm awfully tempted to just write this almost throwaway code and put it on the CPAN. I probably won't, but I toy with the idea.
package Can;
use strict;
use warnings;
use Scalar::Util 'blessed';
use base 'Exporter';
@EXPORT = 'Can';
sub Can {
my ($object, $method) = @_;
return unless blessed $object;
return $object->can($method);
}
1;
And in my code:
use Can; # use Can () if you don't want importing
if ( Can( $thing, 'do_something' ) ) {... }
Depending upon the type of code you write, it's quite easy to get an unblessed reference instead of an object. I get tired of always having to use blessed from Scalar::Util, but this is a common enough practice (I hit a lot in exception handling, for example) that it seems like a "write it and forget it" module might be useful.
And for those who do UNIVERSAL::can($object, $method), don't. If the object overrides &can, you're in trouble. If you already know your objects don't override &can, then you're relying on implementation details that you shouldn't be relying on.
This is also an API problem (Score:1)
Re:This is also an API problem (Score:1)
What if
$objectcontains a class name?Re:This is also an API problem (Score:1)
$classname->foo and &classname::foo exists, and can be called. How they are supposed to be called is up to the module author and consumer, and not the concern of can.
Re:This is also an API problem (Score:1)
I simply mean that there isn't a single use for
UNIVERSAL::canthat makes a replacement easy to write.UNIVERSAL::isais even worse.Simpler means (Score:2)
Re:Simpler means (Score:1)
I really need to get around to adding _CAN to Params::Util
Re:Simpler means (Score:2)