Home > Archive > PERL Beginners > November 2007 > How to check if a function reference is valid or not?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to check if a function reference is valid or not?
|
|
|
| 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?
Thanks.
| |
| Paul Johnson 2007-11-28, 8:00 am |
| On Wed, Nov 28, 2007 at 02:15:39AM -0800, howa wrote:
> sub abc {
> print 'abc';
> }
print "<", exists &abc, ">\n";
print "<", exists &abcee, ">\n";
> my $f1 = \&abc;
> my $f2 = \&abcee;
>
> print $f1; # return CODE(0x..)
> print $f2; # also return CODE(0x..)
print "<", defined &abc, ">\n";
print "<", defined &abcee, ">\n";
> $f1->();
> $f2->();
>
> How do I know if $f2 is a valid function reference, without actual
> calling it?
perldoc -f exists
perldoc -f defined
Note that a sub exists after taking a reference to it even though it
won't necessarily e defined.
--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
| |
| John W . Krahn 2007-11-28, 8:00 am |
| On Wednesday 28 November 2007 02:15, howa 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?
$ perl -le'
sub abc {
print 'abc';
}
my $f1 = \&abc;
my $f2 = \&abcee;
print "\$f1 ", defined &$f1 ? "exists" : "does not exist";
print "\$f2 ", defined &$f2 ? "exists" : "does not exist";
$f1->();
$f2->();
'
$f1 exists
$f2 does not exist
Undefined subroutine &main::abcee called at -e line 13.
John
--
use Perl;
program
fulfillment
| |
| Jeff Pang 2007-11-28, 8:00 am |
| On Nov 28, 2007 6:15 PM, howa <howachen@gmail.com> 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.
| |
| John W . Krahn 2007-11-28, 8:00 am |
| On Wednesday 28 November 2007 04:14, Jeff Pang wrote:
>
> 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.
Correct.
> so, abcee can be anything (a hash, an
> array, a scalar, a subroutine, a handler etc).
No, it is assigning a code reference so only the *main::abcee{CODE}
slot in the symbol table will be in use.
If you had said:
use vars '*abcee';
then abcee could be anything.
John
--
use Perl;
program
fulfillment
| |
|
| On 11$B7n(B28$BF|(B, $B2<8a(B8$B;~(B36$BJ,(B, kra...@telus.net (John W . Krahn) wrote:
> On Wednesday 28 November 2007 04:14, Jeff Pang wrote:
>
>
>
>
>
>
>
> Correct.
>
>
> No, it is assigning a code reference so only the *main::abcee{CODE}
> slot in the symbol table will be in use.
>
> If you had said:
>
> use vars '*abcee';
>
> then abcee could be anything.
>
> John
> --
> use Perl;
> program
> fulfillment
Thanks all
Howard
|
|
|
|
|