http://www.perlmonks.org?node_id=181586

andrew has asked for the wisdom of the Perl Monks concerning the following question:

#----Error Checking @required = qw(first second clan nick pass repass email reemail stree +t postal city country); $req_err; for (@required) { next if param($_); $req_err++; last; } if ($req_err) { error("You forgot some required fields"); }
I get this error

Can't call method "param" on an undefined value at /var/securewww/virtual/webewebin.com/a/signup.cgi line 33.

Replies are listed 'Best First'.
Re: Hmm I get this error from $q's
by clintp (Curate) on Jul 14, 2002 at 16:47 UTC
    This piece of script isn't helpful. It either got mangled when you posted it, or you "trimmed" it and cut out the useful bits.

    Generally the error "can't call method X on undefined value" means that at that point in your code you said:

    $y->X()
    and $y happened to be undefined at that time. Why? Look earlier in your code where you created $y with something like:
    $y=new Class
    The creation failed or you didn't create the object (usually with new) at all.

    I'm betting that you said $q->param() somewhere but never actually said $q=new CGI; beforehand to create the CGI object. But that's just my wild guess.

    Sorry to be vauge, but your posting wasn't really helpful.

      Let me expand on that a little. It appears to me that you are using the funcional interface to a module as opposed to the object oriented version. If this is a CGI script and you are tring to call param from CGI.pm the following is needed.

      use CGI qw(:standard);

      This imports the most common functions from CGI.pm including param(). If you are using a different module please post a follow-up with more of your code.

Re: Hmm I get this error from $q's
by rob_au (Abbot) on Jul 15, 2002 at 00:39 UTC
    It looks like you are attempting to validate information passed from form input fields in a CGI script. For this, I would suggest that you might want to look at either HTML::FormValidator or Data::FormValidator - These modules allow for validation of not only the presence of form input fields in the passed CGI parameters, but also the adherence of these fields to allowed formats. For example:

    use CGI; use Data::FormValidator; my $cgi = CGI->new; my $validator = Data::FormValidator->new({ 'form_input' => { 'required' => [ qw/ first second clan nick pass repass e +mail reemail street postal city country / ], 'constraints' => { 'email' => 'email', 'pass' => '/^[\w\d\-\ ]{1,}$/', 'reemail' => 'email', 'repass' => '/^[\w\d\-\ ]{1,}$/' }, 'filters' => [ qw/ trim / ] } }); my %params = map { $_ => $cgi->param($_) } $cgi->param(); my ( $valid, $missing, $invalid, $unknown ) = $validator->validate( \% +params, 'form_input' );

    This little snippet of code performs a number of tasks:

    • Creates a new CGI object, $cgi.

    • Creates a new Data::FormValidator object with a input validation profile called 'form_input' - This input validation profile defines the required fields and sets regular expression constraints on some of them (email, pass, reemail and repass) which need to be met for the field to be considered as 'valid'.

    • Creates a hash of CGI parameters, indexed by the parameter field name, and,

    • Validates the CGI form input fields with the Data::FormValidator validator against the form_input validation profile - This returns four array references containing those fields which are valid, invalid, missing and unknown respectively.