Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slurping a file ... syntax error - example from perldoc

Tags:

perl

perldoc

I am executing the following from the perldoc man page 'perldoc perlfaq5' which is about slurping a file (reading a file into a variable all at once instead of line by line).

I am seeing the following error; it appears the example has syntax errors. I am surprised they would put an example in the perldoc which doesn't compile properly.

Any ideas how I can get this working?

#! perl

##### perldoc perlfaq5

use strict;
use warnings;

my $file="testfiledata";

open my $in,  '<',  $file      or die "Can't read old file: $!"
open my $out, '>', "$file.new" or die "Can't write new file: $!";

my $content = do { local $/; <$in> }; # slurp!

    # do your magic here

print $out $content;


#:~$ perl test.pl
syntax error at test.pl line 11, near "open "
Can't redeclare "my" in "my" at test.pl line 13, near "my"
Global symbol "$in" requires explicit package name (did you forget to declare "my $in"?) at test.pl line 13.
Execution of test.pl aborted due to compilation errors.


#:~$ perl -v

This is perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi
(with 59 registered patches, see perl -V for more detail)

Tried code as above in the perldoc perlfaq5

Running with perl

Gives error shown above.

like image 880
Eokun Avatar asked Oct 15 '25 00:10

Eokun


1 Answers

I am surprised they would put an example in the perldoc which doesn't compile properly.

People make mistakes.

You identified a bug in the code in the perlfaq5 documentation.

The code is missing a semicolon at the end of the line. The code should be:

open my $in,  '<',  $file      or die "Can't read old file: $!";

This bug was just reported via by user @ikegami:

https://github.com/Perl/perl5/issues/22098

like image 123
toolic Avatar answered Oct 17 '25 16:10

toolic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!