There are lots of method-generator packages already available to do this kind of work for you. (I'm assuming that you're not stuck on the idea of using "pretend objects" and would be happy with a blessed object instance.)
You could do something like this with Class::MakeMethods easily enough:
package MyObject;
use Class::MakeMethods::Autoload {
'new' => 'Template::Hash::new',
'.*' => 'Template::Hash::scalar -self_closure',
};
package main;
my $object = MyObject->new();
my $new_func = $object->attrib();
$new_func->('value');
The Class::MakeMethods::Autoload statement says that we want to generate a constructor for "new", and for any other method name we want to construct a "scalar -self_closure" method.
The "-self_closure" flag informs Class::MakeMethods that you don't want a simple accessor -- you want to return a closure bound the the target object that will act as an accessor.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.