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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|