Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: problem with variable

by screamingeagle (Curate)
on Jul 18, 2002 at 01:19 UTC ( #182673=note: print w/ replies, xml ) Need Help??


in reply to problem with variable

it's difficult to answer your question without seeing some code, but you might want to check whether you've defined the $month variable in an inner block of code, and you're trying to access the value of the variable in an outer block...


Comment on Re: problem with variable
Re: Re: problem with variable
by cal (Beadle) on Jul 18, 2002 at 02:07 UTC
    Thanks, I hope this can help.
    #!/usr/local/bin/perl use CGI qw/:all/; $| = 1; my $q = new CGI; my $action = $q->param('action'); # Read in all the variables set by the form CGI::ReadParse(*input); # Print out a content-type for HTTP/1.0 compatibility print "Content-type: text/html\n\n"; $month = $input{'month'}; $date = $input{'date'}; $year = $input{'year'}; #$action = param('action'); #print $action; $numberoflines = &CountCurrentRecords('events.txt'); if($action eq "add_data"){ print "Your post was sucessful<br>"; #check_event(); add_data(); } else{ #print "We are not in the Add sub-routine<BR>"; #this is just an example fictitious default subroutine } if($action eq "preview"){ #print "<BR>Is this information correct?<br>"; preview(); } else{ #print "We are not in the Preview sub-routine<br>"; #this is just an example fictitious default subroutine } ############### BEGIN COUNTCURRENTRECORDS SUBROUTINE ################# +####### sub CountCurrentRecords { $number = 1; my($database) = ($_[0]); open(DATABASE, "$database"); while ($line=<DATABASE>) { $number++; } $number = 0 if $number < 0; close(DATABASE); return $number; } ###################################################################### +####### ## ## Preview Data Section ## ###################################################################### +###### ############### sub preview ######## sub preview{ print "<html> <HR> <table border='0' width='100%'> <tr> <td width='100%'> <table border='0' width='100%'> <tr> <td width='100%'> <table border='0' width='100%'> <tr> <td width='22%'><font SIZE='+1'color='gray'><B>$month +$date, $year</B></font></td> <td width='78%'><font SIZE='+1'color='black'><B>$input +{'title'}</B></font></td> </tr> </table> </td> </tr> <tr> <td width='100%'><font SIZE='2'>$input{'message'}</font></td +> </tr> </table> </td> </tr> </table> <BR> <form action method='post'> <input type='hidden' name='action' value='add_data'> <input type='submit' value=' Publish new event '> <BR><INPUT type='button' value=' Continue Editing ' onClick='histo +ry.go(-1)'> </form> <BR> $numberoflines </html>\n"; } ###################################################################### +####### ## ## Add Data Section ## ###################################################################### +###### ############### sub add_data ######## sub add_data{ # Print out a content-type for HTTP/1.0 compatibility $addfile = "events.txt"; print "$month"; print $month; #print "We are in the add_data sub-routine<BR>\n"; # Open the file for appending open (OUT, ">>$addfile") or die "Can't open $addfile\n"; $number = $numberoflines; $message = $input{'message'}; $line = join '::', $number,$month,$date,$year,$title,$message; print OUT "$line\n"; close OUT; }
      This may help you uncover some problems and is generally a good idea to include in all your programs.
      #!/usr/local/bin/perl -w use strict;
        I'm bound to get flamed for this, but I get frustrated by folks who want to "help" by simply responding with use strict!, without bothering to explain why this is relevant . I'm under the impression that a number of Monks have been brainwashed into using it without really understanding what it does in particular situations (refs/vars/subs). Dominus wrote a rather good column, Use Strict warnings and diagnostics or die that helps to clear up the fog on this topic.

        Rather, I suggest you make sure to utilize the CGI::Carp module when developing any perl CGI. However, I also suggest that you only use fatalsToBrowser (in its default form) during development and testing... if you must leave it in there, please consider customizing the errors. Leaving the vanilla responses in there can provide more information to guests than you want to let loose.
        use CGI::Carp qw(fatalsToBrowser);
        When this routine is imported via use, fatal errors, such as those produced by die and confess, send error messages to the browser as well as the error log. A simple HTTP response is created with the error message, followed by a request to send mail to the web administrator.**

        -fp

        ** Perl In a Nutshell, Chapter 8, O'Reilly Publishers

        Update: Note about fatalsToBrowser customization.
      Where exactly does $month go from having a value to not having a value? You should add lines like the following until you pinpoint the exact line of code at which it's going bad:

      print "1: input-month: \"$input{'month'}\"<BR>\n"; ... print "1: month: \"$month\"<BR>\n"; ... print "2: month: \"$month\"<BR>\n"; ...
      Add them starting with the first time you reference $month, and then keep adding them until you get to one that doesn't have the value. Or, start in the middle somewhere and do a binary search.

      -Make sure to number them or make them unique in some other way so that you can see correlate the output to the code.
      -Although maybe not necessary here, quoting the value is useful in general when printing debugging html because you can clearly see extra spaces (newlines, etc.) in the value that get collapsed into a single space or ignored by the browser.

      Just adding these print statements and identifying where the value is lost should make it clear to you what's going wrong. If it doesn't, then post the new code and note exactly where the value disappears and it might be clear to someone else here. It seems like such an ugly and stupid way of debugging but simple print statements are often the quickest way to find bugs of this sort.

      Short answer: Your problem is here:
      # Read in all the variables set by the form CGI::ReadParse(*input); $month = $input{'month'}; $date = $input{'date'}; $year = $input{'year'};
      I don't know why not, but ReadParse is not working as you want it to in this instance. As a result $input{month} is undefined and so $month is too.

      Since you're using CGI, and you know how to pull parameters out, I'd suggest you replace this block of code with something more like:

      my $month = $q->param('month'); my $date = $q->param('date'); my $year = $q->param('year'); my $title = $q->param('title'); my $message = $q->param('message');
      This will fix the problem you've described (and make you aware of a lot more).

      Hope this helps.

      jarich

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://182673]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (17)
As of 2014-09-18 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls