jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

I am using Class::DBI and HTML::Template with CGI::Application. To speed the process of providing HTML::Template with the hashes it requires I recently added the following to my subclass:

package AppSys::DBI; use base 'Class::DBI::mysql'; sub hashy { my $self = shift; return { map { my $g = $self->get($_); $g = hashy($g) if ref $g; ( $_ => $g ); } $self->columns }; }
Code originally from PodMaster in Re: Getting HASHES out of Class::DBI. To return HASHREF's which I can pass to HTML::Template. This is fine if the method is called on a Class::DBI object. But what if I have an iterator? OK, I can 'wrap' hashy in a WHILE loop and push the returned HASHREF's onto a list and voila I can pass it to a <TMPL_LOOP..>.

My question is how do I go about determining which Class I am being called from? Am I a Class::DBI object and thus a single record being returned, or am I a Class::DBI::Iterator and thus I can iterate over myself and return a ref to a list of hashes.

jdtoronto

Replies are listed 'Best First'.
Re: Help with extending Class::DBI please.
by perrin (Chancellor) on Feb 03, 2004 at 05:47 UTC
    Class::DBI::Iterator is an entirely separate class, and you are not subclassing it. There should never be a case where you are unsure if you have an iterator or a single object of your class, and you will never be able to call "hashy" on an iterator. Using a while loop, as you suggested, will work fine for extracting data from an iterator or an array of Class::DBI objects.
      I was thinking about just this after I wrote the question - please forgive me. all this OO stuff is very new to me. If I call Class::DBI in scalar context it returns either an object or an iterator. But if it is called in list context it will return an array of objects, which I can loop over.

      That's where I was getting confused, thanks again perrin