Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get POST values in perl

Tags:

cgi

perl

I am trying to customize a script and need to get a POST value from a form using perl. I have no background of perl but this is a fairly simple thing so I guess it should not be hard.

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

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

I know this may not be at all related to the PERL version but it could help I guess clarifying what exactly I am looking to do.

like image 809
Ahoura Ghotbi Avatar asked Jun 18 '12 18:06

Ahoura Ghotbi


2 Answers

Well, in that case please have a look at this simple code: This would 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;
}
like image 132
vijay Avatar answered Sep 22 '22 10:09

vijay


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


In CGI.pm (CGI), the param method merges POST and GET parameters, so we need to inspect the request method separately:

#!/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.';

With Plack::Request (PSGI), you have different methods for POST (body_parameters) and GET (query_parameters) in addition to the mixed interface (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.']];
};
like image 34
daxim Avatar answered Sep 22 '22 10:09

daxim