szabgab has asked for the wisdom of the Perl Monks concerning the following question:
I don't want to reinvent the wheel and I guess someone has implemented something like this, I just could not find it yet on CPAN.package MyClass; use base qw(Class::Accessor); sub set { my ($self, $field, $value) = @_; my %VALID = ( fname => qr/^[\w-]+$/, ); if (defined $VALID{$field}) { if (ref $VALID{$field} eq 'Regexp') { if ($value =~ $VALID{$field}) { $self->{$field} = $value; } else { die "Invalid value '$value'\n"; } } else { die "Unknown VALIDATOR"; } } } __PACKAGE__->mk_accessors(qw(fname));
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: accessor generation with constraints
by lima1 (Curate) on Oct 09, 2007 at 13:57 UTC | |
|
Re: accessor generation with constraints
by stvn (Monsignor) on Oct 09, 2007 at 20:01 UTC | |
|
Re: accessor generation with constraints
by TOD (Friar) on Oct 09, 2007 at 12:15 UTC |