tinita has asked for the wisdom of the Perl Monks concerning the following question:

hi perl monks, i'm just wondering about some code i can use with strict.pm. if I have a subroutine name and i want the coderef for it i always use this:
my $subname = "package::sub"; if (my $coderef = main->can($subname)) { $coderef->(@args); }

i thought the following would generate an error:

use strict; my $coderef = \&$subname; $coderef->(@args);
but it works fine.

syntactically &{$sub} is equal to ${$var}, right? so is \&{$sub} to \${$var}.
but while the latter one does not work with strict, the former does. is this documented somewhere or am I missing the obvious?

update:
*blush*
forget it...

perldoc strict: There is one exception to this rule: $bar = \&{'foo'}; &$bar;

Replies are listed 'Best First'.
Re: strict and symbolic subroutine refs
by Zaxo (Archbishop) on Jul 24, 2004 at 14:29 UTC

    You've hit a special case which strict.pm allows. perldoc strict will tell you that this is to allow magical goto &$AUTOLOAD; to survive strict 'refs'.

    After Compline,
    Zaxo

Re: strict and symbolic subroutine refs
by nothingmuch (Priest) on Jul 24, 2004 at 14:29 UTC
    I have three opinions on the matter.

    1. use strict should prevent people from doing that since it's no different than variables. It could be a bug. But it's documented. This is to make goto &$AUTOLOAD not break.

    2. Are you sure that the solution to your problem, which dances around what is "allowed" and what isn't in terms of strictness is correct in the maintainable sense?

    3. If you're undermining use strict, but know how what you're doing is "bad" and furthermore, know what you're doing, what does it really matter? You could keep a hash of subreferences, and in practice you'll be doing the same thing (the symbol table is a hash), except in a syntatically more recognizable way. use strict is meant to be a tool that helps you not shoot yourself in the foot. If you find it limiting, or find yourself needing to see where it is limiting to get some work done, I don't think it's being used correctly.

    -nuffin
    zz zZ Z Z #!perl
      You could keep a hash of subreferences, and in practice you'll be doing the same thing (the symbol table is a hash),
      we're not talking about me having a problem with strict.pm. (1) =)
      actually i never used the my $coderef = main->can($subname) thing for projects i'm programming on. just for trying out.
      i'm well aware that a hash of subrefs would be the way to go, i just saw that code \&$subname somewhere, and i really never stumbled upon that paragraph in strict.pm where it is documented.
      it's just that i usually thought that would require no strict, and i find that in more people's code that they are doing something like that:
      no strict; my $ref = \&$name;
      so i was really surprised it worked with strict.

      (1) i'm using 'no strict' a lot, though, for munging the symbol table (dynmic class generation and things like that)

        I sounded a bit pedantic, I regret to admit.

        What I really meant was that even if no strict is unnecessary, it might be a good idea to put it there so the code is more self documenting, or to use a solution that doesn't need no strict (is that a double negative?), not because it is a better solution, but because it might be more coherent. The code refs in hash was just an example, I guess.

        -nuffin
        zz zZ Z Z #!perl