I am using the "magic tied object" approach with WARC record headers in a library I am building. I wanted the $fields object to look like a native data structure, complete with (limited to valid data) reference autovivification. Thanks to overload, stuff like push @{$record->fields->{WARC_Concurrent_To}}, $new_record->id; actually works, no matter how many concurrent records $record initially has, creating the WARC-Concurrent-To header if necessary. (The ->fields method returns an object that overloads hash dereference to return a tied hash upon demand and that tied hash's FETCH method returns another magic object that is always a tied array, but overloads string conversion to do an implicit ->[0] if there is only one value for that header. There is also a tied array interface to the fields object if you care about the order in which the fields appear.)

I am also generally using accessor methods that perform "get" normally and "set" if given a new value where that makes sense; many objects are effectively read-only since they represent data in an archive file. I have no idea how old this pattern is; I have seen it both in other Perl code and in C++. I am not using Moose or anything like it, only baseline Perl 5 "blessed reference" objects.

The class that provides that fields object ended up being about 1/3 simple API, internals, and POD, and about 2/3 implementation for all the magic it does to make those "easy" interfaces work. It also has the most complex and longest test script (by about a factor of 3) so far. It was the first piece I wrote... and I am not making any more classes that magical in that library.


In reply to Re: Language design: direct attribute access and postponed mutators (Perl Vs Python) by jcb
in thread Language design: direct attribute access and postponed mutators (Perl Vs Python) by LanX

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.