[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] ignoring undefined subroutines



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