我在MPEG::Audio::Frame中调试a test。如果我运行这个测试,我会得到:
$ cpan -g MPEG::Audio::Frame
$ tar zxvf MPEG-Audio-Frame-0.09.tar.gz
$ cd MPEG-Audio-Frame-0.09
$ perl Makefile.PL
$ make
$ perl -I./blib/lib t/04-tie.t
1..5
ok 1 - use MPEG::Audio::Frame;
ok 2 - 'tie' isa 'MPEG::Audio::Frame'
Not a HASH reference at blib/lib/MPEG/Audio/Frame.pm line 273, <DATA> line 1.
# Looks like your test exited with 255 just after 2.我将问题缩小到以下最小示例:
package My::Module;
use feature qw(say);
use strict;
use warnings;
use overload '""' => \&asbin;
sub asbin {
my $self = shift;
$self->{binhead} # $self is not yet a hash, so execution stops here.
}
sub TIEHANDLE {
bless \$_[1], $_[0]
}
sub READLINE {}
sub read {
say "reading..";
my $pkg = shift;
my $fh = shift || 0; # Why is the stringification operator called here?
}
package main;
use feature qw(say);
use strict;
use warnings;
tie *FH, 'My::Module', *DATA;
My::Module->read(\*DATA);
<FH>;
__DATA__
abc为什么为语句My::Module->read(\*DATA)调用字符串化运算符?
发布于 2019-09-11 22:23:01
shift || 0希望将shift中的参数强制转换为标量。没有为My::Module定义boolify或numify函数重载,因此Perl将使用您的stringify函数。
为了避免在标量上下文中计算对象,您可以将其重新表述为
my $fh = @_ ? shift : 0;
$fh = shift;
$fh = 0 unless ref($fh) || $fh;或者定义一个bool函数重载。
https://stackoverflow.com/questions/57890664
复制相似问题