Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: subroutine refs

by blokhead (Monsignor)
on Nov 30, 2007 at 18:50 UTC ( #654166=note: print w/ replies, xml ) Need Help??


in reply to Re: subroutine refs
in thread subroutine refs

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.

blokhead


Comment on Re^2: subroutine refs
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://654166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-23 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (133 votes), past polls