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

I have the script which gives me a html table. From this table i select the info (checkbox) and I send it with the form:<form method=\"post\" action="xxx.pl">" Here is xxx.pl with which i trie to put the info in a file.But it doesn't work! Any help?
#!/agl/tools/perl/bin/perl $file = 'home/testuser3/choicebug.txt'; sub GetParams { my $webparams; if ($ENV{'QUERY_STRING'} ne '') { $webparams = "$ENV{'QUERY_STRING'}"; } else { read(STDIN, $webparams, $ENV{'CONTENT_LENGTH'}); } return $webparams; } open(FILE, ">>$file")||die "Can't open $file"; GetParams(); while (<$webparams>) { print $webparams; } close (FILE);
thank you very much for your time

Replies are listed 'Best First'.
Re: save form
by castaway (Parson) on Jul 31, 2003 at 10:20 UTC
    You aren't actually getting the return value from GetParams() anywhere, you need to do this at the end:
    my $webparams = GetParams(); print FILE $webparams; close(FILE);
    (instead of your while loop)

    I think you're a little confused on the scope of variables and how subs return data. Try reading up on that.

    C.

Re: save form
by ChrisR (Hermit) on Jul 31, 2003 at 13:05 UTC
    Try this:
    #!/agl/tools/perl/bin/perl use strict; my $file = 'home/testuser3/choicebug.txt'; my $webparams = GetParams(); open(FILE, ">>$file")||die "Can't open $file"; print FILE "$webparams\n"; close (FILE); exit; sub GetParams { my $webparams; if ($ENV{'QUERY_STRING'} ne '') { $webparams = "$ENV{'QUERY_STRING'}"; } else { read(STDIN, $webparams, $ENV{'CONTENT_LENGTH'}); } return $webparams; }

    Also, I would suggest that you get a copy of "Programming Perl" (the camel book) from O'Reilly. This is a must read for any serious perl programmer. There are many other books that will help but the camel book is the best way to start in my opinion.
Re: save form
by bobn (Chaplain) on Jul 31, 2003 at 12:59 UTC

    With while (<$webparams>), you're reading $webparams as if it were a filehandle, but the previous code makes that very unlikely.

    (If you were to do a warn $webparams, youre query would be printed into your error_log, so you could see how to parse it. But don't. Instead, use CGI; to see and parse the params, as it will do that for you and do it correctly.)

    Then, although you've created an output filehandle FILE, your print statement doesn't reference it at all, so assuming you print anythng at all, it's going to STDOUT, not to the file.

    --Bob Niederman, http://bob-n.com
Re: save form
by nite_man (Deacon) on Jul 31, 2003 at 11:23 UTC

    In additional, I'd suggest you always using strict. It will able to help you to reveal many uncertainties in your code!

    Also, I'd suggest you look at Perl documentation before you start to develop some Perl application. Belive me, it will save a lot of you time in the future!

    _ _ _ _ _ _
      M i c h a e l