Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

OR sequence

by batmanor (Novice)
on Mar 21, 2008 at 22:11 UTC ( [id://675538]=perlquestion: print w/replies, xml ) Need Help??

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

I started with just one field, {'Comments'} to catch unwanted html code and with your help it worked. Now I want to catch the code in the other fields as well, {'addr'} {'name'} so I came up with the following:
# If http or <a href= tags are present abort the message if ($FORM {'Comments'} || $FORM{'addr'} || $FORM{'name'} =~m/http|h +tml|HTML|A HREF|a href/i) { &html_message; }
However, no matter what I write with or without html I get sent to the sub routine &html_message. What am I doing wrong?

Thanks in advance, David

Thank you all:

chargrill hipowls ikegami lodin NetWallah

for your help!

batmanor

Replies are listed 'Best First'.
Re: OR sequence
by chargrill (Parson) on Mar 21, 2008 at 22:18 UTC

    You can't do that.

    You'll need something like:

    for my $item( @FORM{ qw/Comments addr name/ } ){ if( $item =~ m/http|html|A HREF/i) { &html_message; } }

    Other notes: you don't need the HTML and html with the /i at the end of your regex

    Your test for HTML is a little naive.

    Your test was just testing:

    if( there's anything in Comments or there's anything in addr or name matches regex ){ &html_message; }

    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

      This will call &html_massage multiple times if more than one of the three items match. That may not be the OP's intent. To call &html_massage only once modify the loop to

      ITEM: for my $item( @FORM{ qw/Comments addr name/ } ){ if( $item =~ m/http|html|A HREF/i) { &html_message; last ITEM; } }

      You need a last in your if to avoid calling html_message three times.
Re: OR sequence
by ikegami (Patriarch) on Mar 21, 2008 at 22:38 UTC

    || does not form a list of operands to pass to another operator. There's isn't such an operator in Perl5 (but I bet there is in Perl6). Of its two operands, it returns the first one that is true.

    What you want is

    if ( $FORM{'Comments'} =~ m/http|html|a href/i || $FORM{'addr'} =~ m/http|html|a href/i || $FORM{'name'} =~ m/http|html|a href/i )

    You can remove the redundancy with a loop

    for (@FORM{qw( Comments addr name )}) { if (m/http|html|a href/i) { html_message(); last; } )

    Is there a reason you didn't apply my suggestion to change
    /http|html|HTML|A HREF|a href/i
    to the simpler and equivalent
    /http|html|a href/i
    That's what the "i" does.

Re: OR sequence (ARRAY ~~ /PATTERN/)
by lodin (Hermit) on Mar 21, 2008 at 22:58 UTC

    One way of doing it is

    if (grep { /http|html|a href/i } $FORM{'Comments'}, $FORM{'addr'}, $FO +RM{'name'}) { ... }
    In Perl 5.10 that can be written as
    if ([ $FORM{'Comments'}, $FORM{'addr'}, $FORM{'name'} ] ~~ /http|html| +a href/i) { ... }
    which is pretty close to how you tried to write it. The latter is also slightly more efficient for large lists, as it stop to look in the list as soon as it finds a match, in contrast to grep which searches the whole list.

    lodin

Re: OR sequence
by NetWallah (Canon) on Mar 21, 2008 at 23:10 UTC
    As usual, TMTOWTDI.

    Here is (another)one:

    if (grep /http|html|a href/i,@FORM{qw[name addr Comments]}){ html_message(); }
    More alternatives:

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: OR sequence
by apl (Monsignor) on Mar 22, 2008 at 15:13 UTC
    You shouldn't have deleted your original question. It prevents others from learning from your question, and confuses those of us who are new to thread.
      The question has been restored.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://675538]
Approved by chargrill
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found