- or download this
#!/usr/bin/perl
use warnings;
use strict;
- or download this
package Constrained;
use Errno qw/EINVAL/;
- or download this
sub _invalidate {
$! = shift;
die sprintf('Constraint violation: %s by %s::%s in %s line %s.',
...
map { qq($_) } (caller 1)[0,3,1,2] ),
"\n";
}
- or download this
sub TIESCALAR {
my $class = shift;
if (defined $_[1]) {
...
}
bless { code => $_[0], val => $_[1]}, $class;
}
- or download this
sub STORE {
my ($self, $val) = @_;
$self->{code}($val) or _invalidate EINVAL;
$self->{val} = $val;
}
- or download this
sub FETCH {
defined $_[0]{val} or _invalidate EINVAL;
$_[0]->{val};
...
}
1;
- or download this
package main;
my ($v, $c) = (qr/[aeiouy]/i, qr/[bcdfghjklmnpqrstvwxyz]/i);
...
tie my $foo, 'Constrained', sub {shift=~/^$c$v$v$c$c$/};
sub foo () :lvalue { $foo }
}
- or download this
print q(Testing FETCH error for undefined value,), $/;
defined( eval { print 'foo is ', foo, $/ }) or print $@, $/;
...
}) ?
('baz is ', baz, $/, $/) :
($@, $/);
- or download this
__END__
Testing FETCH error for undefined value,
Constraint violation: Invalid argument by main::Constrained::FETCH in
+lvval.pl line 50.
...
Testing TIESCALAR error valid value, 'suits',
baz is suits