http://www.perlmonks.org?node_id=396109


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.