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

Well... the newbie is back. I'm using strict now, and surprise, surprise, it has generated numerous errors.
1. I'm trying to test if the user has hit the submit button yet, so, when the page first loads the value for if ($query->param('Action') eq 'Submit') is undefined. However, i get an uninitialized variable error. The code for this section:
my $query = new CGI; $query->param('Action') = "nothing yet"; #PRINT HEADER print $query->header; print $query->start_html( -title=>'Add to the Cancer Genomics Online Library'); # -script=>$JSCRIPT); #PRINT TITLE &print_title; #PRINT FORM print "<h2>Cancer Genomics Online Library Addition Form</h2>\n"; if ($query->param('Action') eq 'Submit') { # we have some data print "<h2>Entry Results</h2>\n"; # check data if (&check_entry($query) == 0) { &print_success($query); &make_entry($query); } else { print "<HR><H2>Errors Found</H2>\n"; print "<P>Please correct the errors above on the form below.\n +"; &print_form($query); } } else { &print_intro; &print_form($query); } &print_tail; print $query->end_html; #End of Program
How can I set $query->param('Action') to something?
2. This is really dumb, but I can't seem to create a file, open it, and then write into it. I keep getting ...print on closed filehandle main::PUBFILE ...
sub make_entry { my($query) = @_; my $PUBFILE = "temp.html"; open PUBFILE, ">$PUBFILE"; select STDOUT; my $pubtitle = $query->param('pubtitle'); my $pubauthor = $query->param('pubauthor'); my $pubdate = $query->param('pubdate'); my $pubjournal = $query->param('pubjournal'); my $pubcategory = $query->param('pubcategory'); my $puburl = $query->param('puburl'); my $pubkey1 = $query->param('pubkey1'); my $pubkey2 = $query->param('pubkey2'); my $pubkey3 = $query->param('pubkey3'); my $pubkey4 = $query->param('pubkey4'); my $pubcomments = $query->param('pubcomments'); #my $pubupload my $yourname = $query->param('yourname'); my $youremail = $query->param('youremail'); my $yourphone = $query->param('yourphone'); print PUBFILE <<END; <!-- \n $pubtitle \n $pubauthor \n $pubcategory \n $pubkey1 &amp;amp;amp;amp;nbsp $pubkey2 &amp;amp;amp;amp;nbsp &amp +;amp;amp;amp;pubkey3 &amp;amp;amp;amp;nbsp \n --> \n <table>\n <tr><td>Title: </td><td>$pubtitle</td><tr>\n <tr><td>Author: </td><td>$pubauthor</td><tr>\n <tr><td>Publish Date: </td><td>$pubdate</td><tr>\n <tr><td>Journal: </td><td>$pubjournal</td><tr>\n <tr><td>Category: </td><td>$pubcategory</td><tr>\n <tr><td>URL: </td><td><a href="$puburl">$puburl</a></td><tr>\n <tr><td>Keywords: </td><td>$pubkey1 &nbsp $pubkey2 &nbsp $pubk +ey3 &nbsp $pubkey4</td><tr>\n <tr><td>Comments: </td><td>$pubcomments</td><tr>\n <tr><td>Posted: </td><td></td><tr>\n </table> <table> <tr><td>Name of Poster: </td><td>$yourname</td><tr>\n <tr><td>Email of Poster: </td><td>$youremail</td><tr>\n <tr><td>Daytime Phone of Poster: </td>$yourphone<td></td><tr>\ +n </table> END close PUBFILE; }
Any ideas why?
Thanks... thanks for all of you guys who are answering and putting up with my dumb questions. It's saving my sanity!

Replies are listed 'Best First'.
(jeffa) Re: using strict; lots more errors
by jeffa (Bishop) on May 10, 2001 at 09:18 UTC
    You set the variable of a parameter like so:
    $query->param('Action','nothing yet'); # or $query->param(-name=>'Action', -value=>'nothing yet');
    see CGI for more details. But don't set this value. The problem is that you are trying to compare it to some value - don't do that. If the parameter you are looking for is not there (as is the case with 'Action' the first time the form is loaded), then param() will return undefined. If you try to compare that to a value, you will get a warning - not fatal, just annoying.

    Let's say that your submit tag looks like this:

    <input type="submit" name="Action">
    Then you can see if the user clicked that button like so:
    if ($query->param('Action')) { # do stuff }
    no need to check it's value if there is only one button. The mere existance of the query string variable 'Action' is enough to pass the test.

    A lot of times, I find myself having a CGI script that really only needs one variable from the user, then I use something like this:

    if (my $foo = $query->param('foo')) { #do stuff with $foo }
    If you can, please persuse the documention for CGI, also Ovid has a great tutorial, and merlyn has a ton of tricks free of charge at his site.

    Now then about the why you get the 'print on closed filehandle' error question - you really should catch your errors by the way:

    open PUBFILE, ">$PUBFILE" || die "do i have permission? $!";
    Because otherwise you will not know why the open failed. You will also want to add this line at the beginning of your script, say just right below use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    this will allow you to see the output of the die in the browser. Another good way to look for CGI run time errors is by 'tailing' the error log file. Each die is systematically logged behind the scenes to the web server's error log file.

    So, why the error? I am just guessing here, but 9 out of 10 reasons why you can't create a file from a CGI script is because of permissions. Take a look at this directory listing of ~jeff/public_html/wooden/

    drwxrwxr-x 2 jeff jeff 4096 Apr 3 01:37 ./ drwxr-xr-x 19 jeff jeff 4096 May 8 23:46 ../ -rw-rw-r-- 1 jeff jeff 11781 Mar 31 01:10 back.jpg -rw-rw-r-- 1 jeff jeff 2078 Mar 31 01:11 bar.jpg -rw-rw-r-- 1 jeff jeff 274 Apr 3 01:37 index.html
    the first line tells the permission for directory wooden - user jeff can create files in it, group jeff can create files in it, but nobody else can (no pun intended). The web server runs as a user - this user is set by the administrator, and usually something like 'nobody' or 'www' or 'www-data'. Not 'jeff'.

    To fix this, you either need to change the group to be the same as the one the web server uses (have to have root access to do this - talk to your admin), or set the permissions of your directory where the files are to be created to 777 (via chmod), writeable by every one ON THAT COMPUTER, not the web.

    whew, hope this helps ;)

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
      open PUBFILE, ">$PUBFILE" || die "do i have permission? $!";
      I think you mean this instead: open PUBFILE, ">$PUBFILE" or die "do i have permission? $!"; Or at least this: open (PUBFILE, ">$PUBFILE") || die "do i have permission? $!"; buckaduck
        DOH!

        i am going to leave it just to show how much it pays to pay attention

        jeff "that should be a new feature so i am correct" a

(arturo) Re setting default values
by arturo (Vicar) on May 10, 2001 at 17:50 UTC

    All I have are a couple of neat coding tips.

    First, here's a neat Perlish way of setting defaults:

    my $parameter_value = $query->param('Action') || 'default value';

    If the Action parameter isn't passed to the script, $query->param('Action') returns undef, which *evaluates to false* in perl (see What is true and false in Perl? for the rules) and thus $parameter_value gets the default value.

    You can also check to see whether a value is defined using defined $value.

    perl -e 'print "How sweet does a rose smell? "; chomp $n = <STDIN>; $r +ose = "smells sweet to degree $n"; *other_name = *rose; print "$other +_name\n"'
Re: using strict; lots more errors
by diarmuid (Beadle) on May 10, 2001 at 18:50 UTC
    Well I'm not too sure about 1. but if you only have one action in your form then you could just try
    if ( defined ($query->param('Action')))
    I'd like to have all the code and run it to see what's really happening bfore I'd know for sure

    For 2. You should really put and || die after the open statement to save yourself a lot of problemseg open(PUBFILE,">$pubfile") || die "Cant open $pubfile \n";