in reply to Re^3: Am I evil for doing this?
in thread Am I evil for doing this?

It's so much less evil, in fact, that it's actually allowed by strict.
Yes; but there is considerable disagreement as to whether this ought to be the case. Some very respectable occupants of the Perl pantheon believe that it's an unnecessary and arbitrary loophole.
If you wanted to be extra careful, you could use UNIVERSAL::can...

That actually depends on what you want. If you want to allow the possibility that the method could get handled by AUTOLOAD, then you should not pre-test with UNIVERSAL::can.

There's also an issue with how you call can. I figure, if you don't know whether the object can handle the $method, then you might not know if it's even an object; so to avoid the error potentially resulting when $self is not an object, I usually do the following, though unfortunately it isn't as pretty:

if ( UNIVERSAL::can( $self, $method ) ) {

Replies are listed 'Best First'.
Re^5: Am I evil for doing this?
by ihb (Deacon) on Mar 11, 2005 at 12:39 UTC

    if ( UNIVERSAL::can( $self, $method ) ) {

    That too, unfortunately, isn't good either, if an autoloading class properly overloads can. Imho, a class that uses AUTOLOAD also has the obligation to overload can accordingly. If it doesn't that class should be fixed, not mine, if that's an option.

    I prefer to do can checks like

    if (blessed($self) || ! ref($self) and my $code = $self->can($method)) + { $self->$code(...); }
    if I need to handle $self's that can't invoke methods.

    Update: Forgot to mention &blessed comes from Scalar::Util.

    ihb

    See perltoc if you don't know which perldoc to read!

      That's an interesting possibility too. I've never run into it yet, but I'm sure it will bite me sooner or later!