Hi Monks,
I'm writing a module that gets all of its information via calls to a database. Is there any point in using AUTOLOAD? I can understand using AUTOLOAD if I'm setting default values for all of the attributes in my constructor, but I'm not doing this, since I don't know what my default values will be until I talk to the database.

Most of my accessor methods (for instance) will look something like this:
sub first_name { my ($self, $id) = @_; unless (defined $self->{'first_name'}{$id} $self->_get_info_from_database; } return $self->{'first_name'}{$id}; }
$self->_get_info_from_database, here, would be a method that reads in a particular table and stores information about the object in $self -- like $self->{'first_name'}{$id}, $self->{'last_name'}{$id}, and so on. That way, the database call only has to be made once per invocation of the program.

I'd really like to be able to use AUTOLOAD or something like it, since the application I'm writing is fairly large, and writing all those get and set methods is a big pain. Is there any way of making AUTOLOAD recognize that when I want to set first_name, for instance, what I really want to do is store a value in the database, or even call another method that I've written to accomplish this? If not, it doesn't seem like AUTOLOAD would be terribly useful for my purposes.

Any ideas, brainstorms, or pointers to CPAN modules appreciated.

In reply to Point of AUTOLOAD in a database environment? by janjan

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.