Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: method aliases with goto(&NAME)

by tobyink (Abbot)
on Sep 17, 2013 at 11:32 UTC ( #1054424=note: print w/ replies, xml ) Need Help??


in reply to method aliases with goto(&NAME)

Won't somebody please think of the children?!?!.

package Parent { sub legacy_method { my ($self, @args) = @_; return $self->current_method(@args); } sub current_method { return 1; } } package Child { use base 'Parent'; sub current_method { return 2; } } Child->legacy_method; # returns 2

If you swap in your goto implementation of the legacy method, then calling Child->legacy_method returns 1.

Here's an alternative safer version using goto...

sub legacy_method { my $next = $_[0]->can('current_method'); goto $next; }

See also http://tvtropes.org/pmwiki/pmwiki.php/Main/ThinkOfTheChildren.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name


Comment on Re: method aliases with goto(&NAME)
Select or Download Code
Re^2: method aliases with goto(&NAME)
by LanX (Canon) on Sep 18, 2013 at 13:01 UTC
    > If you swap in your goto implementation of the legacy method, then calling Child->legacy_method returns 1.

    Don't you think this could perfectly be the intended behavior, since Child doesn't seem to have any legacy_method?

    But your right that the OP was wrong about the equivalence.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      There are probably instances where you'd want two methods that behave identically in the parent class, but behave differently in the child class. However, given that the title of this thread mentioned "method aliases", I'm assuming the intention is that legacy_method acts as an alias (i.e. behaves identically) for current_method, unless a child class explicitly overrides legacy_method to make it behave differently.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
        I had the impression that legacy_method is supposed to be deprecated and that overloading in new children is maybe not intended.

        Well I don't like speculating about the OPs intention, let's wait what he says... :)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

      Don't you think this could perfectly be the intended behavior, since Child doesn't seem to have any legacy_method?

      Not for one second. The child provides a correct interface. The legacy caller uses a correct interface. It should work.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-12-25 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (161 votes), past polls