I have a bug, an improvement, and several stylistic notes.
  1. First the bug. You are not getting and setting the element you think you are. Your RE needs to be fixed to do that.
  2. The improvement. Your get/set methods are going to be slow. Usually an AUTOLOAD is best off creating the method and then calling that so that future invocations will run faster. This is worth some increase in the complexity of your method. (Sorry jeffa, they had not cached their methods properly for that speedup.)
  3. From here on it, it is all about style. You aren't using strict. Inside the AUTOLOAD that is often quite reasonable. But you should at least unimport it there so that it will work fine when dropped in a module that does use strict.
  4. As I have mentioned elsewhere, I am not a fan of making constructors callable by prototype. I think it is healthy to distinguish class methods from instance methods. Even when it wouldn't be overly confusing to allow that, I don't see a point in allowing it for the simple reason that the kind of functionality you are likely to want when constructing a new instance from an old is likely to be different some day.
  5. jeffa's point of having an empty DESTROY method is good.
  6. I see no reason to create your AUTOLOAD routine by assigning it to a typeglob. The usual declaration is enough.
  7. When you are doing as much as you are (and more when I get through with it), I see no reason to try to play golf. Break stuff out a little so people can see what is going on.
  8. I don't find myself using get/set methods very much. If you are putting them in classes mechanically, it is worth asking why. While I don't entirely agree with this article, he does have a lot of good things to say. (I disagree with his dismissal of the value of having different views of the same object. I have done that. I think it is a very valuable thing to do. If most programmers have not done that, that is only evidence of the fact that the value of so doing is not better known. But that is a topic for another day.)
So with that here is a rewritten version:
package PKG; use strict; require Carp; sub new {bless {}, shift;} sub DESTROY {} sub AUTOLOAD { no strict; if ($AUTOLOAD=~/(\w+)$/) { my $field = $1; *{$field} = sub { my $self = shift; @_ ? $self->{$field} = shift : $self->{$field}; }; &$field(@_); } else { Carp::confess("Cannot figure out field name from '$AUTOLOAD'"); } }

In reply to Re (tilly) 1: Short version of autoload by tilly
in thread Short version of autoload by simon.proctor

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.