Help for this page

Select Code to Download


  1. or download this
    #!/usr/bin/perl
    use warnings;
    use strict;
    
  2. or download this
    package Constrained;
    use Errno qw/EINVAL/;
    
  3. 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";
    }
    
  4. or download this
    sub TIESCALAR {
        my $class = shift;
        if (defined $_[1]) {
    ...
        }
        bless { code => $_[0], val => $_[1]}, $class;
    }
    
  5. or download this
    sub STORE {
        my ($self, $val) = @_;
        $self->{code}($val) or _invalidate EINVAL;
        $self->{val} = $val;
    }
    
  6. or download this
    sub FETCH {
        defined $_[0]{val} or _invalidate EINVAL;
        $_[0]->{val};
    ...
    }
    
    1;
    
  7. 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 }
    }
    
  8. or download this
    print q(Testing FETCH error for undefined value,), $/; 
    defined( eval { print 'foo is ', foo, $/ }) or print $@, $/;
    
    ...
          }) ?
          ('baz is ', baz, $/, $/) :
          ($@, $/);
    
  9. 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