On Sat, Jul 28, 2001 at 07:10:59PM -0700, Dan Grossman wrote:
> I'm wondering why Perl doesn't complain about the following code:
> 
> ----------
> #!/usr/bin/perl -w
> use strict;
> 
> my $funcRef = \&otherDummyFunc;
> my $oneVar = &callTheReferredFunc();
> print $oneVar;
> 
> sub dummyFunc {
>     return 42;
> }
> 
> sub otherDummyFunc {
>     return "your mom";
> }
> 
> sub callTheReferredFunc {
>     my $returnVal = &$funcRef;
>     return $returnVal;
> }
> ----------
> 
> Output: your mom
> 
> I don't pass $funcRef to &callTheReferredFunc, and yet "-w" doesn't
> generate a warning for an undefined reference.  Are function
> references somehow global in nature?  This doesn't seem to be true of,
> say, variable references.
> 
> I'm clearly missing something.  Explanations would be helpful ...

Since $funcRef isn't declared inside a block, it's global to the
entire file.

For more information, perldoc perlsub and look for the section
entitled "Private Variables via my()".  You might also want to read
Mark-Jason Dominus's article "Coping With Scoping", available on the
web at http://perl.plover.com/FAQs/Namespaces.html

Walt

PGP signature

Reply via email to