简体   繁体   English

传递给 function 后取消引用 perl 对象数组

[英]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

  1. call another class method to get filecontent调用另一个 class 方法来获取文件内容
  2. content-lines store after split in an array my @lol by reference内容行通过引用在我的@lol数组中拆分后存储
  3. pass this array to function getemall by return @lol .通过return @lol 将此数组传递给 function getemall List-data are reachable by $array_ref->[$i]列表数据可通过$array_ref->[$i]访问
  4. Create a new object my $self = oclass->new;创建一个新的 object my $self = oclass->new; . . Put data in instance variables by Setter functions.通过 Setter 函数将数据放入实例变量中。
  5. Objects work, see Getter function output.对象工作,请参阅 Getter function output。
  6. Store objects in an array.将对象存储在数组中。 Data of objects available via $self as well as via array addressing, see getemall可通过 $self 和数组寻址获得的对象数据,请参阅getemall
  7. Pass it to testprog oobjtest将其传递给 testprog oobjtest
  8. Getting data out of list my @lol fails.我的@lol列表中获取数据失败。 Lack of syntax knowledge, but I cannot solve it.缺乏语法知识,但我无法解决。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM