Perl backticks in hash ref gives different results

I have a file called a.gz, which is a gzipped file that contains the following lines when unpacking:

a
b

Below are two blocks of perl code that I think should “give” the same results, but they don't.

Code No. 1:

use Data::Dumper;
my $s = {
        status => 'ok',
        msg    => `zcat a.gz`
};
print Dumper($s),"\n";

Code No. 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 am using perl 5.10.1 working on Linux

+4
source share
1 answer

perldoc perlop :

( ) undef, . ( $/ $INPUT_RECORD_SEPARATOR) , .

`` ; { ... }, .

{ LIST } , , .. key1, value1, key2, value2, key3, value3, .... , ( undef).

LIST , LIST ( ) .

=> , ,, ( ).

+6
source

All Articles