Re: How to check if a function reference is valid or not?



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.
.



Relevant Pages

  • =?iso-8859-1?q?Re:_Einer_Subroutine_2_Hashes_=FCbergeben?=
    ... Aber wenn ich eine Subroutine schreibe, ... glücklicherweise jeder post FORTRAN Programmierer "call by ... "You may still declare a sub just as you did in Perl 5, ...
    (de.comp.lang.perl.misc)
  • Re: Perl Multithreading and use of Perl Modules
    ... >> I'm trying to create a multithreaded program in Perl 5.8 on Linux. ... The tracker() subroutine needs to be able to check the shared variables ...
    (comp.lang.perl.misc)
  • Re: Einer Subroutine 2 Hashes =?ISO-8859-1?Q?=FCbergeben?=
    ... > die Zuweisung im Funktionsaufruf völlig" dieselbe Semantik? ... > einen Ausdruck) an eine Subroutine übergibst, ... Die "Referenz" kann ja auch weitergereicht werden. ... Die von Perl gebotene Möglichkeit, ...
    (de.comp.lang.perl.misc)
  • RE: Perl / Apache on RH9.0 not working.
    ... script perl usually tells you there are returns but this time it didn't. ... > PerlInitHandler - Subroutine name ...
    (RedHat)
  • Re: A very general question to perl experts
    ... Below I am writing the stub-form of my perl code. ... If I define all the subroutines in the sequence mentioned above then, I face lot of wierd problems in readDefectData function. ... You should use the Perl5 style of subroutine calls ... sub extractSPRL { ...
    (perl.beginners)