data access for a hash inside a Perl object

Discussion in 'Perl Misc' started by freesoft12@gmail.com, Dec 27, 2008.

  1. Guest

    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
     
    , Dec 27, 2008
    #1
    1. Advertising

  2. <> 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/"
     
    Tad J McClellan, Dec 27, 2008
    #2
    1. Advertising

  3. Guest

    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
     
    , Dec 27, 2008
    #3
  4. Guest

    On Fri, 26 Dec 2008 22:29:17 -0800 (PST), "" <> 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
     
    , Dec 28, 2008
    #4
  5. Guest

    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
     
    , Dec 29, 2008
    #5
  6. <> 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/"
     
    Tad J McClellan, Dec 29, 2008
    #6
  7. [A complimentary Cc of this posting was sent to
    Tad J McClellan
    <>], who wrote in article <>:
    > 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
     
    Ilya Zakharevich, Dec 29, 2008
    #7
  8. Guest

    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
     
    , Dec 29, 2008
    #8
  9. [A complimentary Cc of this posting was sent to

    <>], who wrote in article <>:
    > 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
     
    Ilya Zakharevich, Dec 30, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
  2. Pokkai Dokkai
    Replies:
    8
    Views:
    151
    Phrogz
    Nov 18, 2007
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    676
    David A. Black
    Jul 2, 2008
  4. Shashank Khanvilkar

    Hash of hash in perl

    Shashank Khanvilkar, Nov 17, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    153
    gumby
    Nov 18, 2004
  5. Replies:
    2
    Views:
    142
Loading...

Share This Page