You can also do this sort of thing with Tie::OneOff. For example:
use Tie::OneOff;
{
my $i;
sub int_only() : lvalue {
+Tie::OneOff->lvalue({
STORE => sub { $i = ($_[0] =~ m/^\d+$/s) ? $_[0] : die "ba
+d int" },
FETCH => sub { $i },
})
};
}
use Test::More 'no_plan';
use Test::Exception;
lives_ok { int_only = 42 } 'can set int_only to be an integer';
is int_only, 42, 'value set okay';
dies_ok { int_only = "a" } 'cannot set it to be a non-int';
is int_only, 42, 'old value preserved';
int_only =~ s/42/24/s;
is int_only, 24, 'substitution works';
dies_ok {int_only =~ s/4/a/s } 'cannot replace number with letter';
-
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.
|