Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Referencing the locals

by davido (Cardinal)
on Apr 28, 2021 at 19:18 UTC ( [id://11131813]=note: print w/replies, xml ) Need Help??


in reply to Referencing the locals

Don't wield symbolic references for this purpose. You don't need them here. Use strict to disabuse yourself of any temptation. Why it's stupid to use a variable as a variable name.

In your code above, ${'a'} is a symbolic reference to a package global named $main::a. And ${a} is synonymous with $a, which in the scope you're using it in, refers to the lexical version of $a, not the package global.

Instead, either use real references, or a hash:

# Using hard references my @cat = (1,2,3); my @dog = (5,6); my @pig = (4,3,2,1); for my $array (\@cat, \@dog, \@pig) { # On first iteration $array will contain a reference to @cat. # On second iteration, $array will contain a reference to @dog. # On third iteration $array will contain a reference to @pig. }

...or do this...

# Using a hash my %animals = ( cat => [1,2,3], dog => [5,6], pig => [4,3,2,1], ); for my $animal_type (keys %animals) { my @values = @{$animals{$animal_type}}; # Animals will come in an unpredictable order, but as an example: # First iteration, dog will be handled. # Second iteration, pig will be handled. # Third iteration, cat will be handled. }

Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2026-01-14 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (118 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.