Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: To Validate Data In Lvalue Subs

by adrianh (Chancellor)
on Sep 13, 2003 at 23:02 UTC ( #291308=note: print w/replies, xml ) Need Help??


in reply to To Validate Data In Lvalue Subs

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';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://291308]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2019-11-12 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (64 votes). Check out past polls.

    Notices?