There's a good description of something like this in the_damian's book. The key is that when a method call falls into AUTOLOAD, the $AUTOLOAD variable is set to the name of the method that was called. You then basically do something like:
sub AUTOLOAD { my $self=shift; my $attr=$AUTOLOAD; unless (exists $allowed_attrs{$attr}) die "Hey, that attribute does +not exist!\n"; # %allowed_attrs lists the attributes you are allowed to access unless (exists $self->{$attr}) do_get_the_value_from_db(); # do_... +would get the value and store it in $self->{$attr}; return $self->{$attr}; }
The above code (which is untested, so be warned) implements read accessor type functions. Possibilities explored in the book mentioned include expanding it to get/set accessors and "caching" the function by playing symbol table games, so you only have to go once through the expensive AUTOLOAD mechanism.

CU
Robartes-

Update: To change this to a set type accessor, just change do_get_... etc. to a call to your function that sets the value in the DB (the value is passed in as an argument). You can also define accessor names like set_value and filter out the set_ with a regexp in the AUTOLOAD method.


In reply to Re: Point of AUTOLOAD in a database environment? by robartes
in thread 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.