Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

bareword error

by batmanor (Novice)
on Mar 13, 2008 at 23:02 UTC ( [id://674094] : perlquestion . print w/replies, xml ) Need Help??

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

PerlMonks, I am trying to weed html urls from a comment box and use this code. I seems to work in on web site but not another, though the code is identical. I got the m// operator from programming in Perl (O'Reilly, etc) for this code:
# If <html> or <a href= tags are present abort the message if ($FORM {'comments'} = m//http|html|HTML|A HREF|a href/i) { &html_message; }
I got this error message:
Bareword found where operator expected at line 75, near "m +//http" syntax error at line 75, near "m//http" Execution of aborted due to compilation errors.
Any help would be appreciated. David

Replies are listed 'Best First'.
Re: bareword error
by FunkyMonk (Chancellor) on Mar 13, 2008 at 23:21 UTC
    The match operator expects its pattern to be surrounded by delimeters, usually m// (ie your pattern needs to be between the slashes). Another problem you've got, is that if the match is not bound to an expression, it will match against $_. You should use =~ to bind the match to an expression.

    So, you need to change your if to something like

    if ($FORM {'comments'} =~ m/http|html|HTML|A HREF|a href/i )

      and since the i modifier is being used,
      if ($FORM {'comments'}  =~ m/http|html|HTML|A HREF|a href/i )
      can be written as
      if ($FORM {'comments'}  =~ m/http|html|a href/i )

      Note to self: Put two spaces between a and href when using batmanor's form.

        Even further refined:
        if ($FORM {'comments'} =~ m/http|html|a\s+href/i ) any number of, but more than one, whitespace characters are valid between a and href.
      Thanks FunkyMonk,ikegami and Bloodrage for your help. The script is working properly now. Batmanor
Re: bareword error
by grep (Monsignor) on Mar 13, 2008 at 23:40 UTC
Re: bareword error
by johngg (Canon) on Mar 13, 2008 at 23:42 UTC
    A couple of points:

    • You are doing a match, not an assignment so use =~ rather than =.

    • Is the second slash in your m//http|html|HTML|A HREF|a href/i intended or a typo? It is why you are getting a bareword error because you are matching against an empty pattern (m// which actually means last successful match, thanks hipowls for pointing that out) and the 'http' that follows is the bareword. If intended, either escape it with a backslash (m/\/http..../) or change the delimiter used to something other than a forward slash (m{/http....}).

    I hope this is helpful.



      Good answer, one error though, m// is not an empty pattern, it is the last pattern to successfully match, split gets special dispensation so that split m//, $stuff does split on an empty pattern.

      From the split reference:

      As a special case for split, using the empty pattern // specifically matches only the null string, and is not be confused with the regular use of // to mean "the last successful pattern match". So, for split, the following:
      print join(':', split(//, 'hi there'));
      produces the output 'h:i: :t:h:e:r:e'.

        Cripes, I'd never realised that, thanks for pointing it out. I had to write a little piece of code to prove it to myself.

        $ perl -le ' > $s1 = q{catDOGfish}; > $s2 = q{xxYYYzz}; > print $1 if $s1 =~ m{([A-Z]+)}; > print $1 if $s1 =~ m{([0-9]+)}; > print $1 if $s2 =~ m{};' DOG YYY $

        Fancy not knowing that after all this time!

        Thanks again,