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

I would like someone to look over my .cgi script. Its not very long, and is for a class. I completed it and am not looking for answers, however it's not working, and I don't understand the errors i'm getting.

#!/usr/bin/perl #c11case2a.cgi - Allows user to enter info use CGI qw(:standard -debug); #print “Content-type: text/HTML\n\n”; #prevent Perl from creating undeclared variables use strict; #declare variables and assign variables my ($item, $C_basket, @basket); #make cookies $C_basket = cookie( -name => "Basket", -value => "@basket", -path =>"../../cgi-bin/chap11"); #send cookies print header(-cookie => $C_basket ); #display Web page Print <<addressForm; <HTML> <HEAD><TITLE>Jarrod Accessories</TITLE></HEAD> <BODY> <H2>Jarrod Accessories</H2> <H3>Enter Your Information</H3><HR> <FORM ACTION="http://endor.vvc.edu/~shaninarice/cgi-bin/chap11/c11case +2b.cgi" METHOD=POST> <TABLE> <TR><TD>Name</TD><TD><INPUT NAME=Name SIZE=40></TD></TR> <TR><TD>Address</TD><TD><INPUT NAME=Address SIZE=40></TD></TR> <TR><TD>City</TD><TD><INPUT NAME=City SIZE=40></TD></TR> <TR><TD>State</TD><TD><INPUT NAME=State SIZE=40></TD></TR> <TR><TD>Zip Code</TD><TD><INPUT NAME=Zipcode SIZE=40></TD></TR> </TABLE> <BP><INPUT TYPE=submit VALUE=Submit> <INPUT TYPE=reset></P> </FORM> </BODY> </HTML> addressForm

And the errors I get running perl -c:

Bareword found where operator expected at c11case2a.cgi line 29, near + "<TITLE>Jarrod" (Missing operator before Jarrod?) Bareword found where operator expected at c11case2a.cgi line 32, near +"<H3>Enter" (Missing operator before Enter?) String found where operator expected at c11case2a.cgi line 33, at end +of line (Missing semicolon on previous line?) syntax error at c11case2a.cgi line 29, near "HEAD>" Can't find string terminator '"' anywhere before EOF at c11case2a.cgi +line 33.
and the second one....

#!/usr/bin/perl #c11case2b.cgi - Displays user info print “Content-type: text/HTML\n\n”; use CGI qw(:standard); #prevent Perl from creating undeclared variables use strict; #declare and assign variables my ($name, $address, $city, $state, $zipcode, $item, $rec, @items); my @purchases = ( “Gold Bracelet”, “Silver Bracelet”, “Diamond Necklace”, “Ruby Earrings”, “Pearl Ring”); $name = param(‘Name’); $address =param(‘Address’); $city =param(‘City’); $state =param(‘State’); $zipcode =param(‘Zipcode’); #get order from cookie @items = split (/ /, cookie (‘Basket’)); #display order confirmation print "<HTML>\n"; print "<HEAD><TITLE>Jarrod Accessories</TITLE></HEAD>\n"; print "<BODY>\n"; print "<H1H2>Jarrod Accesories</H1H2> \n"; print “$name, Thank you for your order of:<BR><BR>\n”; foreach $item (@items) { print “$purchases [$item]<BR>\n”; } print “<BR>It will be shipped to: <BR><BR>\n”; print “$address<BR>\n”; print “$city,$nbsp; $state $nbsp; $zipcode<BR>\n”; print "</BODY>\n”; print “</HTML>\n";

And It's errors:

Bareword found where operator expected at c11case2b.cgi line 10, near + "my @purchases = ( "Gold" (Might be a runaway multi-line "" string starting on line 3) (Do you need to predeclare my?) syntax error at c11case2b.cgi line 10, near "my @purchases = ( "Gold B +racelet" Unrecognized character \xE2; marked by <-- HERE after d Bracelet<-- HE +RE near column 33 at c11case2b.cgi line 10.

Replies are listed 'Best First'.
Re: Could you look over my completed (but not working) short .cgi script?
by kcott (Archbishop) on May 31, 2014 at 06:36 UTC

    G'day StarRice,

    Welcome to the monastery.

    "And the errors I get running perl -c: ..."

    Look at "Print <<addressForm;": you'll want to s/Print/print/.

    "And It's errors: ... (Might be a runaway multi-line "" string starting on line 3) ..."

    Look at the double quotes you use on line 3. Not &quot;, instead &ldquo;.

    $ perl -Mutf8 -E 'say "Normal double quote: ", ord q{"}'
    Normal double quote: 34
    
    $ perl -Mutf8 -E 'say "Your double quote: ", ord q{“}'
    Your double quote: 8220
    

    -- Ken

      Ahh yup. I fixed the errors and now its working fine. :) Thank you for the suggestions/pointing them out. I have one more question now, when I test the script it doesn't display the 'items ordered/bought' on the last page (c11case2b.cgi). Is my foreach statement correct for that?
Re: Could you look over my completed (but not working) short .cgi script?
by taint (Chaplain) on May 31, 2014 at 04:48 UTC
    Greetings, StarRice.

    Since I understand this is homework. I'll not solve the issue(s) for you. But rather, give you a reading assignment. :)

    Please read the fine Perl documentation. Especially perlop, specifically for your case Quote and Quote like Operators. Other keywords you might find useful are interpolate, and interpolation. As well as print, quote, and quote characters. Lastly, use diagnostics; will often tell you exactly what was wrong, and suggest ways to correct it.

    Best wishes.

    --Chris

    ˇλɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      Alright, thank you. I read the documentation...yikes. It goes way beyond the book we had (a basic book. And I added  use diagnostics, however I don't understand how it thinks I think a 'delimitter' in the middle of my HTML title, do I need to add semi-colons to the end of all of them? I'm able to get help on assignments directly, just not like...if I had asked someone to do it for me. I've never encountered a problem like this though before, it was all simple stuff.

        Ugh. This is really difficult for me too. It would be so easy for me to "spill the beans". But given this is an assignment. I can only point you toward the text that supplies you with the information you need to solve your problem. I gave you some other key words to search for, that would have armed you with the answers. Let me just suggest that quoting really is the key here. As is interpolation. If you continue down the page I gave you in the "reading assignment", and read the section "Gory details of parsing quoted constructs". You should also find clues to what you've done incorrectly. While doing so, think quoting quotes. That may help you discover what you need to know.

        Best wishes.

        --Chris

        ˇλɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      I spoke too soon! I figured it out on the first one. :) Trial and error works successfully sometimes I guess, haha. Thank you! Let me try my hand at the second one now.
Re: Could you look over my completed (but not working) short .cgi script?
by NetWallah (Canon) on May 31, 2014 at 06:50 UTC
    These are common typo's, and the documentation on "heredoc" is terse for a beginner, and there is nothing but syntax here - so I'll help out.

    For c11case2a.cgi, change the Print statement to lower case, and quote the "heredoc" :

    print << "addressForm";
    For the c11case2b - it appears you have used a word processor to edit the script - and that has placed non-standard double quotes instead of plain ones.

    Using a simple text editor and fixing the quotes should get you started.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: Could you look over my completed (but not working) short .cgi script?
by GrandFather (Saint) on May 31, 2014 at 22:34 UTC

    Using strictures (use strict; use warnings;) is excellent and something you should always do. Batch declaring variables negates the most valuable diagnostic that strict provides. In fact what you wreck is exactly what your comment describes as the reason for using strict! Consider this fragment of your code:

    #prevent Perl from creating undeclared variables use strict; #declare variables and assign variables my ($item, $C_basket, @basket); #make cookies $C_basket = cookie( -name => "Basket", -value => "@basket", -path => "../../cgi-bin/chap11" );

    Do you see anything wrong there? How about if we rewrite it this way and run it:

    use strict; #make cookies my $C_basket = cookie( -name => "Basket", -value => "@basket", -path => "../../cgi-bin/chap11" );

    Now you get an error: Global symbol "@basket" requires explicit package name which is strict's way of saying "I haven't seen the variable @basket before". In other words, @basket is undefined.

    So, what do you expect the cookie value to be? What it will be is an empty string, but if you intended that you should write that. If you didn't intend that, batch declaring variables has masked exactly the sort of bug strict should alert you to.

    Perl is the programming world's equivalent of English