my, strict, and function references
Hi, 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 ... Thanks, Daniel -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: my, strict, and function references
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
Re: my, strict, and function references
On Jul 28, Dan Grossman said: #!/usr/bin/perl -w use strict; my $funcRef = \otherDummyFunc; sub callTheReferredFunc { my $returnVal = $funcRef; return $returnVal; } 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. You're confusing something here, and I can't tell what it is. The variable $funcRef is NOT global -- it is lexically scoped (because you used my() on it). It is not visible OUTSIDE the scope that you declared it in -- but it IS visible inside smaller scopes, like the one of the callTheReferredFunc() function. -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ http://www.perlmonks.com/ http://search.cpan.org/ Acacia Fraternity, Rensselaer Chapter. Brother #734 ** Manning Publications, Co, is publishing my Perl regex book ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: my, strict, and function references
On Sat, 28 Jul 2001, Jeff 'japhy/Marillion' Pinyan wrote: On Jul 28, Dan Grossman said: #!/usr/bin/perl -w use strict; my $funcRef = \otherDummyFunc; sub callTheReferredFunc { my $returnVal = $funcRef; return $returnVal; } 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. You're confusing something here, and I can't tell what it is. The variable $funcRef is NOT global -- it is lexically scoped (because you used my() on it). It is not visible OUTSIDE the scope that you declared it in -- but it IS visible inside smaller scopes, like the one of the callTheReferredFunc() function. Ah, I see what my problem was. I was aware of the relevant scoping issues but had also tried the following, which generated an error message (whereas the previous code had not): -- my $dummyVar = 1; my $varRef = \$dummyVar; my $oneVar = dummyFunc(); print $oneVar,\n; sub dummyFunc { my $returnVal = $varRef; # -- note the typo return $returnVal; } -- I was trying to figure out what the heck the difference is between a reference to a function and a reference to a variable that would cause Perl to like one but not the other when accessed inside the function. Of course, I had meant to type $$varRef inside the function, rather than $varRef ... Thanks, Daniel -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]