Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: subroutine refs

by broquaint (Abbot)
on Nov 30, 2007 at 18:19 UTC ( #654158=note: print w/replies, xml ) Need Help??

in reply to subroutine refs

You can safely create a reference to a subroutine by name and the execute that e.g
use strict; sub dynamic { print "Mmm, dynamic.\n"; } my $name = 'dynamic'; my $subref = \&$name; &$subref();


Replies are listed 'Best First'.
Re^2: subroutine refs
by blokhead (Monsignor) on Nov 30, 2007 at 18:50 UTC
    I find this very surprising. But I checked it and it apparently works.

    Why does this give an error:

    use strict; sub dynamic { print "hello\n"; } my $name = "dynamic"; &$name();
    .. and yet this is ok? (they differ only in the last line)
    use strict; sub dynamic { print "hello\n"; } my $name = "dynamic"; &{ \&$name }();
    Before seeing this, I would have bet anything that the &{\&{ ... }} operation was absolutely the same as &{...} (barring weird action-at-a-distance caused by tying, overloading, etc). In fact, I'm not sure whether to consider this a bug or not. Indeed, what is \&$name doing other than "using string as a subroutine ref", which is outlawed under strict refs?

    Yet a similar idea with other kinds of refs doesn't do this. Both of these examples fail under strict refs (in my mind because @{\@{...}} is unconditionally the same thing as @{...} ):

    use strict; our @dynamic = ("hello\n"); my $name = "dynamic"; print @$name;
    use strict; our @dynamic = ("hello\n"); my $name = "dynamic"; print @{ \@$name };
    My conclusion is that there is an important distinction between naming a subroutine and invoking it, which is manifested bizarrely by different behaviors in the \&{blah} and &{blah} mechanisms.


Re^2: subroutine refs
by HelenCr (Monk) on Sep 14, 2013 at 11:44 UTC
    broquaint: This is amazing. I was looking for such a solution for a while.

    Thinking about it, is this canonical? (namely, documented)? or is it a quirk (an oversight in the Perl language design?)

      Removing the intermediate assignment to $name, broquaint’s code simplifies to:

      use strict; my $subref = \&{'dynamic'}; &$subref(); sub dynamic { print "Mmm, dynamic.\n"; }

      which is explicitly documented as an exception to the normal behaviour of strict 'refs' (see strict):

      There is one exception to this rule:

      $bar = \&{'foo'}; &$bar;

      is allowed so that goto &$AUTOLOAD would not break under stricture.

      So, yes, this is canonical.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://654158]
[LanX]: fun, last time I was in Britain people had problems with my name, just rarely they said "like the children TV star Rolf Harris?", this time most replied instantly "Rolf? like the pedophile Rolf Harris" ?
LanX is perlophile
[ambrus]: Corion: read https://metacpan. org/pod/release/ MLEHMANN/AnyEvent- 7.13/lib/AnyEvent. pm#SUPPLYING-YOUR- OWN-EVENT-MODEL- INTERFACE in that case

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2016-12-08 12:01 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (141 votes). Check out past polls.