Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Array wierdness

by Animator (Hermit)
on Apr 25, 2022 at 15:36 UTC ( [id://11143277]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Array wierdness
in thread Array wierdness

Just to elaborate a bit more on this, consider the code:
my @PP = ("foo", "bar", "baz"); my @OO = ("AAA", "bar", "CCC"); $PP[1][0] = "foobar";
What happens is that this does not modify the @PP array, instead it uses the value of $PP[1] as an array reference which points to the global(/package) variable @bar. To make this a bit clearer:
#!/usr/bin/perl -l my @PP = ("foo", "bar", "baz"); my @OO = ("AAA", "bar", "CCC"); $PP[1][0] = "foobar"; print "\@PP: " . join(", ", @PP); print "\@OO: " . join(", ", @OO); print "\@bar: " . join(", ", @::bar);
Output:
@PP: foo, bar, baz
@OO: AAA, bar, CCC
@bar: foobar
The '@PP' and '@OO' array are unmodified; the '@bar' array however was created.

With use strict; you get the error: Can't use string ("bar") as an ARRAY ref while "strict refs" in use

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-29 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found