Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Function name in a variable, can't recall the concept

by Eily (Monsignor)
on Apr 02, 2019 at 14:27 UTC ( [id://1232015]=note: print w/replies, xml ) Need Help??


in reply to Function name in a variable, can't recall the concept

That's a symbolic reference, which is not allowed under strict:

perl -E "use strict; use warnings; sub hello { say 'Hi' }; my $sub = ' +hello'; $sub->()" Can't use string ("hello") as a subroutine ref while "strict refs" in +use at -e line 1.
So if your Pythonista were to look into it, he would find a lot of "Don't do that!"

Replies are listed 'Best First'.
Re^2: Function name in a variable, can't recall the concept
by ikegami (Patriarch) on Apr 02, 2019 at 15:33 UTC

    It's technically the dereference of a symbolic reference that's not allowed. However, there is a (documented) exception:

    \&$sub_name

    This means that while

    $sub_name->()

    isn't allowed,

    (\&$sub_name)->()

    is allowed.

    ($invocant->$method_name() is also allowed under strict.)

      It's technically the dereference of a symbolic reference that's not allowed.
      Is there another case where you would call a variable a symbolic reference when it's not being dereferenced? With eval or when accessing a value through the symbols table?

      You're right about those two exceptions, at least the exceptions are complex enough that beginners shouldn't use them by accident.

      > However, there is an exception:

      More there are:

      use strict; use warnings; sub tst { warn "tst($_[0]) called\n" } my $symbol="tst"; ($::{$symbol})->(1); &{$::{$symbol}}(2); (main->can($symbol))->(3); &{main->can($symbol)}(4);

      C:/Perl_524/bin\perl.exe d:/exp/symbolic_references.pl tst(1) called tst(2) called tst(3) called tst(4) called

      NB:

    • $::{...} is a shortcut for STASH lookup $main::{...} and returns a type-glob
    • ->can(...) returns a coderef

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Those aren't really exceptions. Strict refs is only meant to stop you from accidentally using symbolic refs. (After all, you can always turn it off strict refs off if you want to use symbolic refs intentionally.) %:: and can only take symbol names, so they can't accidentally be used incorrectly.

Re^2: Function name in a variable, can't recall the concept
by bliako (Monsignor) on Apr 02, 2019 at 20:55 UTC

    Here is another exception, no complaints:

    use strict; use warnings; sub asub { print "heee\n"; } my $x = 'asub'; main->$x();

    bw, bliako

      Not really an exception IMHO because asub is a "method" of the main class/package, so the  -> invocant works as documented.


      Give a man a fish:  <%-{-{-{-<

        I would like to call it a method too but where is the reference to the main class/package "object" as the first parameter when calling asub() (a-la $obj->asub() which sends $obj as the first parameter)? Also, I would like to call it a "static method" but this: main::$x() does not work. Too many exceptions! (Edit: could n't resist the pun of too many anomalies :) )

        bw, bliako

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found