Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Can't Reference a Sub by Variable when using Strict

by !1 (Hermit)
on Oct 04, 2004 at 04:44 UTC ( #396109=note: print w/replies, xml ) Need Help??


in reply to Can't Reference a Sub by Variable when using Strict

I'm usually partial to this method. Of course, you might not like the fact that it searches @ISA for a class that contains the subroutine. Oh well.

#!/usr/bin/perl -w use strict; my %lists = ( 'test1' => "stuff", 'test2' => "things", 'test3' => "whatever" ); for my $i (keys %lists) { my $sub = UNIVERSAL::can(__PACKAGE__,$i) || sub { warn "Couldn't cal +l $i" }; &$sub; } sub test1 { print "New message for test1\n"; } sub test2 { print "New message for test2\n"; } __END__ New message for test1 Couldn't call test3 at - line 13. New message for test2

Replies are listed 'Best First'.
Re^2: Can't Reference a Sub by Variable when using Strict
by chromatic (Archbishop) on Oct 04, 2004 at 06:20 UTC

    If you used __PACKAGE__->can( $i ) instead, you could pick up on overridden can() methods and might find out that the package can do much more than you thought it could. That can be very useful sometimes.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2021-09-16 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?