我正在从
perldoc
手册页 'perldoc perlfaq5'
执行以下命令,该命令是关于读取文件(一次将文件读入变量而不是逐行读取)。
我看到以下错误;该示例似乎存在语法错误。我很惊讶他们会在
perldoc
中放置一个无法正确编译的示例。
有什么想法可以让它发挥作用吗?
#! 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)
在 perldoc perlfaq5 中尝试了上面的代码
使用 Perl 运行
出现如上所示的错误。