in reply to Referencing the locals

${a} is just another way to write $a (perldata). ${'a'} is a symbolic reference. The latter is forbidden under strict, and that's a good thing, as it can easily cause all kinds of hard-to-debug chaos (Update: one example). See also Use strict and warnings.

The typical case where I want to use it is when I have a handful of arrays and want to do stuff with each of them ... How can I reference the local ones?

No, you don't want to ;-) You likely want a hash of arrays instead (perldsc).

my %animals = ( cat => [1,2,3], dog => [5,6], pig => [4,3,2,1], );

See also Why it's stupid to `use a variable as a variable name'.

Replies are listed 'Best First'.
Use case for symbolic references (was: Re^2: Referencing the locals)
by Bod (Parson) on Apr 28, 2021 at 21:04 UTC
    ${'a'} is a symbolic reference. The latter is forbidden under strict, and that's a good thing, as it can easily cause all kinds of hard-to-debug chaos

    Perhaps a silly question but I don't know the answer so I shall ask it anyway...
    Is there ever a case to use a symbolic reference in Perl? The documentation doesn't give any hint as to why one would be needed other than to describe them as powerful.

      Is there ever a case to use a symbolic reference in Perl?

      Two places that come to mind where I regularly write no strict 'refs'; is dynamic method/function generation (example 1, example 2) and implementations of import (example 1, example 2, at the bottom of each node). Other than those legitimate uses, there's interaction with legacy code (example 1, example 2). Note how in each of those examples the scope of the no strict is limited to be as small as possible.

      Update: Added more links to examples.

      Historically, they were probably useful, but I came to Perl when 5.6 was already there, so I don't remember them. Nowadays, strict forbids them, so the answer is "No". On the contrary, check Can you use string as a HASH ref while "strict refs" in use? on how confusing they can be.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        > Nowadays, strict forbids them, so the answer is "No"

        Well except for all cases of no strict 'refs'; where the answer is "Yes". ;)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery