Esteemed Monks,

In using Class::DBI::mysql to get some data from a single table using the code fragment below:

sub en_display { # Display 'full signup or enroll' form my $self = shift; my $errs = shift; my $output =''; my $sess = $self->param('up_session'); if ( $sess->param('_IS_USER') && $sess->param('_IS_REGISTERED') ) +{ my $obj = AppSys::User->search( 'upusername' => $sess->param(' +_IS_USER')); while ( my $usr = $obj->next ) { #$output .= "Username: " . $usr->upusername . "<br><br>"; foreach ( $usr->columns ) { $output .= "<b>$_</b>; $usr->{$_} <br>"; } } } return $output;
I find I get no output printed. This is wierd! So I went back to the documentation and could find nothing. Then I recalled that when I first looked at Class::DBI I tried the code from Class::DBI Intro by trs80. Now in there I read:
# now we will make an object that relates to a single # record. We know that our entry is id 1 since it is # the only record in the database so we do the following our $user = Table::User->retrieve(1); # $user contains our object, but since we haven't called # any methods on the objects it has not made any calls to # the database. print ">> Class::DBI Results (single column)\n"; print " Users First Name: " , $user->first_name , "\n\n"; # Now $user contains all the information related to # the record in the database. You can verify this with # Data::Dumper if you want.
Which is just what I did and, yes, he is right! In my own code at the top of the page if I uncomment the line #$output .= "Username: " . $usr->upusername . "<br><br>"; I get the expected output.

Why do I need this apparently redundant method call to get the data into the object? Is there some way of ensuring that the data is loaded without the additonal call?

jdtoronto


In reply to Class::DBI strangeness... by jdtoronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.