Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

To Validate Data In Lvalue Subs

by Zaxo (Archbishop)
on Sep 03, 2003 at 00:11 UTC ( #288461=perlmeditation: print w/replies, xml ) Need Help??

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
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://288461]
Approved by herveus
Front-paged by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2019-12-14 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?