Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by ikegami (Patriarch)
on Apr 02, 2019 at 15:33 UTC ( [id://1232026]=note: print w/replies, xml ) Need Help??


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

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.)

Replies are listed 'Best First'.
Re^3: Function name in a variable, can't recall the concept
by Eily (Monsignor) on Apr 03, 2019 at 08:18 UTC

    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.

Re^3: Function name in a variable, can't recall the concept
by LanX (Saint) on Apr 14, 2019 at 13:50 UTC
    > 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.

        > Those aren't really exceptions.

        I was referring to "allowed under strict" requirement.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found