develooper Front page | perl.perl5.porters | Postings from July 2011

[perl #47181] Try OO semantics before throwing fatal error

Thread Previous
Father Chrysostomos via RT
July 17, 2011 22:35
[perl #47181] Try OO semantics before throwing fatal error
Message ID:
On Sun Jul 17 18:11:29 2011, LAWalsh wrote:
> If one of the new method resolution orders doesn't work, 
> an easy way to prevent the problem is to prevent perl from evaluating the
> "\&method" until runtime.  i.e.
> Orig program was:
> package A;
> sub method {'method'};
> package B; @ISA=qw(A);
> sub setup_error{ $x=\&method;}
> package main;
> bless my $ref={},B;
> print $ref->method . "\n";
> $ref->setup_error();
> print $ref->method . "\n"; 
> Simple fix:
> in line w/sub setup_error, change that to:
> sub setup_error{ eval "$x=\&method;"}

I don’t think you’re doing what you think you’re doing. :-)

If I add ‘warn $@’ to the end of setup_error, I get:
    syntax error at (eval 1) line 1, near "="
because the $x is interpolated.

If you want to take a reference to a subroutine, but only if it exists
(i.e., no autovivving), use *method{CODE}.

In fact, if you want to ignore stubs in method calls, you could write
your own AUTOLOAD to deal with that, and maybe even put it in a CPAN
module. :-)

> Yeah, it's a hack to get around the original 'hack' implemented for
> autoload, but it certainly low cost to implement and requires no work to
> change perl!
> (the things you learn & think of over time...).
> Of course perl getting much better in 5.10 and improving in 5.12 helps

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About