in reply to Capturing Method Call, And Relaying.
Have a look at this, it may be the sort of thing you are looking for:
package OO; my $properties = { 'user' => [ qr/^[a-zA-Z0-9]+$/, 'Username may only contain alphanu +meric characters' ], 'password' => [ qr/^[a-zA-Z0-9]+$/, 'Password may only contain alp +hanumeric characters' ], 'key' => [ qr/^[a-fA-F0-9]+$/, 'Key may only contain A-F alphanume +ric characters' ], }; $o = OO->new( user => 'username' ); $o->password( 'secret' ); $o->key( '***Invalid' ); $o->no_exist( 'foo' ); print "Password: ", $o->password(), $/; print "Input errors:\n" . $o->error() if $o->error(); use Data::Dumper; print Dumper $o; sub new { my $class = shift; my $self = { error => '' }; bless $self, $class; $self->init(@_) if @_; return $self; } sub init { my ( $self, %props ) = @_; for my $prop ( keys %props ) { my $value = $props{$prop}; $self->$prop( $value ); } } sub AUTOLOAD { my ( $self, $value ) = @_; my ( $method ) = $AUTOLOAD =~ m/::([^:]+)$/; if ( $properties->{$method} ) { if ( defined $value ) { # its a set method if ( $value =~ m/$properties->{$method}->[0]/ ) { $self->{$method} = $value; } else { $self->error( $properties->{$method}->[1] . "\n" ); } } else { # we have a get method return $self->{$method}; } } else { $self->error("Unknown method $method called from " . (join ' ' +, caller()) . "\n" ); } } sub error { my ( $self, $error ) = @_; $self->{error} .= $error if $error; return $self->{error}; } __DATA__ Password: secret $VAR1 = bless( { 'error' => 'Key may only contain A-F alphanumeric cha +racters Unknown method no_exist called from OO C:\\PROGRA~1\\PERLBU~1\\debug\\ +oo 15 ', 'password' => 'secret', 'user' => 'username' }, 'OO' ); Input errors: Key may only contain A-F alphanumeric characters Unknown method no_exist called from OO C:\PROGRA~1\PERLBU~1\debug\oo 1 +5
cheers
tachyon
|
|---|