Perl IO::File 可以与 Autodie 一起使用吗?
Perl Does IO::File work with Autodie?
我想了解为什么 IO::File
似乎不适用于 use autodie
:
示例 #1:测试程序使用 open
:
#! /usr/bin/env perl
#
use strict;
use warnings;
use feature qw(say);
use autodie;
use IO::File;
open( my $fh, "<", "bogus_file" );
# my $fh = IO::File->new( "bogus_file", "r" );
while ( my $line = $fh->getline ) {
chomp $line;
say qq(Line = "$line");
}
这失败了:
Can't open 'bogus_file' for reading: 'No such file or directory' at ./test.pl line 9
看起来 autodie
正常。
示例 #2:相同的测试程序,但现在使用 IO::File
:
#! /usr/bin/env perl
#
use strict;
use warnings;
use feature qw(say);
use autodie;
use IO::File;
# open( my $fh, "<", "bogus_file" );
my $fh = IO::File->new( "bogus_file", "r" );
while ( my $line = $fh->getline ) {
chomp $line;
say qq(Line = "$line");
}
这失败了:
Can't call method "getline" on an undefined value at ./test.pl line 11.
貌似autodie
没抓到坏IO::File->new
开
然而,据我所知,IO::File->new
在下面使用了 open
。这是来自 IO::File
:
的代码
sub new {
my $type = shift;
my $class = ref($type) || $type || "IO::File";
@_ >= 0 && @_ <= 3
or croak "usage: $class->new([FILENAME [,MODE [,PERMS]]])";
my $fh = $class->SUPER::new();
if (@_) {
$fh->open(@_) # <-- Calls "open" method to open file.
or return undef;
}
$fh;
}
sub open {
@_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
my ($fh, $file) = @_;
if (@_ > 2) {
my ($mode, $perms) = @_[2, 3];
if ($mode =~ /^\d+$/) {
defined $perms or $perms = 0666;
return sysopen($fh, $file, $mode, $perms);
} elsif ($mode =~ /:/) {
return open($fh, $mode, $file) if @_ == 3;
croak 'usage: $fh->open(FILENAME, IOLAYERS)';
} else {
# <--- Just a standard "open" statement...
return open($fh, IO::Handle::_open_mode_string($mode), $file);
}
}
open($fh, $file);
}
是什么导致 autodie
无法按预期工作?
autodie
是词法范围的。因此,它会在您的文件中更改(包装)对 open
的调用,但不会在 IO::File
.
内更改
我想了解为什么 IO::File
似乎不适用于 use autodie
:
示例 #1:测试程序使用 open
:
#! /usr/bin/env perl
#
use strict;
use warnings;
use feature qw(say);
use autodie;
use IO::File;
open( my $fh, "<", "bogus_file" );
# my $fh = IO::File->new( "bogus_file", "r" );
while ( my $line = $fh->getline ) {
chomp $line;
say qq(Line = "$line");
}
这失败了:
Can't open 'bogus_file' for reading: 'No such file or directory' at ./test.pl line 9
看起来 autodie
正常。
示例 #2:相同的测试程序,但现在使用 IO::File
:
#! /usr/bin/env perl
#
use strict;
use warnings;
use feature qw(say);
use autodie;
use IO::File;
# open( my $fh, "<", "bogus_file" );
my $fh = IO::File->new( "bogus_file", "r" );
while ( my $line = $fh->getline ) {
chomp $line;
say qq(Line = "$line");
}
这失败了:
Can't call method "getline" on an undefined value at ./test.pl line 11.
貌似autodie
没抓到坏IO::File->new
开
然而,据我所知,IO::File->new
在下面使用了 open
。这是来自 IO::File
:
sub new {
my $type = shift;
my $class = ref($type) || $type || "IO::File";
@_ >= 0 && @_ <= 3
or croak "usage: $class->new([FILENAME [,MODE [,PERMS]]])";
my $fh = $class->SUPER::new();
if (@_) {
$fh->open(@_) # <-- Calls "open" method to open file.
or return undef;
}
$fh;
}
sub open {
@_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
my ($fh, $file) = @_;
if (@_ > 2) {
my ($mode, $perms) = @_[2, 3];
if ($mode =~ /^\d+$/) {
defined $perms or $perms = 0666;
return sysopen($fh, $file, $mode, $perms);
} elsif ($mode =~ /:/) {
return open($fh, $mode, $file) if @_ == 3;
croak 'usage: $fh->open(FILENAME, IOLAYERS)';
} else {
# <--- Just a standard "open" statement...
return open($fh, IO::Handle::_open_mode_string($mode), $file);
}
}
open($fh, $file);
}
是什么导致 autodie
无法按预期工作?
autodie
是词法范围的。因此,它会在您的文件中更改(包装)对 open
的调用,但不会在 IO::File
.