Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re^2: subroutine refs by blokhead
in thread subroutine refs by CColin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-16 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found