anazawa has asked for the wisdom of the Perl Monks concerning the following question:
The above output the following:# cat.pl use strict; use warnings; sub walk {} sub run {} my $walkable = defined &{ 'walk' }; undef &{ 'walk' }; &{ 'run' };
According to perldiag, the above error message means:$ perl cat.pl Can't use string ("run") as a subroutine ref while "strict refs" in us +e at cat.pl line 9.
Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref.
I understand why the script throws an exception, but I'm not sure why defined/undef &{'walk'} are allowed. Are there any differences?
Sincerely,
Ryo
UPDATE: I noticed &{ 'run' } calls run(), while defined/undef &{ 'walk' } never call walk(). I think that's the difference. PBP written by D. Conway says "18.7 Always use strict". In this section, \&{$DUMP} doesn't violate "strict refs" because the expression doesn't call dump_at(). I thank PBP :-)
NOTE: PBP says "Never use symbolic references". This practice is undoubtedly reasonable.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: undef/defined &{$name} while "strict refs"
by Khen1950fx (Canon) on Aug 21, 2012 at 02:38 UTC | |
by anazawa (Scribe) on Aug 21, 2012 at 03:22 UTC | |
by Anonymous Monk on Aug 21, 2012 at 06:43 UTC | |
by anazawa (Scribe) on Aug 21, 2012 at 07:04 UTC | |
by Anonymous Monk on Aug 21, 2012 at 07:22 UTC | |
| |
|
Re: undef/defined &{$name} while "strict refs"
by AnomalousMonk (Archbishop) on Aug 21, 2012 at 18:36 UTC | |
by anazawa (Scribe) on Aug 21, 2012 at 19:49 UTC |