Looping through Class::Accessor accessors

Discussion in 'Perl Misc' started by Derek Basch, Jun 21, 2006.

  1. Derek Basch

    Derek Basch Guest

    Hi Everyone,

    I began using Class::DBI and love it. However, there are situations
    where I want to loop through the Class::Accessor accessors for an
    object and also get the name of the accessor as I loop through.

    For instance here is some psuedo-perl that I wrote to visualize what I
    need:

    # returns an array of objects
    my @gpsaccounts = Database::Members::Gpsaccounts->search($user_id);

    # loop through the array of objects
    foreach my @row_class (@gpsaccounts) {
    # loop through the objects accessors
    foreach $class_accessor (@row_class) {
    # print the accessors name and value
    print "$class_accessor_name: $class_accessor_value\n"
    }
    }

    Yes, I know that the above is syntactically unpossible but I don't know
    how else to express it.

    Here's how you would normally do it:

    my @gpsaccounts =
    Database::Members::Gpsaccounts->search_gpsaccounts($user_id);

    foreach(@gpsaccounts) {
    print $_->fullname . "\n";
    print $_->user . "\n";
    print $_->email . "\n";
    print "------------------------" . "\n";
    }

    Any ideas? I am a newer Perl programmer so any help is appreciated.

    Thanks,
    Derek Basch
     
    Derek Basch, Jun 21, 2006
    #1
    1. Advertising

  2. Derek Basch

    Derek Basch Guest

    wrote:
    > Derek Basch wrote:
    > > Hi Everyone,
    > >
    > > I began using Class::DBI and love it. However, there are situations
    > > where I want to loop through the Class::Accessor accessors for an
    > > object and also get the name of the accessor as I loop through.


    Thanks for all the help Steven.

    However, the solution suddenly occured to me last night. I was
    declaring my accessors with the 'columns' function of the Class::DBI
    module. Therefore, there must be a hash or something somewhere to loop
    over. I found that Class::DBI objects have various column retrieval
    functions. I used the 'all_columns' function that returns the column
    names. Works great :).


    sub test {
    my @gpsaccounts = Members::Gpsaccounts->search($user_id);
    foreach my $row (@gpsaccounts) {
    foreach my $column ($row->all_columns()) {
    print $column . "\n";
    }
    }

    Derek Basch
     
    Derek Basch, Jun 21, 2006
    #2
    1. Advertising

  3. Derek Basch

    Derek Basch Guest

    > However, the solution suddenly occured to me last night. I was
    > declaring my accessors with the 'columns' function of the Class::DBI
    > module. Therefore, there must be a hash or something somewhere to loop
    > over. I found that Class::DBI objects have various column retrieval
    > functions. I used the 'all_columns' function that returns the column
    > names. Works great :).
    >
    >
    > sub test {
    > my @gpsaccounts = Members::Gpsaccounts->search($user_id);
    > foreach my $row (@gpsaccounts) {
    > foreach my $column ($row->all_columns()) {
    > print $column . "\n";
    > }
    > }
    >
    > Derek Basch



    Here's an improved version:

    sub test {
    my @gpsaccounts = Members::Gpsaccounts->search($user_id);
    foreach my $row (@gpsaccounts) {
    foreach my $column ($row->columns()) {
    print $row->$column . "\n";
    }
    }
     
    Derek Basch, Jun 21, 2006
    #3
    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. Joe Van Dyk

    template accessors for class?

    Joe Van Dyk, Apr 5, 2006, in forum: C++
    Replies:
    5
    Views:
    334
    Victor Bazarov
    Apr 6, 2006
  2. John Wilger
    Replies:
    3
    Views:
    177
    Dave Thomas
    Nov 13, 2004
  3. Diego Virasoro

    newbie: class attribute accessors?

    Diego Virasoro, Oct 23, 2006, in forum: Ruby
    Replies:
    5
    Views:
    150
    Eero Saynatkari
    Oct 23, 2006
  4. Aaron
    Replies:
    2
    Views:
    529
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    279
Loading...

Share This Page