[Omaha.pm] ignoring undefined subroutines

Hugh Jarce hjarce2001 at yahoo.com
Thu Sep 9 04:54:51 CDT 2004


Jay Hannah wrote:
> How would you know that header_$who is a sub and not just an errant
> scalar defined in the package "main"?
>
> The code below blows up unless you take the $two = line out...
>
> j
>
> -----------
> % cat j.pl
> #!/usr/bin/perl
> 
> my @subs = qw( one two three );
> $two = "kaboom!";    # Comment this line out and everything is fine
> foreach (@subs) {
>     if (exists($main::{$_})) {
>        &$_;
>     }
> }
> sub one { print "1\n"; }
> sub three { print "3\n"; }
> 
> % ./j.pl
> 1
> Undefined subroutine &main::two called at ./j.pl line 7.

Your solutions are better, but since you asked... ;-)

h

-----------
% cat h.pl
#!/usr/bin/perl

my @subs = qw( one two three );
$two = "kaboom!";
foreach (@subs) {
    local *sym = $main::{$_};
    defined($sym) and print "jay is trying to break hugh's code\n";
    defined(*sym{CODE}) and &$_;
}
sub one { print "1\n"; }
sub three { print "3\n"; }

% ./h.pl
1
jay is trying to break hugh's code
3

See also Devel::Symdump.



		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 


More information about the Omaha-pm mailing list