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

Fellow Monasterians,

I'm using a validation package to determine a number of things, in this case whether a radio button in a form has been clicked on.

Fundamentally, my problem comes when passing values to the package. If param('method') has a value, there is no problem—all the values are passed in proper order. However, if 'method' has no value, the flag '1' I'm using to indicate that the selection of a 'method' is mandatory is moved the to $value position in my package and a true value is erroneously returned.

If use quotes: "$query->param('method')" the $value in the package is CGI=HASH(0x811b24c)->param('method') and basically get no measurable result.

So, how do I make sure that the package "sees" the undefined value? The stop-gap would be to reverse the order Validate->checked (1, $query->param('method')), but that doesn't seem like a long-term solution.

Thanks!

use Validate; my ($method, $error) = Validate->checked ($query->param('method'), 1); if ( $error->{ msg } ) { push @errors, "A <b>method</b> $error->{ msg +}" } package Validate; sub checked { my ($class, $value, $mandatory) = @_; print Dumper ($class, $value, $mandatory); if (!$value && $mandatory) { return (undef, { msg => 'must be checked' }); } else { return ($value); } } 1;

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Passing undefined values to package
by tlm (Prior) on Aug 04, 2005 at 13:28 UTC

    Since checked is only checking for a scalar, the simplest solution would be this:

    Validate->checked( scalar $query->param( 'method' ), 1 );
    Without the scalar, the call to param happens in list context, so it returns the empty list, (), which gets lost. By forcing a scalar context then param returns undef.

    the lowliest monk

      Wow, how 'fundamental' is that?! I think I read about scalar in the first 5 minutes of my first Perl book. But how soon we forget. Thanks, it worked great and required the least amount of changes to my code.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Passing undefined values to package
by JediWizard (Deacon) on Aug 04, 2005 at 13:18 UTC

    my $manditory = (defined($query->param('method')) ? $query->param('met +hod') : 0; my ($method, $error) = Validate->checked ($manditory, 1);

    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol