use Tie::OneOff; { my $i; sub int_only() : lvalue { +Tie::OneOff->lvalue({ STORE => sub { $i = ($_[0] =~ m/^\d+$/s) ? $_[0] : die "bad 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';