Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I save the result of POST to a file?

by Anonymous2003 (Initiate)
on Jul 30, 2003 at 06:48 UTC ( #279092=perlquestion: print w/replies, xml ) Need Help??

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

How can I take the information from a html form(post method, action=script) and save it to a text file? My perl version(5.004_04 for solaris) doesn't recognize "use"!Thank you

update (broquaint): title change (was text file)

  • Comment on How do I save the result of POST to a file?

Replies are listed 'Best First'.
Re: How do I save the result of POST to a file?
by arthas (Hermit) on Jul 30, 2003 at 07:13 UTC

    You can use require to emulate use. To get the same behaviuor as use Module you need to say:

    BEGIN { require MODULE; import MODULE LIST; }

    To get the same behaviour as use MODULE () you need to say:

    BEGIN { require MODULE; }

    This way you should be able to use cpan:://CGI module. If the module requires a more recent version of Perl, try looking for an older version, or for cgi-lib.pl, which is not maintained anymore but is still available around the web.

    Hope this helps!

    Michele.

Re: How do I save the result of POST to a file?
by ChrisR (Hermit) on Jul 30, 2003 at 15:10 UTC
    I agree that an upgrade of Perl would be the first step. However, in some instances, upgrades and loading of modules may not be allowed. In that case, I have a couple of routines that may help out. The first will simply get the form parameters into a string which you can then manipulate yourself:
    sub GetParams { my $webparams; if ($ENV{'QUERY_STRING'} ne '') { $webparams = "$ENV{'QUERY_STRING'}"; } else { read(STDIN, $webparams, $ENV{'CONTENT_LENGTH'}); } return $webparams; }
    This one is a bit more complex. It will return a hash where the keys are the names of the inputs on the html form and the values are, of course, the values of the inputs. If you have things like multi-select list boxes, you should choose to return a hash of arrays. The data will then be accessed like:
    $hash{input_name}[0] $hash{input_name}[1]
    To find out how many items were returned for a given key, use:
    my $numitems = $#{$hash{input_name}};
    This routine takes one or two parameters which are specified in the code:
    sub GetParamsHash { my $type = shift; # type = 0 or not included returns a hash # type = 1 returns a hash of arrays my $webparams = shift; my $webparams2; my %webparams3; if(!$webparams) { if ($ENV{'QUERY_STRING'} ne '') { $webparams = "$ENV{'QUERY_STRING'}"; } else { read(STDIN, $webparams, $ENV{'CONTENT_LENGTH'}); } } if($type == 1) { $webparams2 = $webparams; $webparams2 =~ s/\&/\n/g; $_ = $webparams2; while(/^(.*?)=(.*)$/gm){ push @{$webparams3{$1}}, $2; } } else { $webparams2 = $webparams; $webparams2 =~ s/\&/\n/g; $_ = $webparams2; %webparams3 = /^(.*?)=(.*)$/gm; } return %webparams3; }
    One important thing to remember is that the data sent from the html form may be encoded. I have another very simple routine that is quite helpful in decoding this data:
    sub DecodeURL { my $text = shift; $text =~ tr/\+/ /; $text =~ s/%([a-f0-9][a-f0-9])/chr(hex($1))/eig; return $text; }
Re: How do I save the result of POST to a file?
by nite_man (Deacon) on Jul 30, 2003 at 07:11 UTC

    If I understood clearly you cannot use module CGI. If it is, there is a one way to get form data and store them:

    if($ENV{'REQUEST_METHOD'} eq 'GET') { $query=$ENV{'QUERY_STRING'} } elsif($ENV{'REQUEST_METHOD'} eq 'POST') { sysread(STDIN,$query,$ENV{'C +ONTENT_LENGTH'}) } else { die 'Unsupported method: $ENV{'REQUEST_METHOD'}' } # Now $query consists a string with pairs name=value which # are sticked '&' $query =~ s/&/\n/g; open(FILE, '/your/file') or die 'Cannot open file: $!'; print FILE $query; close FILE;
    Hope I helped.
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
      the pb is that it does'nt recognize neither 'sysread' nor 'readparse' which were the two solution I knew! here the beginning of my form: <form method=\"post\" action="cible.pl"> where cible.pl is a script that will use the answers of the form...

        Well, you can get form data from STDIN:

        my $query = <>;
        because all form data, which are sent using method POST, come to the STDIN of handled script.
              
        --------------------------------
        SV* sv_bless(SV* sv, HV* stash);
        
Re: How do I save the result of POST to a file?
by talexb (Chancellor) on Jul 30, 2003 at 14:37 UTC
    • If possible, upgrade your version of Perl to 5.8. The version you are using is about five years old.
    • You may not be able to use CGI because it was never installed. Running
      perl -MCGI -de 1
      should tell you whether CGI.pm is installed or not. Check with the System Administrator if in doubt.
    • What do you mean when you say sysread and BEGIN aren't recognized? You need to present code examples for us to even begin to help you.

    This forum helps people with Perl problems. CGI and HTML are topics that may be Perl-related and may not be.

    --t. alex
    Life is short: get busy!
Re: How do I save the result of POST to a file?
by Anonymous2003 (Initiate) on Jul 30, 2003 at 14:40 UTC
    Here is what I've tried:
    #!/agl/tools/perl/bin/perl if($ENV{'REQUEST_METHOD'} eq 'GET') { $query=$ENV{'QUERY_STRING'} } elsif($ENV{'REQUEST_METHOD'} eq 'POST') { sysread(STDIN,$query,$ENV{'CONTENT_LENGTH'}) } else { die 'Unsupported method: $ENV{'REQUEST_METHOD'}' } # Now $query consists a string with pairs name=value which # are sticked '&' $query =~ s/&/\n/g; open(FILE, '/home/testuser3:choicebug.txt') or die 'Cannot open file: +$!'; print FILE $query; close FILE;

    edited by ybiC: <code> tags enclosing code sample

      1. Go to CPAN.
      2. Read the documentation for CGI.
      3. Try some of the examples.
      4. Try your form again.

      In future, please wrap your code examples inside code tags. This means we don't get your code smooshed into one big line.

      --t. alex
      Life is short: get busy!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://279092]
Approved by arthas
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2022-10-04 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (15 votes). Check out past polls.

    Notices?