Sponsored Content
Top Forums Shell Programming and Scripting Perl: Variable input via HTML Post 62175 by cbkihong on Saturday 12th of February 2005 10:10:06 AM
Old 02-12-2005
First, make sure you have a proper shebang for perl. /bin/perl? I rarely see perl installed in /bin, but if it's indeed there then forget it.

Modern perl scripters no longer craft hacks to read form data from standard input primitively. In those code, POST form data used to be read from <STDIN> according to CGI specification, from which the content in the form of &-separated key=value pairs are usually extracted with a combination of split() and regular expressions. You may still find examples with code like this. However, this is prone to errors and is no longer recommended. The Perl community has agreed on a common standard - the CGI module. If your tutorial doesn't mention the CGI module, it should be considered out of date already and you ought to cast some questioning glances on it.

Here I modified your script a little bit to put in the missing parts. The CGI module knows how to find out whether a POST or GET form is involved, and then read and extract form data with the appropriate method for you. No fiddling of those dirty things required anymore:

Code:
#!/usr/bin/perl

use CGI;

my $cgi = new CGI;
print $cgi->header('text/html');

if (defined $cgi->param('ok')) {
 my %params = $cgi->Vars;
 print "My name is: ${params{realname}}<br>";
 print "My email is: ${params{email}}<br>";
} else {
print <<ENDHTML;
<HTML>
<HEAD>
<TITLE>CGI Test</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="mycgi.pl">
name: <INPUT TYPE=TEXT NAME="realname"><BR>
email: <INPUT TYPE=TEXT NAME="email"><BR>
<INPUT TYPE=SUBMIT NAME="ok">
</FORM>
</BODY>
</HTML>

ENDHTML
}

Some perl people actually recommend you to use the CGI object exclusively to output the form completely with its API rather than hardcoding the HTML tags in print() statements, but I think this is just a matter of taste. Isolating form snippet in templates and construct form output dynamically with a template engine (such as Template Toolkit) is seen as an even better approach.

Just put the script (mycgi.pl) in your Web server, give it executable chmod, and that should work. It should work if you have a reasonably recent version of CGI. It should be, if your Perl version is recent enough. The CGI module is shipped with Perl by default.

Last edited by cbkihong; 02-12-2005 at 11:00 PM.. Reason: use $cgi->header() instead of legacy way
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl and html

hi. im very new to perl. is it possible to fill up a web form and submit it using perl? example, i would like to sign up for a yahoo account though a perl script (ofcourse, granting the "type the characters as shown in imgage" is absent)? (8 Replies)
Discussion started by: marcpascual
8 Replies

2. Shell Programming and Scripting

HTML parsing by PERL

i have a HTML report file..its in attachment(a part of the whole report is attached..name "input html.doc").also its source is attached in "report source code.txt" i just want to seperate the datas like in first line it should be.. NHTEST-3848498958-NHTEST-10.2-no-baloo a and so on for whole... (3 Replies)
Discussion started by: avik1983
3 Replies

3. Shell Programming and Scripting

Pulling Input from HTML into Shell

I am trying to add to a Web page that I have, the ability to have users make requests as long as they key in a password. Can anyone help me with validating the text from the text field on the Web page into UNIX? Please let me know if I need to be clearer. Thanks, (1 Reply)
Discussion started by: mosammey
1 Replies

4. Shell Programming and Scripting

Perl + Korn + HTML

I have a perl script that prints up the html code and executes a few korn scripts to populate the web code. Disclaimer === I can throw together some korn scripts pretty quick. This code changes pretty frequently. I don't know enough about perl to do everything I need. One day maybe I'll get... (4 Replies)
Discussion started by: i9300
4 Replies

5. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

6. Shell Programming and Scripting

Replacing variable values in html tags

Hi please help me with this . I have a file test.txt with following content $cat test.txt <td>$test</td> <h2>$test2</h2> and I have a ksh with following content $cat test.ksh #!/bin/ksh test=3 test2=4 while read line do echo $line done < test.html I am expecting the output as (4 Replies)
Discussion started by: panduandpavan
4 Replies

7. UNIX for Dummies Questions & Answers

HTML: Display contents of file using Variable

<tr><td bgcolor=#D7EBAD><b>Instructions :</b></td> <td>`cat temp.txt`</td></tr> Hi Experts, I have an requirement of displaying file contents in HTML mail , for which am using the above approach, Where as the output is kind of not as expected. If there are 5 lines in the file, in the... (4 Replies)
Discussion started by: scott_cog
4 Replies

8. Shell Programming and Scripting

XML variable for input in same input file

Dear All , i stuck in one problem executing xml .. i have input xml as <COMMAND name="ARRANGEMENT.WRITE" timestamp="0" so="initial"> <SVLOBJECT> <LONG name="CSP_PMNT_ID" val="-1"/> <MONEY name="CSP_CEILING" amount="0.0" currency="AUD"/> ... (6 Replies)
Discussion started by: arvindng
6 Replies

9. Shell Programming and Scripting

Input data of a file from perl into HTML table

Hi , I need an help in perl scripting. I have an perl script written and i have an for loop in that ,where as it writes some data to a file and it has details like below. cat out.txt This is the first line this is the second line. .....Now, this file needs to be send in mail in HTML... (2 Replies)
Discussion started by: scott_cog
2 Replies

10. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies
HTML::ElementRaw(3pm)					User Contributed Perl Documentation				     HTML::ElementRaw(3pm)

NAME
HTML::ElementRaw - Perl extension for HTML::Element(3). SYNOPSIS
use HTML::ElementRaw; $er = new HTML::ElementRaw; $text = '<p>I would like this &nbsp; HTML to not be encoded</p>'; $er->push_content($text); $h = new HTML::Element 'h2'; $h->push_content($er); # Now $text will appear as you typed it, non-escaped, # embedded in the HTML produced by $h. print $h->as_HTML; DESCRIPTION
Provides a way to graft raw HTML strings into your HTML::Element(3) structures. Since they represent raw text, these can only be leaves in your HTML element tree. The only methods that are of any real use in this degenerate element are push_content() and as_HTML(). The push_content() method will simply prepend the provided text to the current content. If you happen to pass an HTML::element to push_content, the output of the as_HTML() method in that element will be prepended. REQUIRES
HTML::Element(3) AUTHOR
Matthew P. Sisk, <sisk@mojotoad.com> COPYRIGHT
Copyright (c) 1998-2010 Matthew P. Sisk. All rights reserved. All wrongs revenged. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
HTML::Element(3), HTML::ElementSuper(3), HTML::Element::Glob(3), HTML::ElementTable(3), perl(1). perl v5.10.1 2010-06-09 HTML::ElementRaw(3pm)
All times are GMT -4. The time now is 07:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy