Re: How to check if a function reference is valid or not?
- From: pangj@xxxxxxxxxxxxx (Jeff Pang)
- Date: Wed, 28 Nov 2007 20:14:40 +0800
On Nov 28, 2007 6:15 PM, howa <howachen@xxxxxxxxx> wrote:
sub abc {
print 'abc';
}
my $f1 = \&abc;
my $f2 = \&abcee;
print $f1; # return CODE(0x..)
print $f2; # also return CODE(0x..)
$f1->();
$f2->();
How do I know if $f2 is a valid function reference, without actual
calling it?
My answer is: you can't.
See this test:
sub abc {
print 'abc';
}
my $f1 = \&abc;
my $f2 = \&abcee;
for (keys %::) {
print "$_ -> $::{$_}\n" if /abc/;
}
__END__
the output is:
abcee -> *main::abcee
abc -> *main::abc
that's to say, when you say $f2 = \&abcee you have created an entry in
this script's symbol table. so, abcee can be anything (a hash, an
array, a scalar, a subroutine, a handler etc).
when and only when you call 'abcee' as a subroutine, perl can't find
the corresponding code substance, you have the chance to find the
error.
.
- Follow-Ups:
- Re: How to check if a function reference is valid or not?
- From: John W . Krahn
- Re: How to check if a function reference is valid or not?
- References:
- Prev by Date: Re: How to check if a function reference is valid or not?
- Next by Date: Re: can't run mailx as PERL system command
- Previous by thread: Re: How to check if a function reference is valid or not?
- Next by thread: Re: How to check if a function reference is valid or not?
- Index(es):
Relevant Pages
|