in reply to goto superclass method
Unfortunately, this doesn't work in vanilla Perl:
#!/usr/bin/perl use strict; use warnings; sub Foo::method { return "Foo" } BEGIN { @Bar::ISA = qw( Foo ); } sub Bar::method { goto $_[ 0 ]->SUPER::can( 'method' ); } print Bar->method(), "\n";
This is an infinite loop, because it dispatches to UNIVERSAL::can with $_[ 0 ] as the first parameter. This means that adorning the call with SUPER:: doesn't make a difference here. And since the package for $_[ 0 ] is Bar, can() finds Bar::method, so round and round we go…
But we can easily make this work. I came up with this before reading steves' reply, but it is just a cleaner, more polished version of that trick. Stick this somewhere in the code:
sub SUPER::can { my $caller = ( caller 1 )[ 3 ]; no strict 'refs'; local *$caller; return UNIVERSAL::can( @_ ); }
Tiny as it is, maybe this ought to be on CPAN?
See replies.
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re^2: goto superclass method
by merlyn (Sage) on Dec 22, 2004 at 21:34 UTC | |
by Aristotle (Chancellor) on Dec 22, 2004 at 21:49 UTC | |
by merlyn (Sage) on Dec 22, 2004 at 23:19 UTC | |
by Aristotle (Chancellor) on Dec 22, 2004 at 23:48 UTC | |
by merlyn (Sage) on Dec 22, 2004 at 23:53 UTC | |
| |
by steves (Curate) on Dec 22, 2004 at 21:41 UTC | |
by Aristotle (Chancellor) on Dec 22, 2004 at 21:50 UTC | |
by Mr. Muskrat (Canon) on Dec 22, 2004 at 22:00 UTC |