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

Hello ,
I am having a lot of trouble getting this script to run correctly. The if statement on line 47 does not see the $currentvariable. Is this related to the errors below?

I would appreciate any help. Thanks

The script is called from a submission form, using these four parameters. When I run this I get errors on all the lines using param:
my $writer = param{'writer'};
my $title = param{'title'};
my $text = param{'text'};
my $current = param{'current'};
Odd number of elements in hash assignment at reflections.cgi line 11 Odd number of elements in hash assignment at reflections.cgi line 12 Odd number of elements in hash assignment at reflections.cgi line 13 Odd number of elements in hash assignment at reflections.cgi line 14
<--- START SCRIPT -->
#!/usr/local/bin/perl -w use strict; use diagnostics; use CGI qw(:standard); my $cgi = new CGI; my $old_writer; my $old_title; my $old_text; my $writer = param{'writer'}; my $title = param{'title'}; my $text = param{'text'}; my $current = param{'current'}; my $html_file ="reflections.txt"; #this filename holds the $reflection_holder variable for finsh_events. +cgi my $data_file ="reflections.dat"; #this filename holds the $data_holder variable for manipulation my $data_holder = "$writer :: $title :: $text"; #this hold the reflection in a data form with "::" delimiters my $reflection_holder ="$writer <BR><BR><Font color=green>$title</font +> <BR> <br><Font face=Verdana, Arial, Helvetica, sans-serif size=1>$t +ext</font>"; #this holds the reflection in an html format #archive_holder holds the archive reflection html format my $archive_holder = " <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=windows-12 +52'> <meta name='GENERATOR' content='Microsoft FrontPage 4.0'> <meta name='ProgId' content='FrontPage.Editor.Document'> <title>Reflections</title> </head> <body bgcolor=#ffffff> <p><font face='Book Antiqua' color='#b5a466' size='5'>Reflections</fon +t></p> <p><font face='Book Antiqua' size='2'><font color='#B5A466'>by</font>& +nbsp; $writer </font></p> <p><font size='2'>$title</font></p> <hr> <p><font SIZE='2'>$text <P> <P></font></p> </body> </html>"; print "Content-type: text/html\n\n"; if ($current eq "ON") { print "<HR><B>This is what will be posted on the Reflections section o +f the homepage.</b><HR>\n"; print "by $writer<BR><BR>\n"; print "<Font color=green>$title</Font><BR><BR>\n"; print "$text<HR><br><br>\n"; #$text is actually the variable that con +tains the reflection #READ IN the old reflection data file before overwriting so it can be +archived open(FILE,"$data_file"); my @old_reflection=<FILE>; close FILE; foreach my $i (@old_reflection) { chomp($i); ($old_writer,$old_title,$old_text) = split(/\ :: /,$i); # this is the same as $count = $count + 1; my $count++; } my $the_title = "$old_title.shtml"; #current_holder holds the current reflection in html format for archiv +ing my $current_holder = " <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=windows-12 +52'> <title>Reflections</title> </head> <body bgcolor=#ffffff> <p><font face='Book Antiqua' color='#b5a466' size='5'>Reflections</fon +t></p> <p><font face='Book Antiqua' size='2'><font color='#B5A466'>by</font>& +nbsp; $old_writer</font></p> <p><font size='2'>$old_title</font></p> <hr> <p><font SIZE='2'> $old_text<P> <P></font></p> </body> </html>"; #print "<br><br> $current_holder <BR><BR>\n"; #store current reflections in archive open (FILE, "+> ../../../reflections/archive/$the_title"); print FILE "$current_holder\n"; close FILE; #create new reflection text file open (FILE, "+> $html_file "); print FILE " $reflection_holder\n"; close FILE; #create new reflection data file open (FILE, "+> $data_file "); print FILE "$data_holder\n"; close FILE; print "<FORM action=/cgi-local/homepage_update/events/finish_events.cg +i method=post><input type='submit' value='Submit' name='reflections'> +</form>\n"; } else { #store just entered reflection in Archive for later use print "This will be stored in the Archive until it is used\n"; open (FILE, "+> ../../../reflections/archive/$title.shtml "); print FILE "$archive_holder\n"; close FILE; }

Replies are listed 'Best First'.
Re: If statement wont branch
by BrowserUk (Patriarch) on Nov 21, 2002 at 00:49 UTC

    Try changing my $writer = param{'writer'}; and the other similar lines to

    my $writer = $cgi->param('writer');. I think that will fix some of your problems.

    Update: I also noticed this code:

    foreach my $i (@old_reflection) { chomp($i); ($old_writer,$old_title,$old_text) = split(/\ :: /,$i); # this is the same as $count = $count + 1; my $count++; }

    Whilst, as your comment states, $count++ is the same as $count = $count _ 1;. By declaring $count within the for loop, it is scoped (see:Lexical scoping like a fox for a head start on understanding what this means. ) to the for loop. This means that it will get re-created each time around the loop with the net result is it will never take a value higher than one. Currently you do not seem to be making use of this value so it isn't affecting anything, but if you intend using it later on, you should probably declare the vaiable before the loop something like this:

    ... my $count=0; foreach my $i (@old_reflection) { chomp($i); ($old_writer,$old_title,$old_text) = split(/\ :: /,$i); # this is the same as $count = $count + 1; $count++; } ....

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: If statement wont branch
by runrig (Abbot) on Nov 21, 2002 at 00:49 UTC
    I get errors on all the lines using param:
    my $writer = param{'writer'}; my $title = param{'title'}; my $text = param{'text'}; my $current = param{'current'};
    Perhaps you'd like to look more closely at the CGI docs, and call the param function (or call the param method on your CGI) instead of passing a hash array reference to param?
    my $writer = param('writer') # or my $writer = $cgi->param('writer'); # etc.
    Update:Your calls are parsing to this:
    my $var = param({'this'});
    So perl is interpreting your argument as a hash array reference with one key and no value.
Re: If statement wont branch
by thelenm (Vicar) on Nov 21, 2002 at 00:48 UTC
    I don't know if it has anything to do with line 47, but you'll want to add a $ sign to "param" on the lines giving you errors, e.g.
    my $writer = $param{'writer'}; my $title = $param{'title'}; my $text = $param{'text'}; my $current = $param{'current'};
    Update: Whoops, you're not trying to use a hash after all. I should look before I leap. See the answers below.

    -- Mike

    --
    just,my${.02}