Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Help.pm automatically explains how to use misused objects

by lodin (Hermit)
on Jan 12, 2008 at 08:09 UTC ( #662042=note: print w/replies, xml ) Need Help??


in reply to Help.pm automatically explains how to use misused objects

This is a nice module, and should be combined with Symbol::Sub::Approx to actively help (while issuing a warning). :-)

There's a major problem with it though; the usual AUTOLOAD problem. Maybe another class in the inheritance tree has an AUTOLOAD to handle it. Then Help isn't really helping--rather the opposite. So you need to do

use 5.010; use mro; # Make next::can available. use Sub::Name (); # Used to make next::can work # in anonymous subroutines. sub AUTOLOAD { my ($bottom_class, $method) = $AUTOLOAD =~ /(.*)::(.*)/s; # Note the /s above. It's legal to have # newlines in packages and symbols. my $next = Sub::Name::subname( $method => sub { $bottom_class->next::can } )->(); goto &$next if $next; ...; }
By adding just two more statements in AUTOLOAD you illustrate a new Perl 5.10 feature. :-)

But when moving onto Perl 5.10 your algorithm, while elegant and simple, becomes fragile due to the new method resolution orders (mro). Utilizing the lovely Devel::Symdump module by Andreas J. König you can replace

my %known_method; my @classes = ($bottom_class); while (@classes) { my $class = shift @classes; next if $class eq __PACKAGE__; unshift @classes, @{"$class\::ISA"}; for my $name (keys %{"$class\::"}) { next unless defined &{"$class\::$name"}; $known_method{$name} ||= $class; } }
with
use Devel::Symdump; my %known_method = reverse map /(.*)::(.*)/s, map Devel::Symdump::->functions($_), grep $_ ne __PACKAGE__, @{mro::get_linear_isa($class)} ;
If you're on a pre 5.10 perl you can use Class::ISA's self_and_super_path. (I've contacted Mr Burke, the maintainer of Class::ISA, about updating it to use mro::get_linear_isa when it's available. Currently you need to figure out that yourself.)

Of course, this makes it much less educational about the symbol table. :-)

Update: I remembered that Help makes itself part of the inheritance tree, so I've removed the import of &subname to avoid it being inherited, and removed Help from %known_method.

lodin

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-05-09 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (100 votes). Check out past polls.

    Notices?