1

I'm weird with dereferencing objects.

In my prog oobjtest I want to get a list of objects.

Steps: in perl testprog oobjtest 1) Call a class method my @lo_hashref_objects = oclass::getemall();. Print them.

Steps: in perl class oclass

  1. call another class method to get filecontent
  2. content-lines store after split in an array my @lol by reference
  3. pass this array to function getemall by return @lol. List-data are reachable by $array_ref->[$i]
  4. Create a new object my $self = oclass->new;. Put data in instance variables by Setter functions.
  5. Objects work, see Getter function output.
  6. Store objects in an array. Data of objects available via $self as well as via array addressing, see getemall
  7. Pass it to testprog oobjtest
  8. Getting data out of list my @lol fails. Lack of syntax knowledge, but I cannot solve it.

The class file

    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

     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:~$ 
7
  • In the places where you see HASH(0x....) you can iterate over the key => value pairs. while(my ($key, $value) = each %the_hash) { ...} If the hash is a hash ref, you need to dereference it first. Commented Dec 11, 2020 at 17:12
  • at Ted: I have to use by method calls (getbez etc). In line : >>>"getemall:CCC", $lo_hashref_objects[$i]->getbez<<< it is possible. In my eyes the same case, but... Commented Dec 11, 2020 at 18:36
  • Those lo_hashref_objects sound like objects that you could iterate over with the while loop I described. Each hashref references a hash with 0-many key => value pairs. Commented Dec 11, 2020 at 18:38
  • at Ted: your loop access the hash data directly? If so, this is no answer imho. Yes, they are blessed objects as you can see in new() Commented Dec 11, 2020 at 18:42
  • I've not written an answer since I'm not quite sure I understand the question. I'm just trying to point out things you seem to have missed. If you have one hashref (like $hashref), you dereference and can look at what's in it with while(my ($key, $value) = each %$hashref) { print "$key = $value\n"; }. I thought those HASH thingies were a problem, that's why I hinted that. Commented Dec 11, 2020 at 18:46

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.