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
- call another class method to get filecontent
- content-lines store after split in an array my @lol by reference
- pass this array to function getemall by return @lol. List-data are reachable by $array_ref->[$i]
- Create a new object my $self = oclass->new;. Put data in instance variables by Setter functions.
- Objects work, see Getter function output.
- Store objects in an array. Data of objects available via $self as well as via array addressing, see getemall
- Pass it to testprog oobjtest
- 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:~$
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.lo_hashref_objectssound like objects that you could iterate over with thewhileloop I described. Each hashref references a hash with 0-many key => value pairs.hashref(like$hashref), you dereference and can look at what's in it withwhile(my ($key, $value) = each %$hashref) { print "$key = $value\n"; }. I thought thoseHASHthingies were a problem, that's why I hinted that.