在我的 Perl (v5.30.0) 脚本中,我有世界上最简单的对象:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
package Thingee;
# Constructor with name new
sub new
{
my $type = shift;
my %params = @_;
my $self = {};
$self->{'somedata'} = $params{'somedata'};
bless $self, $type;
}
sub printMe
{
my ($self) = @_;
printf "Data: \"%s\"\n", $self->{'somedata'}; # LINE 19
}
还有什么可以更简单?现在,在脚本的“主体”中,我创建了一个 Thingee
对象列表。我发现的是,新创建的 Thingee
似乎在创建时受到了祝福……但如果我将列表传递给子例程,则相同的对象将被视为未受到祝福。这是代码:
package main;
sub foo
{
print "foo() ==========================================\n";
my @ObjectArr = @_;
print Dumper(@ObjectArr);
foreach my $obj (@ObjectArr)
{
$obj->printMe(); # LINE 33
}
}
# I make a list of objects:
my @ObjectArr = ();
push @ObjectArr, Thingee->new( 'somedata' => "My dog has fleas" );
push @ObjectArr, Thingee->new( 'somedata' => "My cat is fine" );
foreach my $obj (@ObjectArr)
{
$obj->printMe();
}
foo(\@ObjectArr);
输出是:
Data: "My dog has fleas"
Data: "My cat is fine"
foo() ==========================================
$VAR1 = [
bless( {
'somedata' => 'My dog has fleas'
}, 'Thingee' ),
bless( {
'somedata' => 'My cat is fine'
}, 'Thingee' )
];
Can't call method "printMe" on unblessed reference at ./PassHash6.perl line 33.
令人沮丧。在代码的“主要”部分,我可以遍历 foreach
循环,并且可以访问新创建的 Thingee
对象。但是在子例程 foo()
的范围内,完全相同的 foreach
循环会抛出 Can't call method "printMe"on unblessed reference
错误.哈姆夫!
我最喜欢的关于为什么需要祝福的解释 comes from this SO post :
An unblessed reference is one where one variable is not a legal reference to an object[.]
好的,这是有道理的。但是,以下所有内容怎么可能是真的:
- 我的
Thingee
在“主要”代码的范围内受到祝福 - 我的
Thingee
不 在foo()
范围内受到祝福
- 在
foo()
中,我的Thingee
在Data:Dumper()
的眼中仍然可见
为了好玩,我将 foo()
中的 foreach()
block 修改为:
foreach my $obj (@ObjectArr)
{
bless $obj, "Thingee"; # New command here
$obj->printMe(); # LINE 34
}
但是现在脚本抛出这个错误:
<EVERYTHING FROM BEFORE...>
Not a HASH reference at ./PassHash6.perl line 19.
呃。第 19 行来自 package Thingee
部分:
sub printMe
{
my ($self) = @_;
printf "Data: \"%s\"\n", $self->{'somedata'}; # LINE 19
}
有人看到我做错了什么吗?
最佳答案
您正在将对数组的引用传递给 foo
foo(\@ObjectArr);
在 foo
中,你可以
my @ObjectArr = @_;
@ObjectArr
包含一个元素,引用传递给 foo
。这不是您想要的。
选项 1:foo
接受对数组的引用。
sub foo {
my $objs = shift;
$_->printMe() for @$objs;
}
foo( \@objs );
选项 2:foo
接受对象。
sub foo {
$_->printMe() for @_;
}
foo( @objs );
关于perl - 如何祝福传递给子程序的列表中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74549975/