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:
$obj->Name = "My Name"; print $o->Name, "\n"; # "My Name, post processed".
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:
package magic; sub TIESCALAR { my $class = shift; die "Incorrect number of parameters" if @_ % 2; my $self = bless {}, $class; $self->_Init(@_); return $self; } sub _Init { my ($self, %opt) = @_; $self->{PRE} = $opt{PRE} || sub { return uc $_[0] }; $self->{POST} = $opt{POST} || sub { my $val = shift; $val =~ s/FOO/BAR/; return $val; }; return; } sub STORE { my ($self, $value) = @_; $value = $self->{PRE}->( $value ); $value = $self->{POST}->( $value ); $self->{VAL} = $value; } sub FETCH { return $_[0]->{VAL} } ######################################## package cool; sub new { my $class = shift; tie my $name, "magic", @_; bless \$name, $class; } sub name :lvalue { ${$_[0]}; } ######################################## package main; tie my $string, "magic"; $string = 'My left foot'; print "$string\n"; my $obj = cool->new( 'POST' => sub { return reverse $_[0] } ); $obj->name = "My right foot"; print $obj->name, "\n";
So - is there a better way of doing this? Is it hard to extend Attribute::Property to do what perldeveloper wanted? If this is the way to do it, how can it be wrapped up in a module like Attribute::Property to make it easier to use?

Cheers - L~R

Update: Modified code to accept a code ref for pre/post hook instead of hardcoded


In reply to Extending LValuable Subs with Tied Variables by Limbic~Region

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.