Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

•Re: Detecting function vs method in an AUTOLOAD?

by merlyn (Sage)
on Apr 11, 2004 at 23:19 UTC ( #344295=note: print w/ replies, xml ) Need Help??


in reply to Detecting function vs method in an AUTOLOAD?

I hope you never figure this out. There's a lot of code out there that presumes that nothing can detect the difference between method and subroutine calls. This is deliberate, and is thus a feature, not a bug.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


Comment on •Re: Detecting function vs method in an AUTOLOAD?
Re: •Re: Detecting function vs method in an AUTOLOAD?
by tilly (Archbishop) on Apr 11, 2004 at 23:34 UTC
    It is documented that AUTOLOAD got inherited for functions by accident, that feature is deprecated, and it will break at some point in the future.

    Why then should we actively discourage authors of AUTOLOADs who want to try to match future behaviour, now? Offering people no way to smooth the transition between how their code acts now and how it will act in some future release seems like a pretty serious omission to me.

    Furthermore it means that adding an AUTOLOAD can introduce many unintended effects that you have no way around. (Which is probably one reason why inherited AUTOLOADs for functions is now deprecated.)

    My attitude is this. A normal method has no way of knowing what package you tried to call it in. However an AUTOLOAD method has that information in $AUTOLOAD. Why? Because details of how you dispatched to AUTOLOAD can very reasonably affect what that AUTOLOAD should do. While I agree that a normal function or method should not guess, in an AUTOLOAD you may want that information for similar reasons.

Re^2: Detecting function vs method in an AUTOLOAD?
by bart (Canon) on Apr 12, 2004 at 01:01 UTC
    There's a lot of code out there that presumes that nothing can detect the difference between method and subroutine calls. This is deliberate, and is thus a feature, not a bug.
    Thus the resulting mess in CGI.pm for example, trying to distinguish between method and function, is by design? Jeezes.
Re: •Re: Detecting function vs method in an AUTOLOAD?
by TimToady (Parson) on Apr 12, 2004 at 19:10 UTC
    From the viewpoint of Perl 5, it's a feature, but from the viewpoint of Perl 6, it's a bug. Perl 6 will distinguish AUTOMETH from AUTOSUB. (Those are the declarative forms, which return references. Perl 6 will distinguish those from the defining forms, which are spelled AUTOMETHDEF and AUTOSUBDEF, and which may only be called on predeclared (or autodeclared) names to supply a missing definition.)
      So, there'll be ways to "fake" a method call using a subroutine call, like we have now? There'll have to be, or else you can't get Perl5 semantics inside Perl6 syntax, as promised.

      For example, this should still work, somehow:

      my $meth = $object->can("foo"); $meth->($object, $arg1, $arg2); # simulate $object->foo($arg1, $arg2);

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Even today, in Perl5, I'd write that as

        $object->$meth($arg1,$arg2);

        but it's clear that one or the other should be there, and not something ugly like

        apply($object,$method,$arg1,$arg2);
        or, assuming that can returned a bound method,
        my $meth_ref = $object->can($method); apply($meth_ref,$arg1,$arg2);
        Yes, once you've taken a reference to a method, it's essentially a sub ref at that point, so the Perl 6 equivalent of your code works as you'd expect. In fact, the standard dispatchers are defined in those terms. But the standard dispatchers will be able to keep subs and methods straight to the extent that you wish them to (which, by default, is up to the point that a method turns out to be completely missing, and you wish it to "failover" to looking for a multi-sub instead).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://344295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-11-25 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (148 votes), past polls