data access for a hash inside a Perl object

F

freesoft12

Hi,

Inside my Perl Object, I use a hash to store all the data.

I want to access the data inside the hash using accessors. If I had an
array, I would provided methods to return the size of the array and
then a method for getting the i'th data i.e.

# $obj is an object of my class
my $size = $obj->get_size_of_array();
for ($i = 0; $i < $size; ++$i) {
my $value = $obj->get_value($i); # i would return array[$i]
}

How can I create accessors for the data inside a hash? Or can I return
a constant reference to the hash (like in C++)?

Regards
John
 
T

Tad J McClellan

Hi,

Inside my Perl Object, I use a hash to store all the data.

I want to access the data inside the hash using accessors. If I had an
array, I would provided methods to return the size of the array and
then a method for getting the i'th data i.e.


Even better, you could provide a method that simply returns
the data without needing to know the size of the data structure
or needing to compute indexes (ie. an "iterator"):

while ( my $value = $obj->iterate_array() ) {

or worse, return a list of all of the values:

foreach my $value ( $obj->get_array_values() ) {

# $obj is an object of my class
my $size = $obj->get_size_of_array();
for ($i = 0; $i < $size; ++$i) {


These 2 lines of code provide the subscript into the array...

my $value = $obj->get_value($i); # i would return array[$i]
}

How can I create accessors for the data inside a hash?


.... by providing subscripts (keys) into the hash:


foreach my $key ( $obj->return_hash_keys() ) {
my $value = $obj->return_hash_value($key);

or, using an iterator

while ( my $value = $obj->iterate_hash_values() ) {
 
F

freesoft12

Hi Ted,

Can you pl provide some more info on how to implement '$obj-
iterate_hash_values()'?

Your suggestion got me to think about creating a separate iterator
class that saves the state of the iteration. However, I am not sure
what/how to save the state between each hash table iteration? For an
array/list, I can save the current index being processed and increment
until the end of the list.

Regards
John
 
S

sln

Hi,

Inside my Perl Object, I use a hash to store all the data.

I want to access the data inside the hash using accessors. If I had an
array, I would provided methods to return the size of the array and
then a method for getting the i'th data i.e.

# $obj is an object of my class

Yes it is, try to print the object: print $obj,"\n";
What does it tell you?
my $size = $obj->get_size_of_array();
for ($i = 0; $i < $size; ++$i) {
my $value = $obj->get_value($i); # i would return array[$i]
}

How can I create accessors for the data inside a hash? Or can I return
a constant reference to the hash (like in C++)?

Regards
John

You can return anything you want. Whats an accessor? Its a million dollar
word only used in beginner object oriented programming books.

"Get/Set" object oriented programming and accessors are an illusion,
even in C++... void ** is real.

As far as OOP in Perl, you can put lipstick on a pig, but its still a pig.

sln
 
F

freesoft12

Hi,

I have found a nice way of providing an iterator access to the data
inside my object using 'closures'. The advantage of an iterator-based
approach is seen with this solution.

---- hash_access_test.pl ----

BEGIN { # add the location to where 'hash_access.pm' is stored
push(@INC,".");
}

use strict; # comment out these two
use diagnostics; # and '-w' switch, once testing is over
use hash_access;

# hash_access is my test class. create an object of that class
my $obj = hash_access->new();

# call a function to populate the hash inside the object
$obj->populate_hash();

# get the iterator to the data inside the hash
my $it = $obj->get_iterator();
while (my ($key,$value) = $it->()) {
print "key = $key value = $value\n";
}
---------- end ---------

--------- hash_access.pm ----------
# this is my class containing a hash to store some data
package hash_access;

sub new {
my $type = shift;
my $self = {};
$self->{data_} = {}; # initialize the internal hash
bless($self,$type);
return $self;
}

sub populate_hash {
my $self = shift;

# populate the hash with some test data
$self->{data_}->{'a'} = 'b';
$self->{data_}->{'b'} = 'c';
$self->{data_}->{'c'} = 'd';
}

sub get_iterator {
my $self = shift;

# using a 'closure' to call the 'each' function for the hash data
return sub {
return each(%{$self->{data_}});
}
}
----------- end --------------

Regards
John
 
T

Tad J McClellan

use diagnostics; # and '-w' switch, once testing is over


That should be:

use diagnostics; # and 'use warnings', once testing is over

See the "What's wrong with -w and $^W" section in

perldoc perllexwarn
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Tad J McClellan
use diagnostics; # and 'use warnings', once testing is over

See the "What's wrong with -w and $^W" section in

perldoc perllexwarn

I prefer -w. IMO, perllexwarn.pod should be corrected:

-w is very useful, the major advantage with using -w on the command
line to enable warnings is that it is all or nothing. Take the
typical scenario when you ... etc...

Hope this helps,
Ilya
 
F

freesoft12

Thanks for your suggestions Ted, Ilya.

This is the first time I've read "perllexwarn.pod". It says that '-w'
switch switches on the warnings in all modules. This might not be
helpful if I am including third-party modules. Since all the modules
in my Perl project are written from scratch (using modules from the
standard Perl installation), I feel that that '-w' switch is the one I
should use.

Regards
John
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
(e-mail address removed)
This is the first time I've read "perllexwarn.pod". It says that '-w'
switch switches on the warnings in all modules. This might not be
helpful if I am including third-party modules.

In my experience, the only situation when -w is not helpful is if both
of the following conditions hold:

a) I trust the 3rd party module writers that their code is correct;
b) I know that the code of these modules is wrong (at least warning-wise).

Given that these restriction are more or less contradictory, I do not
encounter this situation often...

I see a lot of use for `no warnings "foo"'. I see very little use for
`use warnings' (only inside modules to enable warnings "no matter what";
but this may be too offensive when user's interests taken into account...).

Hope this helps,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top