Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Can't Reference a Sub by Variable when using Strict

by Fletch (Bishop)
on Oct 04, 2004 at 02:08 UTC ( [id://396094]=note: print w/replies, xml ) Need Help??


in reply to Can't Reference a Sub by Variable when using Strict

Erm, because that's what use strict does. You either need to use coderefs (my %lists = ( stuff => \&test1, things => \&test2 );), or use the grandfathered goto syntax:

for my $i ( keys %lists ) { my $coderef = \&{ $i }; &{ $coderef }; }

Read perldoc strict for more details.

Replies are listed 'Best First'.
Re^2: Can't Reference a Sub by Variable when using Strict
by chromatic (Archbishop) on Oct 04, 2004 at 06:18 UTC

    Did you try your code under strict? There's a symbolic reference of the same kind in the second line.

      Creating symbolic references to subroutines is quite valid under strictures e.g
      use strict; sub foo { print "It's all good."; } my $subref = \&{ "foo" }; &$subref; __output__ It's all good.
      HTH

      _________
      broquaint

        That's a bug not a feature :-)

        C:\>perl -MO=Deparse test.pl sub foo { print q[It's all good.]; } my $subref = \&{'foo';}; &$subref;
      freebie:~ 1085> cat bar + 10:08:26 use strict; my %lists; for my $i ( keys %lists ) { my $coderef = \&{ $i }; &{ $coderef }(); } freebie:~ 1086> perl -c bar + 10:08:28 bar syntax OK

      This is perfectly valid due to the goto exception I mentioned. If you read perldoc strict:

      There is one exception to this rule: $bar = \&{'foo'}; &$bar; is allowed so that "goto &$AUTOLOAD" would not break unde +r stricture.

        So it is; thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found