Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Newbie: query about array assignment

by AnomalousMonk (Abbot)
on Sep 13, 2012 at 17:21 UTC ( #993539=note: print w/ replies, xml ) Need Help??


in reply to Newbie: query about array assignment

@stuff = "This","That","Other";

When I first saw this and other, similar code fragments in this thread, my first expectation was that the comma expression  "This","That","Other" would evaluate to the right-most list item, "Other". After all, that's the rule: Evaluate left side, throw away, return right side in left-associative fashion.

After a bit of thought, I realized that the rule is to evaluate the leftmost expression, toss the result, evaluate and return the result of the rightmost expression, left-associative. Of course, the leftmost expression has a side effect: it creates and initializes the  @stuff array. The following helped me elucidate the process:

>perl -wMstrict -MO=Deparse,-p -le "my @stuff = 'One', 'Two', 'Three'; print qq{(@stuff)}; ;; my @stoff = scalar('Eine', 'Zwei', 'Drei'); print qq{(@stoff)}; ;; my @sniff = scalar(my @snarf = '111', '222', '333'); print qq{sniff: (@sniff) snarf: (@snarf)}; ;; my @stiff = ('Uno', 'Dos', 'Tres'); print qq{(@stiff)}; " Useless use of a constant (Eine) in void context at -e line 1. Useless use of a constant (Zwei) in void context at -e line 1. Useless use of a constant (222) in void context at -e line 1. Useless use of a constant (Two) in void context at -e line 1. Useless use of a constant (Three) in void context at -e line 1. BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; ((my(@stuff) = 'One'), '???', '???'); print("(@stuff)"); (my(@stoff) = scalar(('???', '???', 'Drei'))); print("(@stoff)"); (my(@sniff) = scalar(((my(@snarf) = '111'), '???', '333'))); print("sniff: (@sniff) snarf: (@snarf)"); (my(@stiff) = ('Uno', 'Dos', 'Tres')); print("(@stiff)"); -e syntax OK >perl -wMstrict -le "my @stuff = 'One', 'Two', 'Three'; print qq{(@stuff)}; ;; my @stoff = scalar('Eine', 'Zwei', 'Drei'); print qq{(@stoff)}; ;; my @sniff = scalar(my @snarf = '111', '222', '333'); print qq{sniff: (@sniff) snarf: (@snarf)}; ;; my @stiff = ('Uno', 'Dos', 'Tres'); print qq{(@stiff)}; " Useless use of a constant (Eine) in void context at -e line 1. Useless use of a constant (Zwei) in void context at -e line 1. Useless use of a constant (222) in void context at -e line 1. Useless use of a constant (Two) in void context at -e line 1. Useless use of a constant (Three) in void context at -e line 1. (One) (Drei) sniff: (333) snarf: (111) (Uno Dos Tres)


Comment on Re: Newbie: query about array assignment
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (15)
As of 2015-07-28 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (255 votes), past polls