I've hacked up a class to be inherited from allowing me to parse an input hash, verify that each key value pair passes a regex for that key, and access keys by method name.

The third capability (accessing keys by method name) is giving my mind fits right now (possibly because it's almost 5:00 A.M). I'd like to be able to call  $o->parameters->user, and access the user attribute held in the object $o (Note: not $o->parameters).  $self->{'params'}->{$_} would access it inside a subroutine. How would I 'fake' an object, so as to have  $o->parameters->user return  $o->{'params'}->{'user'}, and have  $o->parameters->user('Jeffory') change  $o->{'params'}->{'user'} to Jeffory, after validating it with a subroutine in my class (so, in fact  $o->{'params'}->{'user'} = $o->validate('Jeffory'); ) In other words, the parameters method has to return an object that has access to the class it's currently in, parses the method called on it, and executes the code (the object could be self, but I'd prefer not to do that, as I'd like to allow people to use _anything_, even new or validate, as a parameter--it's probably what I'll end up doing, though):
return $o->{'param'}->{$method};
The easiest way ( I guess?) to do that would be to create a 'false' object, that captures the method call, but is actually in the class that $o is in, and therefore can call its methods...? Any ideas if that's available?

Obviously, there's a major design flaw in this class, but humor me (or give me advice on the code, which I've posted below--the code probably doesn't work, I've been freewriting it, which means there are probably spelling errors, and important logic errors ) :)
package Input::SignUp; our @ISA = qw/Input/; use strict; use warnings; sub _init { $_[0]->make_methods( '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' ], ); } 1;
package Input; use strict; use warnings; use Want; sub new { my $self = bless {}, shift; $self->{'params'} = Parameters->new; $self->_init; $self; } sub make_methods { $self = shift; my %methods = @_; for (keys %methods) { my($p,$re,$error) = $methods{$_}; $self->{'regex'}->{$p} = $re; $self->{'error'}->{$p} = $error; } } sub validate { $self = shift; my %input = @_; $self->{'params'}->{$_} = $self->_validate($input{$_},$_}) for keys +%input; } sub _validate { $_[1] =~ $_[0]->{'regex'}->{$_[2]} or $_[0]->error($_[2],$_[0]->{'er +ror'}->{$_[2]}); $1; } sub error { my $self = shift; push @{$self->{'errors'}}, [ @_ ] and return 1 if scalar @_ == 2; return scalar @{$self->{'errors'}}; } sub error_hash { my @loop_data; for (@{$self->{'errors'}}) { push(@loop_data, { 'ERROR' => ucfirst $_->[0].' Error: '.$_->[1] } + ) for @{$self->{'errors'} }; } return ( 'ERRORS' => \@loop_data ); } sub parameters { my $self = shift; return bless [], if want('OBJECT'); # Single Param. # This is where I'm stuck. Ideally, the object would be called # as $o->parameters->user('Bob'), or something like that. Then # We'd validate the parameter, using the key 'user'. It's rather # simple to do, by just changing parameters->user to parameters->('u +ser','bob'), # but I don't want that! } 1;

Gyan Kapur

In reply to Capturing Method Call, And Relaying. by Revelation

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.