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

elsif ($q->param('name') || $q->param('site') || $q->param('siteid') | +| $q->param('email') || $q->param('pass') || $q->param('aim') || $q-> +param('yahoo') || $q->param('icq') || $q->param('msn') || $q->param(' +location') || $q->param('intrests') =~ /\|/){ &error("Please make sure no fields have vertical bars, |"); }
I dont get what is wrong here, evertime it checks it always says that I need to check for vertical bars. It wont let me get passed this statment if everthing is right

Replies are listed 'Best First'.
Re: using logical in if statement
by Corion (Patriarch) on Jun 11, 2002 at 19:36 UTC

    I'm not exactly sure if you know the difference between what you want and what you wrote (and how Perl interprets that).

    What you want seems to be the statement "No field may contain a vertical bar".

    What you wrote is a translation from english to Perl like "If the name or the site or ... or the intrests contain a vertical bar, return an error ".

    What Perl makes of your sentence is "If the name is not the empty string and not 0 or the site is not the empty string and not 0 or ... or the interests contain a vertical bar, return an error".

    This is obviously not what you wanted. There are two ways to approach what you want to achieve. Either reformulate your intention - if no single string may contain a vertical bar, then also the concatenation of these strings may not contain a vertical bar. Or create a predicate function, has_vertical_bar, which tells you if a single string contains a vertical bar :

    sub has_vertical_bar { my ($string) = @_; return $string =~ /\|/; }; # ... later, in your code ... my @parameters = qw(name site siteid email pass aim yahoo icq msn loca +tion intrests ); my $errormsg = ""; foreach my $parameter (@parameters) { if has_vertical_bar($q->param($parameter)) { $errormsg ||= "There was some problem with your input. Please make + sure no fields have vertical bars."; $errormsg .= "I found a vertical bar in the field $parameter. "; }; }; error($errormsg) if ($errormsg);

    Update: You might also want to take a look at disallowing vertical bar characters, which (strangely enough) seems to revolve about a similar problem.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: using logical in if statement
by danboo (Beadle) on Jun 11, 2002 at 19:38 UTC
    You're not doing what I think you think you're doing. Err, umm...

    You're testing whether any of the params are *true* or the last one matches a pipe, then error out. Instead you either want to put the matches after each param, or as I might do this:

    elsif (grep { index($q->param($_), '|') >= 0 } qw/name site siteid/) { &error("Please make sure no fields have vertical bars, |"); }
    Note the use of index rather than a regex. Just a personal preference for the type of match you wish to do.

    Cheers,

    - danboo

Re: using logical in if statement
by VSarkiss (Monsignor) on Jun 11, 2002 at 19:40 UTC

    It's only testing whether $q->param('intrests') (sic) doesn't match a vertical bar. For all the other values, it's testing whether they're false (empty string or zero).

    This is a pretty awful way to structure an ifbut to get the effect you want you'd have to do:

    if ($q->param('name') =~ /\|/ || $q->param('site') =~ /\|/ || $q->param('siteid') =~ /\|/ # and so on.... ) { # whatever...
    It'd be much nicer to do:
    foreach my $p (qw(name site siteid ...)) { if ($q->param($p) =~ /\|/) { error("$p has a vertical bar, that's bad"); # or something } }
    HTH

Re: using logical in if statement
by dsheroh (Monsignor) on Jun 11, 2002 at 19:40 UTC
    You are not performing the test you think you are performing.

    Based on the error, it appears that you want to see whether any of the listed params contains a vertical bar. The code you have written checks for whether 'intrests' contains a vertical bar or any of the other params is non-empty and non-zero.

Re: using logical in if statement
by hacker (Priest) on Jun 11, 2002 at 19:41 UTC
    Redundant reply removed for brevity.