I have a file called a.gz which is a gzipped file which contains the following lines when unzipped:
a
b
Below are two blocks of perl code which I think "should" give the same results but they don't.
Code #1:
use Data::Dumper;
my $s = {
status => 'ok',
msg => `zcat a.gz`
};
print Dumper($s),"\n";
Code #2:
use Data::Dumper;
my $content = `zcat a.gz`;
my $s = {
status => 'ok',
msg => $content
};
print Dumper($s), "\n";
Code #1 gives the following result:
Odd number of elements in anonymous hash at ./x.pl line 8.
$VAR1 = {
'msg' => 'a
',
'b
' => undef,
'status' => 'ok'
};
Code #2 returns the following result:
$VAR1 = {
'msg' => 'a
b
',
'status' => 'ok'
};
I'm using perl 5.10.1 running in Linux
In scalar context, it comes back as a single (potentially multi-line) string, or
undef
if the command failed. In list context, returns a list of lines (however you've defined lines with$/
or$INPUT_RECORD_SEPARATOR
), or an empty list if the command failed.
Assigning to a scalar puts ``
in scalar context; using it in { ... }
puts it in list context.
{ LIST }
takes a list and interprets its contents alternating between keys and values, i.e. key1, value1, key2, value2, key3, value3, ...
. If the number of elements is odd, you get a warning (and the missing value is taken to be undef
).
LIST , LIST
(the comma operator in list context) concatenates two lists.
=>
works just like ,
but automatically quotes the identifier to its left (if there is one).