Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

undef/defined &{$name} while "strict refs"

by anazawa (Scribe)
on Aug 21, 2012 at 01:42 UTC ( #988541=perlquestion: print w/ replies, xml ) Need Help??
anazawa has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, "strict refs" declaration confuses me. The declaration doesn't allow symbolic references. The following code tries to call, undef and defined subroutines using symbolic references while "strict refs".
# cat.pl use strict; use warnings; sub walk {} sub run {} my $walkable = defined &{ 'walk' }; undef &{ 'walk' }; &{ 'run' };
The above output the following:
$ perl cat.pl Can't use string ("run") as a subroutine ref while "strict refs" in us +e at cat.pl line 9.
According to perldiag, the above error message means:
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.

Comment on undef/defined &{$name} while "strict refs"
Select or Download Code
Re: undef/defined &{$name} while "strict refs"
by Khen1950fx (Canon) on Aug 21, 2012 at 02:38 UTC
    You could use "the one exception to the rule":
    #!/usr/bin/perl -l use autodie; use common::sense; sub walk { use strict qw/refs/; use warnings FATAL => 'syntax'; print "Walk this!"; } sub run { use strict qw/refs/; use warnings FATAL => 'syntax'; print "Run with this!"; } my $walkable = defined &{ 'walk' }; undef &{'walk'}; my $runable = \&{ 'run' }; &$runable;
      Thanks to your explanation, I understand "strict refs" allows the following:
      my $runable = \&{ 'run' }; &$runable;
      Your code shows how we avoid throwing exceptions. I think, however, it's still unclear why defined/undef &{ 'run' } doesn't throw exceptions while "strict refs".

        I think, however, it's still unclear why defined/undef &{ 'run' } doesn't throw exceptions while "strict refs". ,/i>

        Have you read what strict says about refs?

        Please read it now

Re: undef/defined &{$name} while "strict refs"
by AnomalousMonk (Abbot) on Aug 21, 2012 at 18:36 UTC
    ... I'm not sure why defined/undef &{'walk'} are allowed.

    I'm sure others can cite a precise historical development of the usage of strictures, but for me it's a question of expedience.

    The use of symbolic references is not discouraged because symbolic references are Evil, but because in certain cases experience has shown them to cause much trouble. These particular cases are foreclosed by  use strict 'refs';

    Some cases are both useful and entirely safe. Testing for the definition of a subroutine with
        defined &{ 'subroutine_name' }
    is obviously so.

    An indispensable use is the very creation of a hard reference in the first place. It's hard to see how some statement like
        my $coderef = \&{ 'subroutine_name' };
    is not going to be involved. Indeed, all the following work (and why should they not?)
        my $coderef = \&subroutine_name;
        my $coderef = \&{subroutine_name};
        my $coderef = \&{ 'subroutine_name' };
    although  &{subroutine_name} produces an "Ambiguous use of..." warning.

    The final case you mention is the potentially very useful  undef &{ 'subroutine_name' };   One can imagine doing this via a hard reference, e.g.
        my $coderef = \&{ 'subroutine_name' };
        undef $$coderef;
    or some such roundabout implementation (that one doesn't actually work, BTW), but what would be the point? After all, it's the name of the subroutine that ultimately has to be undefined; just going to the address of the actual code and scribbling something over it (a return? a jump to an exception?) would not seem to gain you very much.

    Anyway, my USD0.02.

      Your suggestion is insightful. We need to manipulate symbol table carefully. Though I was under the impression that the following causes autovivification, it doesn't:

      my $bool = defined &{ 'foo::bar' };
      c.f.
      my $namespace = do { no strict 'refs'; \%{"foo::"} }; my $bool = exists $namespace->{bar} && defined *{ $namespace->{bar} }{ +CODE};
      In fact, the following causes autovivification:
      no strict 'refs'; my $before = exists $foo::{bar}; # false my $bool = defined *{ 'foo::bar' }{CODE}; my $after = exists $foo::{bar}; # true
      I misunderstood defined &{ 'foo::bar' } cause autovivification. That's why you think defined &{ 'foo::bar' } is safe, right?

      I agree the name of the subroutine has to be undefined. We are allowed to avoid "no warnings redefine" when we use undef &{ 'foo::bar' }.
      undef &{ 'foo::bar' }; no strict 'refs'; *{ 'foo::bar' } = sub {};
      c.f.
      no strict 'refs'; no warnings 'redefine'; *{ 'foo::bar' } = sub {};
      By the way, the following should work:
      my $coderef = \&{ 'subroutine_name' }; #undef $$coderef; # typo ? undef &{ $coderef };
      Thanks for your comment :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988541]
Approved by GrandFather
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-12-28 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls