Why does File :: Slurp return a scalar when it should return a list?

I am new to the File :: Slurp module, and in his first test with it he did not give the expected results. It took me a while to figure this out, so now I'm wondering why I saw this particular behavior.

My call to File :: Slurp looked like this:

my @array = read_file( $file ) || die "Cannot read $file\n";

I included the "die" part because I'm used to doing this when opening files. My @array will always contain the entire contents of the file in the first element of the array. Finally, I pulled out the "|| die" section, and it started working as I expected.

Here is an example to illustrate:

perl -de0

Loading DB routines from perl5db.pl version 1.22
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):   0
DB<1> use File::Slurp

DB<2> $file = '/usr/java6_64/copyright'

DB<3> x @array1 = read_file( $file )
0  'Licensed material - Property of IBM.'
1  'IBM(R) SDK, Java(TM) Technology Edition, Version 6'
2  'IBM(R) Runtime Environment, Java(TM) Technology Edition, Version 6'
3  ''
4  'Copyright Sun Microsystems Inc, 1992, 2008. All rights reserved.'
5  'Copyright IBM Corporation, 1998, 2009. All rights reserved.'
6  ''
7  'The Apache Software License, Version 1.1 and Version 2.0'
8  'Copyright 1999-2007 The Apache Software Foundation. All rights reserved.'
9  ''
10  'Other copyright acknowledgements can be found in the Notices file.'
11  ''
12  'The Java technology is owned and exclusively licensed by Sun Microsystems Inc.'
13  'Java and all Java-based trademarks and logos are trademarks or registered'
14  'trademarks of Sun Microsystems Inc.  in the United States and other countries.'
15  ''
16  'US Govt Users Restricted Rights - Use duplication or disclosure'
17  'restricted by GSA ADP Schedule Contract with IBM Corp.'
DB<4> x @array2 = read_file( $file ) || die "Cannot read $file\n";

0  'Licensed material - Property of IBM.
IBM(R) SDK, Java(TM) Technology Edition, Version 6
IBM(R) Runtime Environment, Java(TM) Technology Edition, Version 6

Copyright Sun Microsystems Inc, 1992, 2008. All rights reserved.
Copyright IBM Corporation, 1998, 2009. All rights reserved.

The Apache Software License, Version 1.1 and Version 2.0
Copyright 1999-2007 The Apache Software Foundation. All rights reserved.

Other copyright acknowledgements can be found in the Notices file.

The Java technology is owned and exclusively licensed by Sun Microsystems Inc.
Java and all Java-based trademarks and logos are trademarks or registered
trademarks of Sun Microsystems Inc.  in the United States and other countries.

US Govt Users Restricted Rights - Use duplication or disclosure
restricted by GSA ADP Schedule Contract with IBM Corp.
'

|| ? , Perl, File:: Slurp. File:: Slurp, , , , , - File:: Slurp . , .

+3
3

|| () :

C-style Logical

|| . , , . , .

:

#! /usr/bin/perl

sub lhs {
  my $ctx = wantarray;
  if (defined $ctx) {
    if ($ctx) { "list" }
    else      { "scalar" }
  }
  else { "void" }
}

print lhs || 0, "\n";

:

$ ./or-ctx.pl
scalar

, :

my @array = eval { read_file("/etc/issue") };
die unless @array;

, ( $@ eval), read_file die, - .

+6

|| , , .

my @array = read_file( $file ) || die "Cannot read $file\n";

: , "return" die @array. , , .

- (or), :

my @array = read_file( $file ) or die "Cannot read $file\n";

@array , , . , , , .

"" die @array, , @array , .

, File::Slurp read_file :

( $/ , , ''). . [ ]

:

    my $text = read_file( 'filename' ) ;
    my @lines = read_file( 'filename' ) ;

. , @lines .

+9

. -, , or and . , ,

 my @array = read_file( $file ) || die "Cannot read $file\n";

. :: Slurp:: read_file croak :

err_mode

You can use this parameter to control the behavior of the read_file when an error occurs. This parameter defaults to "croak". You can set it to "carp" or "quiet" so as not to handle errors.

+4
source

Source: https://habr.com/ru/post/1748157/


All Articles