How to get POST values ​​in perl

I am trying to customize a script and you need to get the POST value from the form using perl. I don't have a perl background, but this is a pretty simple thing, so I think it should not be difficult.

This is the PHP version of the code I would like to have in PERL:

<?php
$download = ($_POST['dl']) ? '1' : '0';
?>

I know that this may not be at all related to the PERL version, but it can help me guess what I'm doing exactly.

+5
source share
4 answers

Well, in that case, please take a look at this simple code: this will help you:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

sub output_top($);
sub output_end($);
sub display_results($);
sub output_form($);

my $q = new CGI;

print $q->header();

# Output stylesheet, heading etc
output_top($q);

if ($q->param()) {
    # Parameters are defined, therefore the form has been submitted
    display_results($q);
} else {
    # We're here for the first time, display the form
    output_form($q);
}

# Output footer and end html
output_end($q);

exit 0;

# Outputs the start html tag, stylesheet and heading
sub output_top($) {
    my ($q) = @_;
    print $q->start_html(
        -title => 'A Questionaire',
        -bgcolor => 'white');
}

# Outputs a footer line and end html tags
sub output_end($) {
    my ($q) = @_;
    print $q->div("My Web Form");
    print $q->end_html;
}

# Displays the results of the form
sub display_results($) {
    my ($q) = @_;

    my $username = $q->param('user_name');
}

# Outputs a web form
sub output_form($) {
    my ($q) = @_;
    print $q->start_form(
        -name => 'main',
        -method => 'POST',
    );

    print $q->start_table;
    print $q->Tr(
      $q->td('Name:'),
      $q->td(
        $q->textfield(-name => "user_name", -size => 50)
      )
    );

    print $q->Tr(
      $q->td($q->submit(-value => 'Submit')),
      $q->td('&nbsp;')
    );
    print $q->end_table;
    print $q->end_form;
}
+6
source

Style tip: you almost never need to assign 0 or 1 to a variable. Just evaluate the value in the bool context.


CGI.pm(CGI) param POST GET,

#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use CGI qw();
my $c = CGI->new;
print $c->header('text/plain');
if ('POST' eq $c->request_method && $c->param('dl')) {
    # yes, parameter exists
} else {
    # no
}
print 'Do not taunt happy fun CGI.';

Plack:: Request (PSGI) POST (body_parameters) GET (query_parameters) (parameters):

#!/usr/bin/env plackup
use strict;
use warnings FATAL => 'all';
use Plack::Request qw();
my $app = sub {
    my ($env) = @_;
    my $req = Plack::Request->new($env);
    if ($req->body_parameters->get_all('dl')) {
        # yes
    } else {
        # no
    }
    return [200, [Content_Type => 'text/plain'], ['Do not taunt happy fun Plack.']];
};
+4

: The Fool Guide to CGI.pm, Perl CGI.

: "... POST ( ) ".

, !

0

. POST . Key. GET, CGI.

#!/usr/bin/perl

my $FormData = '';
read(STDIN, $FormData, $ENV{'CONTENT_LENGTH'});

## Variable $FormData holds all POST values passed

use CGI;
my $cgi = new CGI;
print $cgi->header();
print "$FormData";
0

All Articles