[英]Dereference perl array of objects after pass to function
I'm weird with dereferencing objects.我对取消引用对象很奇怪。
In my prog oobjtest I want to get a list of objects.在我的 prog oobjtest 中,我想获取对象列表。
Steps: in perl testprog oobjtest 1) Call a class method my @lo_hashref_objects = oclass::getemall();步骤:在 perl testprog oobjtest 1) 调用 class 方法my @lo_hashref_objects = oclass::getemall(); .
. Print them.
打印它们。
Steps: in perl class oclass步骤:在 perl class oclass
The class file class 文件
package oclass;
use warnings;
use strict;
use Carp;
# to make your class data a file-scoped lexical
my $Census = 0;
use feature qw/say switch/;
use lib qw(/home/hj/lib/perl/oo/test);
our $verb_file = '/home/hj/lib/perl/oo/test/data/verb.txt';
#-----------------------------------------------------------
sub new
{
my $class = shift;
my $self = {};
$self->{bez} = undef;
$self->{etym} = undef;
# "private" data
$self->{"_CENSUS"} = \$Census;
bless ($self, $class);
++ ${ $self->{"_CENSUS"} };
return $self;
}
# class method
sub _get_file_content
{
my ($class_name) = @_;
open my $fh, "<:encoding(UTF-8)", $verb_file or die;
my @lol = ();
while (<$fh>) {
chomp;
# gather lines per reference in list lol
push @lol, [ split ' & ' ];
}
close $fh or croak "Couldn't close '$verb_file': $Carp::OS_ERROR";
for my $array_ref ( @lol ) {
print "DEBUG _get_file_content: lol\t @$array_ref , \n";
for (my $i = 0; $i <= $#$array_ref; $i++) {
say "$i: ", $array_ref->[$i];
}
}
return @lol;
} # end _get_file_content
sub getemall
{
my @lol = ();
my @lo_hashref_objects = ();
@lol = oclass::_get_file_content();
for my $array_ref ( @lol ) {
print "getemall: lol\t @$array_ref , \n";
for (my $i = 0; $i <= $#$array_ref; $i++) {
say "$i: ", $array_ref->[$i];
}
}
my $i = 0;
for my $array_ref ( @lol ) {
my $self = oclass->new;
$self->setbez($array_ref->[0]);
$self->setetym($array_ref->[1]);
say $self->getbez;
say $self->getetym;
push @lo_hashref_objects, $self;
print "getemall:CCC", $lo_hashref_objects[$i]->getbez, " \n";
print "getemakk: C1C1C1: ", $lo_hashref_objects[$i]->getetym, " \n";
$i++;
}
return @lo_hashref_objects;
} # end getemall
#-----------------------------------------------------------
sub getbez {
my $self = shift;
return $self->{bez};
}
sub setbez {
my ($self, $bez) = @_;
croak('Usage: $self->setbez($bez)') if @_ < 2;
$self->{bez}= $bez;
return;
}
sub setetym {
my $self = shift;
if (@_) { @{ $self->{etym} } = @_ }
return @{ $self->{etym} };
}
sub getetym {
my $self = shift;
return @{ $self->{etym} };
}
1; # so the require or use succeeds
Testprogram测试程序
#!/usr/bin/perl
#
use warnings;
use strict;
use Carp;
use lib qw(/home/hj/lib/perl/oo/test);
use oclass;
### call the class method "getemall" to gather all the single lines in file (data)
my @lo_hashref_objects = oclass::getemall();
my $i=0;
for my $obj_ref ( @lo_hashref_objects ) {
print " \nafter call of getemall: $obj_ref \n";
print "after call of getemall: $obj_ref->getbez \n";
print "ref obj_ref: ", ref($obj_ref), "\n";
print "after call of getemall: $obj_ref \n";
print "afterlo_hashref_object: $lo_hashref_objects[$i++]->getbez \n";
print "after call of getemall: $obj_ref->getetym \n";
}
Output partially Output 部分
der
Versuch
getemall:CCC der
getemakk: C1C1C1: Versuch
die
Verbindung
getemall:CCC die
getemakk: C1C1C1: Verbindung
after call of getemall: oclass=HASH(0x55f70e320278)
after call of getemall: oclass=HASH(0x55f70e320278)->getbez
ref obj_ref: oclass
after call of getemall: oclass=HASH(0x55f70e320278)
afterlo_hashref_object: oclass=HASH(0x55f70e320278)->getbez
hj@debian:~$
You cannot call methods in a string-interpolatin in that way.您不能以这种方式调用字符串插值法中的方法。
print "after call of getemall: $obj_ref->getbez \n";
will produce会产生
after call of getemall: oclass=HASH(0x55f70e320278)->getbez
One way to solve this is to use解决此问题的一种方法是使用
print "after call of getemall: " . $obj_ref->getbez. " \n";
another way would be to use the "baby-cart"另一种方法是使用“婴儿车”
print "after call of getemall: @{[$obj_ref->getbez]} \n";
Both should call the method correctly and display the data.两者都应该正确调用该方法并显示数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.