Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no list context in </p> <pre><code>$c = ($a, $b); </code></pre> <p>Instead, what you are seeing is the <a href="http://perldoc.perl.org/perlop.html#Comma-Operator" rel="nofollow">comma operator</a> in action:</p> <blockquote> <p>Binary "," is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C's comma operator.</p> </blockquote> <p>To see this more clearly, take a look at:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $x = {a =&gt; 1}; my $y = {a =&gt; 2, b =&gt; 0}; my $z = ($x, $y); print "\$x = $x\t\$y = $y\t\$z = $z\n"; my @z = ($x, $y); print "@z\n"; </code></pre> <p>First, I used <a href="http://perldoc.perl.org/warnings.html" rel="nofollow">warnings</a>. Therefore, when I run this script, I get the warning:</p> <blockquote> <p><code>Useless use of private variable in void context at C:\Temp\t.pl line 7.</code></p> </blockquote> <p><strong>Always</strong> enable warnings.</p> <p>Now, the output shows what's happening:</p> <pre>$x = HASH(0x39cbc) $y = HASH(0x39dac) $z = HASH(0x39dac) HASH(0x39cbc) HASH(0x39dac)</pre> <p>Clearly, <code>$z</code> refers to the same anonymous hash as does <code>$y</code>. No copying of values was done.</p> <p>And, <code>$z[0]</code> refers to the same anonymous hash as does <code>$x</code> and <code>$z[1]</code> refers to the same anonymous has as do <code>$y</code> and <code>$z</code>.</p> <p>Note that parentheses alone do not create list context. In the case of </p> <pre><code>my @z = ($x, $y); </code></pre> <p>they are necessary because <code>=</code> binds more tightly than the comma operator.</p> <pre><code>my @z = $x, $y; </code></pre> <p>would assign <code>$x</code> to <code>$z[0]</code> and discard <code>$y</code> (and emit a warning) whereas </p> <pre><code>my @z = 1 .. 5; </code></pre> <p>would work as expected.</p> <p>Finally, if you wanted to assign to <code>$z</code> a new anonymous hash which contains copies of the anonymous hashes to which both <code>$x</code> and <code>$y</code> point, you would do</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $x = {a =&gt; 1}; my $y = {a =&gt; 2, b =&gt; 0}; my $z = { %$x, %$y }; print Dumper $z; </code></pre> <p>which would output:</p> <pre>$VAR1 = { 'a' => 2, 'b' => 0 };</pre> <p>because hash keys are, by definition, unique. If you want to preserve all values associated with the keys of both hashes, you need to do something slightly more complicated (and use anonymous arrayrefs as values in the "union" hash):</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $x = {a =&gt; 1}; my $y = {a =&gt; 2, b =&gt; 0}; my $z; push @{ $z-&gt;{$_} }, $x-&gt;{$_} for keys %$x; push @{ $z-&gt;{$_} }, $y-&gt;{$_} for keys %$y; print Dumper $z; </code></pre> <p>Output:</p> <pre>VAR1 = { 'a' => [ 1, 2 ], 'b' => [ 0 ] };</pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload