Whenever your setter needs to be normalized to a different value lvalue accessors don't cut it.
Well...that certainly used to be the case. Indeed, the fact that you have to employ the complexities of tied proxy variables to retain control when using lvalue accessors, is precisely why I cautioned against lvalue accessors in "Perl Best Practices".
But last week I released the latest version of the Contextual::Return module, with which you can now hide all the complexity of tied proxies behind two simple LVALUE, and RVALUE blocks. For example:
use Contextual::Return;
# Name method offers unconstrained access...
sub name : lvalue {
my ($self) = @_;
$name_of{ident $self};
}
# Rank method constrains access: can assign only valid ranks
# (value being assigned is passed as $_ to LVALUE block)...
sub rank : lvalue {
my ($self) = @_;
LVALUE {
croak "Invalid rank ($_) assigned" if !$is_valid_rank{$_};
$rank_of{ident $self} = $_;
}
RVALUE {
$rank_of{ident $self};
}
}
# Serial method offers read-only access...
sub serial {
my ($self) = @_;
return $snum_of{ident $self};
}
With this functionality now available, I'm seriously reconsidering my long-held objections to lvalue accessors.
Damian
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|