On Thu, 11 Sep 2003 at 07:55, drieux opined:

d:simply because
d:
d:      $obj->can($do);
d:
d:does not mean
d:
d:      $obj->should($do);
d:
d:The problem I am looking for in my should() method
d:is a programatic way to solve Which Method to invoke
d:the correct sub to deal with a query string. I have
d:a tough enough time keeping my code in line as it is,
d:I really do not want it to be reading POD when it should
d:be working and generating HTML.... 8-)

the way i do it is to assign an action to each form.  each action has 
associated parameters.  the form sends the action in an <input 
type="hidden"> tag.

d:
d:My traditional trick is of the form
d:      
d:      my $qs = make_hash_of_query_string($request);
d:      my $actions = {....}; # the hash of subs
d:      my $callForm = $qs->{'callForm'} || 'main_display'; # get the callForm 
d:parameter
d:
d:      my $page = (defined($action->{'callForm'}) ? # is it defined
d:              $action->{'callForm'}->($qs) :
d:              main_display($qs);
d:
d:Which of course requires me to maintain the Hash of $actions
d:so that I 'register' by hand, each of the Subs that SHOULD
d:be used in the cgi code. A problem that I do not escape
d:if I have to maintain a similar list of which 'callForms'
d:are legitimate tokens to be returned from the browser.

another way is to check the list of acceptable 'actions' in the module
constructor, then if you pass an action method that's not defined, return
an error.  for example:

package Foo;
use Carp;

{
  my %REQ_PARAMS = (
    action1 => { # criteria for action 1 },
    action2 => { # criteria for action 2 },
    etc...
  );

  sub req_params {
    return $REQ_PARAMS{shift->action};
  }
}

sub new {
  my ($class, %args) = @_;

  croak "missing action arg" unless defined $args{action};

  my $self = bless {
    _ACTION     => $args{action}
  }, $class;

  croak "method $args{action} not found" unless $self->req_params;

  return $self;
}

sub action { shift->{_ACTION} }


then in your script:

#!/usr/bin/perl -w

use Foo;

my $f1 = Foo->new( action => 'action2' ); # works ok
my $f2 = Foo->new( action => 'action6' ); # shits the bed



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to