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

Re^2: Sub ref from string without eval

by moritz (Cardinal)
on Apr 02, 2012 at 15:01 UTC ( #963032=note: print w/replies, xml ) Need Help??


in reply to Re: Sub ref from string without eval
in thread Sub ref from string without eval

\&{$x} works perfectly fine with strict refs. It is only implicit usage of strings as references that is prohibted by strict refs. If you go to the length of writing \&{ ... }, perl assumes a symbolic reference is indeed what you want.
use strict; use warnings; use 5.010; sub Foo { 'in sub Foo' } my $x = 'Foo'; my $sub_ref = \&{$x}; say $sub_ref->(); __END__ in sub Foo

Replies are listed 'Best First'.
Re^3: Sub ref from string without eval
by ikegami (Pope) on Apr 02, 2012 at 19:09 UTC

    It is only implicit usage of strings as references that is prohibted by strict refs.

    Nope.

    $ perl -e'use strict; my $x = "foo"; \${ $x }' Can't use string ("foo") as a SCALAR ref while "strict refs" in use at + -e line 1. $ perl -e'use strict; \${ "foo" }' Can't use string ("foo") as a SCALAR ref while "strict refs" in use at + -e line 1.

    \&{ ... } is clearly an exception.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2020-12-01 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (5 votes). Check out past polls.

    Notices?