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

I'm working on a simple little Perl script for a class of mine. Essentially it consists of a simple HTML Order Form that passes data to the Perl script via POST. The script then generates a typical confirmation page. The Perl interperter is giving me the following error: "Quantifier follows nothing before HERE mark in regex m/+ << HERE / at order.pl.cgi line 14" The following block of code contains line 14. It is taken directly from my textbook, so for the life of me, I don't know why it would be generating the error:
for ($i=0; $i < $size; $i++){ #11 ($key, $value) = (split /=/, $fields[$i]); #12 $value =~ s/[;<>\(\)\{\}\*\|'`\&\$!#:"\\]/\ $1/g; #13 $value =~ s/+/\ $1/g; #14 $hash{$key} = $value; #15 }
#16 The complete script can be viewed by downlading the attachment at the top of this thread.

Replies are listed 'Best First'.
Re: Problem with assignment for class...
by broquaint (Abbot) on Oct 04, 2003 at 21:51 UTC
    You need to escape the + as it is a meta-character which represents the 1 or more quantifier e.g
    my $value = 'foo+bar+baz'; $value =~ s/\+/ /g; print $value; __output__ foo bar baz
    Also note that I didn't escape the space as it doesn't need to be escaped. See. perlre and perlretut for more info. BTW, if you're trying to parse the key=value pairs from a url then likes of CGI may prove very useful.
    HTH

    _________
    broquaint

Re: Problem with assignment for class...
by BazB (Priest) on Oct 04, 2003 at 21:26 UTC

    $value =~ s/+/\ $1/g;
    + is a metacharacter (more accurately a quantifier) - read perldoc perlre for a full list.

    Update: Mmmm. Perhaps that wasn't completely helpful.
    The error message you're getting is pretty informative as far as error messages go.
    The + is making the substitution/regex expect one or more of whatever character preceeds it.
    In this case, there is no proceeding character, so perl complains.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

      Will this prevent the script from executing (i.e. more of a Warning than an Error such as with C++)? If so, what method should I use for replacing + with a space for processing POST input?
        You still didn't read perlre as recommended, did you?

        "+" is a quantifier. It makes no sense as the only thing in a regexp. It has to modify something. Finding a nonsensical syntax within a regular expression will cause an error, because regular expressions are a built-in part of Perl.

        If you want to match the literal character "+" you can do so by escaping it, or by metaquoting it:

        s/\+/ / # or s/\Q+\E/ /

        But you really shouldn't be parsing your own CGI parameters. Use the CGI.pm module, at least until you understand why this advice is important.

        As for additional reading: The CGI.pm POD. And perlretut, and perlre, and perlrequick all provide good reading material on regular expressions, each targeting a different stage in your learning curve.


        Dave


        "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
Re: Problem with assignment for class...
by Cody Pendant (Prior) on Oct 04, 2003 at 21:45 UTC
    What's the textbook? It seems like it contains untested code. That for loop seems a bit strange, and where does "$size" come from? It looks like a very non-native-perl way to loop through an array.

    The line

    $value =~ s/+/\ $1/g;

    looks like it's trying to replace plus-signs with spaces, which is logical, but I don't understand the right hand side -- that slash doesn't make sense and neither does the $1. And as pointed out before, it ignores the plus sign's special character status it fails. The left hand side of it at least should be this:

    $value =~ s/\+/\ $1/g;


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print