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

I posted a question a while back "To register or not to register, that is the question." regarding my user registration. I work most of the errors out of it and there were alot of them but i still have a few that have me stumped.....
When i run it I get these messages....

C:\Perl\bin>perl reg.cgi -c
Unrecognized escape \s passed through at reg.cgi line 102.
syntax error at reg.cgi line 162, near "1;" Missing right curly or square bracket at reg.cgi line 168, at end of line Execution of reg.cgi aborted due to compilation errors.

Line 102

sub good{#99 my @info = @_;#100 my $title = "Registration successful!";#101 my $body = qq(<body>#102
Line numbers but in for you.
End Of Program.
sub password{ $s = @_; srand($s ^ time); @c=split(/ */, "bcdfghjklmnpqrstvwxyz"); @v=split(/ */, "aeiou"); for($i = 1; $i <= 4; $i += 1;); { return($c[int(rand(20))], $v[int(rand(5))]); } }

edited: Thu Jan 2 05:08:43 2003 by jeffa - title change (was: Help Please....), formatting

Replies are listed 'Best First'.
Re: To register or not (part 2)
by jdporter (Paladin) on Dec 31, 2002 at 19:24 UTC
    I guess you missed it in the chatterbox when I said you had an extra semicolon in your for construct. Instead, you put in an extra extra semicolon.

    Taking them both out:
    sub password { my( $s ) = @_; srand( $s ^ time ); my @c = split / */, "bcdfghjklmnpqrstvwxyz"; my @v = split / */, "aeiou"; for ( my $i = 1; $i <= 4; $i++ ) { return( $c[ rand @c ], $v[ rand @v ] ); } }
    But the error there is pretty obvious: you return from password on the very first iteration through the for loop.

    I'm guessing that what you really want to do is accumulate all those things in an array, and return them at the end:
    sub password { my( $s ) = @_; srand( $s ^ time ); my @c = split / */, "bcdfghjklmnpqrstvwxyz"; my @v = split / */, "aeiou"; my @result; for ( my $i = 1; $i <= 4; $i++ ) { push @result, $c[ rand @c ], $v[ rand @v ]; } return @result; }
    This can be made even more idiomatic by using map:
    sub password { my( $s ) = @_; srand( $s ^ time ); my @c = split / */, "bcdfghjklmnpqrstvwxyz"; my @v = split / */, "aeiou"; return map { $c[ rand @c ], $v[ rand @v ] } 1..4; }
    Other things I did to make it "better":
    1. Declare all local variables with my.
    2. Assign $s the first element of @_, rather than the length of @_.
    3. Pass the length of each array to the corresponding call to rand. That way you don't have to hard-code the lengths. (No "magic numbers".)
    4. Remove the int inside the array index; numbers are automatically int-ified in that context.
    5. Increment $i using ++ rather than +=1.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: To register or not (part 2)
by dws (Chancellor) on Dec 31, 2002 at 19:41 UTC
    i still have a few that have me stumped..... When i run it I get these messages....

    In most cases, it makes sense to fix the problems that Perl reports in the order they're reported. But when you see something like

    Missing right curly or square bracket at reg.cgi line 168, at end of line

    that's an indication that you've got unmatched braces that may be screwing up scope upstream. Fix the scoping problems first, and prior problems often vanish quietly.

Re: To register or not (part 2)
by chromatic (Archbishop) on Dec 31, 2002 at 19:15 UTC

    All of Perl's error messages are explained in perldiag. From the command line, type perldoc perldiag to read them. You can also add the line use diagnostics; at the start of your program (after use strict;) to have perl automatically look up the proper explanation when it encounters an error.

    One helpful tip is to fix only the first error message listed at a time, as it may have a cascading effect.

Re: To register or not (part 2)
by vek (Prior) on Dec 31, 2002 at 22:23 UTC
    And just to expand briefly on the point dws made concerning the Missing right curly or square bracket... error - I have found PerlTidy to be rather handy in tracking down those types of errors. It can save you a lot of time and eye strain searching for the missing curly bracket :-)

    -- vek --
Thanks everyone.
by eoin (Monk) on Jan 01, 2003 at 13:15 UTC
    Thanks everyone, I,ve finnaly got it working. I just have to add a few more fetures to it and then i'll upload it.
    The finnal product will be at:
    "http://eoinmurphy.netfirms.com/cgi-bin/reg.cgi"
    The relative html form is at:
    "http://eoinmurphy.netfirms.com/reg.htm"

    Thanks again.
    Eoin.