Keep It Simple, Stupid | |
PerlMonks |
Extending LValuable Subs with Tied Variablesby Limbic~Region (Chancellor) |
on Jun 24, 2004 at 13:36 UTC ( [id://369354]=perlquestion: print w/replies, xml ) | Need Help?? |
Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
All,
Today, perldeveloper (a new monk) asked in the CB if it was possible to make class method that can both set and get a value, but with post and pre-processing like C#. The following example was given: I remembered Zaxo's post on validating data in lvaluable subs and that one of the replies mentioned it duplicating the work of Attribute::Property. After reading the POD, I realized that it doesn't quite fit the bill: Your object must be a blessed hash reference. In short: $foo->bar = 14 and $foo->bar(14) assign 14 to $foo->{bar} after positive validation. It limits the object type to a hash reference (which is most common), but then doesn't actually provide hooks for pre/post "processing". I came up with the following:
Cheers - L~R Update: Modified code to accept a code ref for pre/post hook instead of hardcoded
Back to
Seekers of Perl Wisdom
|
|