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

Why won't this code work. I have a parse script that I am trying to stop my forms from being abused. This line keeps giving me a 500 error.
$value =~ tr/[/url][/[/url] [/g;
Thanks in advance

Replies are listed 'Best First'.
Re: How to use tr///
by rhesa (Vicar) on Dec 17, 2006 at 02:09 UTC
    because it's a syntax error :-)

    See tr, s, and perlre for how to write tr operations, and proper regular expressions.

    I can only guess at what you're trying to do, but did you mean the following instead?

    $value =~ s{\Q[/url][\E} {[/url] [}g;
Re: How to use tr///
by graff (Chancellor) on Dec 17, 2006 at 02:11 UTC
    (1) You have too many slashes in that line, (2) the square brackets should either be balance or escaped, and (3) you are confusing "tr///" with "s///" -- only the latter takes a "g" modifier.

    Figure out whether you are doing a string substitution (s///) or a character transliteration (tr///); use something besides "/" as the delimiter for the statement -- e.g. do  tr{x/z}{abc} or  s{a/c}{xyz}g; -- and if you are using s///, you have to use \[ to match a literal square bracket; any unescaped square brackets must be balanced, and should surround a set of characters that you intend to match at the given position in the regex.

Re: How to use tr///
by swampyankee (Parson) on Dec 17, 2006 at 02:14 UTC

    I suggest you check perlop, looking for tr///; I'm not sure what you're trying to do, but I think you're confusing the transliteration operator (tr///) with the substitution operator (s///); the former does not process regex.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: How to use tr///
by Devanchya (Beadle) on Dec 17, 2006 at 02:33 UTC
    To add to what has been said, the number one thing to help make you go less insane reading that is to use a different divider.

    I really wish more books changed the divider in regular expression examples to get across that you can use more than one symbol. It's become my cause at work to get co-workers to stop making \ / soup.

    $value =~ s!/foo!/bar!g
    --

    Even smart people are dumb in most things...
Re: How to use tr///
by GrandFather (Saint) on Dec 18, 2006 at 01:47 UTC

    Sigh. Trivial syntax errors like this should be picked up and fixed in no time flat, and with strictures they are. Add use strict; use warnings; to your code and you will see:

    syntax error at noname.pl line 4, near "tr/[/url][/[" Missing right curly or square bracket at noname.pl line 4, at end of l +ine

    reported. Code that you obtain from elsewhere ought to have strictures added and then any issues flaged ought to be looked at. Note though that there is valid code around that will throw up warnings and errors - often because the code is using techniques that have been deemed unsafe in some sense. Edit with care!


    DWIM is Perl's answer to Gödel
Re: How to use tr///
by Anonymous Monk on Dec 17, 2006 at 02:36 UTC
    Sorry I should have offered a better explaination.
    $value =~ tr/[/url][/[/url] [/g;
    The reason I am trying to use the code above is: I have someone abusing one of my web forms. They add lines in the form which include [/url][. So, I was trying to write a bit in my script that picks this up and recognises it as this abuser and stops him from being able to send his spam through my form. I have tried IP blocking but he obviously has a dialup account and it keeps changing his IP address, so the next best thing I could think of was to block him when the script sees this text. If anyone has a better idea on how to stop people from abusing my web forms it would be appreciated. Thank you again.
      Did you write this code or did you get it from somewhere on the net? Can you post the code here? The reason I ask is that there are probably other ways to exploit your form and this fix would just be a stopgap.
        Here is a copy of the script as requested.