Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > data access for a hash inside a Perl object

Reply
Thread Tools

data access for a hash inside a Perl object

 
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      12-27-2008
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
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      12-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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() ) {


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      12-27-2008
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
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      12-28-2008
On Fri, 26 Dec 2008 22:29:17 -0800 (PST), "(E-Mail Removed)" <(E-Mail Removed)> wrote:

>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

 
Reply With Quote
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      12-29-2008
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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      12-29-2008
(E-Mail Removed) <(E-Mail Removed)> wrote:

> 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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      12-29-2008
[A complimentary Cc of this posting was sent to
Tad J McClellan
<(E-Mail Removed)>], who wrote in article <(E-Mail Removed)>:
> 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
 
Reply With Quote
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      12-29-2008
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
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      12-30-2008
[A complimentary Cc of this posting was sent to
(E-Mail Removed)
<(E-Mail Removed)>], who wrote in article <(E-Mail 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
make a hash object to string object as look like a hash ? Pokkai Dokkai Ruby 8 11-18-2007 05:23 AM
Perl hash of hash efficiency. tak Perl Misc 28 08-07-2006 02:15 PM
Hash of hash in perl Shashank Khanvilkar Perl Misc 3 11-18-2004 08:44 PM



Advertisments