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

Please advise why this wont print the variable $webpage1 with the value of "first"??

It doesnt print the value on my page or in the text file. The text file doesnt have any data in it. why doesnt this work??

use CGI qw(:all); print header; $webpage1 = param("first"); print "<html><head> <title>Web Pages for local data.</title> </head><body>"; $filepath = "C:/inetpub/wwwroot/fil.txt"; $newtext = "$webpage1\n"; open (FILE, ">>$filepath") || "Did not write:$!\n"; print FILE $newtext; close (FILE); print "<p>Data submitted.<br><b>$newtext</b></p>"; print "</body></html>";

Edited: ~Thu Aug 1 13:58:44 2002 (GMT) by footpad: Added HTML formatting tags, per Consideration.

Replies are listed 'Best First'.
Re: Wont write to text file.
by katgirl (Hermit) on Aug 01, 2002 at 10:50 UTC
    This might work better:
    #!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use CGI; my $req = new CGI; my $webpage1 = $req->param("first"); my $filepath = "C:/inetpub/wwwroot/fil.txt"; my $newtext = "$webpage1\n"; print $req->header, $req->start_html('Web Pages for local data.'); open (FILE, ">>$filepath") or die "Did not write:$!\n"; print FILE $newtext; close (FILE); print $req->p('Data Submitted'), $req->br($req->b('$webpage1')), $req->end_html;
Re: Wont write to text file.
by crenz (Priest) on Aug 01, 2002 at 10:52 UTC

    A couple observations:

    • Are you using use strict and use warnings? These are helpful in alerting you to undefined functions, variables etc.
    • param() only returns those variables that were sent via POST; so make sure you don't use GET in your HTML form (recognizable by URLs like http://server/myscript.cgi?q=CGI+programming) or use url_param()
    • Use a test script to see which parameters are passed to perl. See e.g. Dumping the CGI environment
    • Consider using CGI::Lite, http://search.cpan.org/search?dist=CGI-Lite, it's faster than CGI.pm
    • Look for a way to dump the names of all variables that were posted by the browser (does param() do that? I don't know, I don't use CGI.pm) -- maybe you just misspelt the name in the HTML file.
    • Your use of $newtext is not really necessary -- why not just print FILE "$webpage1\n"; and use $webpage1 in the print statement later

      param() does indeed return parameters from POST and GET. Since you don't use CGI.pm, please at least check the documentation if you're going to give advice about it.

        Thanks for the hint. I was not sure, so I read the POD documentation, but could find no reference to what happens if you just call param()

        Or maybe I shouldn't RTFM without having a cup of coffee beforehand ;-)

Re: Wont write to text file.
by ajt (Prior) on Aug 01, 2002 at 10:54 UTC

    Good to see you using CGI, but I think you'll find adding warnings and strict is always useful too. You can start warning by adding -w to the shebang line, or on more modern Perls by loading the warning module. Turn strict on by using it too. e.g.

    #!/usr/bin/perl -w use strict; use warnings;

    Looing at your code, and I assume it's a cut'n'paste, rather than a re-type, I'd say that you could clean up your open statment to something like:

    open FILE, ">>", $filepath or die "Unable to write to file\n$!";

    Secondly you are loading all of CGI, but then doing your own HTML, which is a bit of a waste. Either load only the bits of CGI you want (e.g. use CGI qw(:cgi); or take advantage of it's HTML printing. For example:

    print header; print start_html(-title => 'Web Pages for local data.'); ... print p("Data Submitted.", br, b($newtext)); print end_html;

    Depending upon which version of CGI you are running you should be able to start the script from the command line, and pass dummy variables to it, which can help with debugging.

    Another debugging tip from the docs is the Dump option, which will make CGI spit out in crude HTML all it's input parameters, so you can see if there is something funny going on, for example the param has a differnt name in the HTML form. See the CGI docs for more details.

    UPDATE: If you are going to use the OO interface for CGI, it's better to:

    my $q = CGI->new;

    rather than:

    my $q = new CGI;

    See davorg's comments in Re: (elbie): What does this warning mean? for an explanation.


    --
    ajt
Re: Wont write to text file.
by TStanley (Canon) on Aug 01, 2002 at 10:57 UTC
    Also, you might want to consider using taint checking when using the CGI module. NEVER trust user input!!

    WARNING: UNTESTED CODE!!
    #!/usr/bin/perl -wT use strict; use CGI; my $q=new CGI; my $webpage=$q->param('first'); if($webpage!~/\w+/){ print $q->header; print $q->start_html; print $q->h2('Incorrect parameter'); print $q->end_html; }else{ print $q->header; print $q->start_html; print $q->h2("Param first = $webpage"); print $q->end_html; }

    TStanley
    --------
    Never underestimate the power of very stupid people in large groups -- Anonymous
Re: Wont write to text file.
by valdez (Monsignor) on Aug 01, 2002 at 10:41 UTC

    Why aren't you creating a new CGI object and using its methods?

    Try this way:

    use CGI; $q = new CGI; $webpage1 = $q->param('first');

    Hope this helps. Ciao, Valerio

Re: Wont write to text file.
by cLive ;-) (Prior) on Aug 01, 2002 at 11:05 UTC

    Please advise why this won't print the variable $webpage1 with the value of "first"??

    Am I the only one who's reading this right?

    $webpage1 = 'first';

    If you are wanting to take the value of the form field submitted, then use param.

    .02

    cLive ;-)

    --
    seek(JOB,$$LA,0);

      Thanks to all who helped me with this.