homepage Welcome to WebmasterWorld Guest from 54.211.230.186
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Perl to CGI
medranoenrique

5+ Year Member



 
Msg#: 3716999 posted 8:18 pm on Aug 6, 2008 (gmt 0)

is there a way to convert a Perl script into CGI

hears is the scrit im using

#!/usr/bin/perl -w

print "whats your name?\n";
$name = <STDIN>;
sleep (2);
print "nice to meet you \n";
sleep (2);
$n1 = substr($name,0,1);
$n2 = substr($name,1,1);
$n3 = substr($name,2,1);
$n4 = substr($name,3,1);
$n5 = substr($name,4,1);
$n6 = substr($name,5,1);
$n7 = substr($name,6,1);
$n8 = substr($name,7,1);
$n9 = substr($name,8,1);
$n10 = substr($name,9,1);
$n11 = substr($name,10,1);
$n12 = substr($name,11,1);
$n13 = substr($name,12,1);
$n14 = substr($name,13,1);
$n15 = substr($name,14,1);
$n16 = substr($name,15,1);
$n17 = substr($name,15,1);
$n18 = substr($name,16,1);
$n19 = substr($name,17,1);

print "$n1 $n2 $n3 $n4 $n5 $n6 $n7 $n8 $n9 $n10 $n11 $n12";
print "$n13 $n14 $n15 $n16 $n17 $n18 $n19 \n\n";

my $now = gmtime;
print "it is $now\n in greenwich england which is \nfive hour

 

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3716999 posted 11:40 pm on Aug 6, 2008 (gmt 0)

a CGI script returns a HTTP Response document and gets its input from the parameters of a HTTP GET Request or from the data embedded in a HTTP POST Request.

your best option is to use the CGI perl module.
module download and documentation:
[search.cpan.org...]

perl_diver

5+ Year Member



 
Msg#: 3716999 posted 12:47 am on Aug 7, 2008 (gmt 0)

Yea, use the CGI module, it comes with all standard distributions of perl. Keep in mind, when you write CGI scripts, the output will more than likely involve html code, so you have that to learn too. The CGI module handles HTML generation but I still recommend you learn HTML and CSS, which are pretty simple unless you want to do some really fancy stuff. CGI security is also very important.

medranoenrique

5+ Year Member



 
Msg#: 3716999 posted 7:05 pm on Aug 7, 2008 (gmt 0)

what do you do with the tar.gz file that i downloaded

Also what dose this error message mean

"Global symbol "$tm2" requires explicit package name at Tm line 54."

the error message appers for all of my variables $tm2 is one of them

perl_diver

5+ Year Member



 
Msg#: 3716999 posted 8:22 pm on Aug 7, 2008 (gmt 0)

This is going to be a very poor vehicle for teaching you perl. Forums are good for helping with problems or basic concepts, but you need to learn perl judging by what you have posted.

The program you are running is compiled with "strict", which means all variables have to be properly declared with "my" or "our" or possibly "vars". Look up the "strict" pragma in the perl documentation or search online.

As far as the tar.gz, file, I have no idea what you are asking.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3716999 posted 9:51 pm on Aug 7, 2008 (gmt 0)

that tar.gz is called a tarball which is a compressed archive format.
usually something like this will work:
tar zxf filename.tar.gz

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3716999 posted 1:50 am on Aug 8, 2008 (gmt 0)

I cannot imagine a perl installation today that doesn't have the CGI module installed. So you can probably delete that .tar.

Here is what they are telling you:

You are using STDIN input to supply data to your program. On the web, this is done by a) submitting form data, and b) parsing out the submitted data to supply it to your program. This is what the CGI module does. This process is called Read and Parse: read in the encoded input stream and parse it out into key/value pairs.

Here's a working web-ified example of your command line program. Compare it with yours and you'll see what it does. Four caveats:

- I changed "name" to "your_name" to avoid any conflicts with HTML documents; forms have an attribute called "name" (which is the "key" for the input data.) If it doesn't confuse the browser, it will confuse the newbie. :-)

- You will see this

qq

Look up qq in the perl documentation; basically it means print, or store, everything from here to here . Note that this board software "breaks" this into a double pipe. All you should have to change if you copy and paste is change these to the single vertical pipe on your keyboard, just above your enter key. There are four of them in this script.

- When developing from the web, you ALWAYS have to have a content-type header followed by two newlines before anything else prints or it will give you a server error. This is distinctly different than your command line program that just prints to STDOUT.

print "content-type:text/html\n\n";

- The permissions must be set to executable for the script to run via the web: chmod 755 [filename]. Upload it, set the permissions, go to your browser and request the file.

Welcome to perl. :-)

#!/usr/bin/perl

#this script is named cgitest.cgi
use CGI;
$cgi_query = new CGI;
$now = gmtime;

print "content-type: text/html\n\n";
if ($cgi_query->param('your_name') ne '') {

$your_name = $cgi_query->param('your_name');
$n1 = substr($your_name,0,1);
$n2 = substr($your_name,1,1);
$n3 = substr($your_name,2,1);
$n4 = substr($your_name,3,1);
$n5 = substr($your_name,4,1);
$n6 = substr($your_name,5,1);
$n7 = substr($your_name,6,1);
$n8 = substr($your_name,7,1);
$n9 = substr($your_name,8,1);
$n10 = substr($your_name,9,1);
$n11 = substr($your_name,10,1);
$n12 = substr($your_name,11,1);
$n13 = substr($your_name,12,1);
$n14 = substr($your_name,13,1);
$n15 = substr($your_name,14,1);
$n16 = substr($your_name,15,1);
$n17 = substr($your_name,15,1);
$n18 = substr($your_name,16,1);
$n19 = substr($your_name,17,1);

print qq¦
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Enter Your Name</title>
</head>
<body>
<p>Nice to meet you $your_name.</p>
<p>$n1 $n2 $n3 $n4 $n5 $n6 $n7 $n8 $n9 $n10 $n11 $n12</p>
<p>$n13 $n14 $n15 $n16 $n17 $n18 $n19</p>
<p>it is $now\n in greenwich england.</p>
</body>
</html>
¦; #end print qq
}

else {
print qq¦
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Enter Your Name</title>
</head>
<body>
<form method="post" action="cgitest.cgi">
<p>What's your name?</p>
<p><input type="text" name="your_name" id="your_name" value=""></p>
<p><input type="submit" value="Submit"></p>
<p>it is $now\n in greenwich england.</p>
</body>
</html>
¦;
}

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3716999 posted 1:59 am on Aug 8, 2008 (gmt 0)

One other thing: in perl the cliche is TMTOWTDI (There's more than one way to do it.) After you get that running, swap out the relevant section with this. It creates an array (list) of all the letters and does exactly the same thing, without all the extra variables, and allows the length of "your_name" to exceed 19.

if ($cgi_query->param('your_name') ne '') {

$your_name = $cgi_query->param('your_name');
## let's create an array instead of creating all those variables
@letters = split('',$your_name);
print qq¦
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Enter Your Name</title>
</head>
<body>
<p>Nice to meet you $your_name.</p>
<p>
¦;
foreach $l (@letters) { print "$l "; }
print qq¦
</p>
<p>it is $now\n in greenwich england.</p>
</body>
</html>
¦; #end print qq
}

lexipixel

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3716999 posted 3:47 am on Aug 8, 2008 (gmt 0)

This appears to be another incarnation of the same "how do I write a Perl / CGI script" posted and rehashed by RudyS who wrapped up the last 40 post thread on the subject by saying medranoenrique had been using his login for the summer...

RudyS, medranoenrique, whoever:
Refer back to [webmasterworld.com...] thread.. there are PERL / CGI examples there "you guys" said you got running.

Also same problem more or less:
[webmasterworld.com...]

perl_diver

5+ Year Member



 
Msg#: 3716999 posted 5:52 am on Aug 8, 2008 (gmt 0)

rocknbil,

Just a suggestion, instead of:

qq¦text¦

you can use paired brackets:

qq{text}
qq[text]
qq(text)

and that way you don't have to escape the delimiter if it is used within the text. They also display better on this forum. I prefer the first example myslef using curly brackets {}.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3716999 posted 1:29 am on Aug 9, 2008 (gmt 0)

Aye cap'n, habit I guess, and that the pipe is the least likely of characters to EVER appear in any normal text. Although most perl coders avoid the pipe like the plague, in the context of qq it's not harmful and is less likely to create an escaping issue as it's always filtered out of input.

medranoenrique, it's probably best to form "good habits" from the start and heed this recommendation.

lexipixel

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3716999 posted 8:16 am on Aug 10, 2008 (gmt 0)

habit I guess, and that the pipe is the least likely of characters to EVER appear in any normal text
-rocknbil

I've got dozens of scripts online that run against pipe delimited ASCII "data bases"... I'm so set in my ways I still use-

print 'Whatever<br>\n';

or

#
$s = 'ever';
print "What$s<br>\n";
#

and consider it daring to use-

#
$lf = '<br>\n';
$s = 'ever';
printf "What%s%s", $s, $lf;
#

...it took me long enough to get used to not using line numbers when I stopped using BASIC.

Gotta try that qq thingy some day.. :-)

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3716999 posted 10:29 pm on Aug 10, 2008 (gmt 0)

I love qq. :-)

This

$s = 'ever';
print "What$s<br>\n";

can be

print qq[What's all this "whatever" I hear about?\n];

It makes it much easier to output huge blocks with less errors, and all your variables will still interpolate.

medranoenrique

5+ Year Member



 
Msg#: 3716999 posted 7:14 pm on Aug 12, 2008 (gmt 0)

i have a question about something that was given to me by RudyS and it was given to him a long time ago i think and he was given this

#!/usr/local/bin/perl

use CGI;
$query = new CGI;

$var1 = ($query->param("var1"));
$lcseq = lc($var1);
chomp ($lcseq);
$length = length($lcseq);
$SCRIPT_uri = $ENV{'SCRIPT_URI'};

i would like to know what it means

anybody who help thanks ;)

perl_diver

5+ Year Member



 
Msg#: 3716999 posted 9:27 pm on Aug 12, 2008 (gmt 0)

You were directed at the beginning of this thread by phranque to read the CGI module documentation. You seem to have ignored that suggestion.

Since I am of the opinion that forums are a poor place to try and teach someone a programming language, I am dropping out of this thread.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3716999 posted 11:51 pm on Aug 12, 2008 (gmt 0)

that script doesn't really "do" anything.

new and param are CGI methods:
[search.cpan.org...]
[search.cpan.org...]

lc, chomp and length are basic perl functions:
[perldoc.perl.org...]

ENV is a special perl variable - a hash containing all environment variables:
[perldoc.perl.org...]

as perl_diver suggested, you should really read up on the basics.
you have done NO preliminary work!

from the Perl Forum Charter [webmasterworld.com]:
Do My Homework posts are not all that welcome.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3716999 posted 11:57 pm on Aug 12, 2008 (gmt 0)

<sigh> :-) Patience is a virtue, for I too was once lost with all this (and still am many times . . . )

The first one is the call to the perl interpreter. It tells the CGI gateway where to look to execute perl.

#!/usr/local/bin/perl

This means to include the CGI module for it's use in this script.

use CGI;

This creates a CGI object that will hold all the stuff submitted by the form and store it in the variable "$query.".

$query = new CGI;

This next one requires knowledge that a form exists somewhere that has this field:
Var1: <input type="text" name="var1" value="">

var1 is the "key" for whatever value is entered into this field.

So what is happening here is you are telling your CGI object to extract the value submitted for the form field var1 and store that value in a perl variable, $var1.

$var1 = ($query->param("var1"));

This says to take anything in that value and convert it to lower case. Store the result in $lcseq.

$lcseq = lc($var1);

(Actually this is an extra step. You can just use the original variable $var 1, like $var1 = lc($var1);)

For whatever reason it's being done, this next one does what it says - chomps off the last character of $lcseq. Usually this is only used when reading in text files to chomp off newlines (carriage returns.) Not sure why it would be used here, unless the form field is a textarea.

chomp ($lcseq);

This next one gives a numeric length for $lcseq (number of characters.) For example, if I do this:

$rtfm = "read the manual!";
$len = length($rtfm);

The value of $len should be 16.

$length = length($lcseq);

This next one takes an environment variable, the URI of the script, and stores it in the variable $SCRIPT_uri. There are many envars, all available to any perl web program, besides this is one. Google for environment variables for more info.

$SCRIPT_uri = $ENV{'SCRIPT_URI'};

Sooooo . . . . what this chunk of code does is read and parse the value of some form with a field named var1, does some trivial stuff with it . . . and that's about it, it doesn't really do . . . anything . . . .

lexipixel

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3716999 posted 1:49 am on Aug 13, 2008 (gmt 0)

i have a question about something that was given to me by RudyS and it was given to him a long time ago i think and he was given this
-medranoenrique

"Give a man a perl script and he processes data for a day,
teach a man perl and he writes code for a lifetime."
- a wise old scripter

A parting bit of advice for medranoenrique and RudyS --- take a copy of The Perl Cookbook, (by Nathan Torkington), out of the library and start at Page 1... By the time you get through the book you will know perl.

lexipixel

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3716999 posted 2:43 am on Aug 13, 2008 (gmt 0)


$len = length($rtfm);
The value of $len should be 16.
-rocknbil

My interpreter returned a longer length to the $rtfm variable --- you seem to have used the "kinder gentler" $rtm type variable -- possibly you left out a regular expression? e.g.-

#
$rtm = $rtfm;
$rtm =~ s/f(rigg¦uck¦rig)//;
$rtm =~ s/(ing¦in'¦in)//;
#

(pipe fix as usual)..

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3716999 posted 6:39 am on Aug 13, 2008 (gmt 0)

Ahhh . . . I stand corrected, again! But nay, that was a social filter, not programming. :-P

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3716999 posted 6:42 am on Aug 13, 2008 (gmt 0)

thanks for the lesson in patience, rocknbil.

@lexipixel:
"Give a man a perl script and he processes data for a day,
teach a man perl and he writes code for a lifetime."
- a wise old scripter

"Give a man a fish, and you feed him for a day. Teach a man to fish, you feed him for a life time."
- the Perl Forum Charter [webmasterworld.com]

medranoenrique

5+ Year Member



 
Msg#: 3716999 posted 3:46 pm on Aug 13, 2008 (gmt 0)

I finished my work and i got something working online...
Thanks to everyone who helped me out ... Below is my finial work. That works on the web.

~~Enrique Medrano~~

#!/usr/local/bin/perl

use CGI;
$query = new CGI;

$var1 = ($query->param("var1"));
$lcseq = lc($var1);
chomp ($lcseq);
$oligolength = length($lcseq);
$SCRIPT_uri = $ENV{'SCRIPT_URI'};

$tm = 0;
$charpos = 0;
until($charpos == $oligolength)
{
$pair = substr($lcseq, $charpos, 2);
if ($pair eq "aa"){$tm += 2.116;}
if ($pair eq "ac"){$tm += 3.068;}
if ($pair eq "ag"){$tm += 2.750;}
if ($pair eq "at"){$tm += 1.862;}
if ($pair eq "ca"){$tm += 3.068;}
if ($pair eq "cc"){$tm += 3.893;}
if ($pair eq "cg"){$tm += 4.591;}
if ($pair eq "ct"){$tm += 2.708;}
if ($pair eq "ga"){$tm += 2.750;}
if ($pair eq "gc"){$tm += 4.739;}
if ($pair eq "gg"){$tm += 3.893;}
if ($pair eq "gt"){$tm += 3.047;}
if ($pair eq "ta"){$tm += 1.227;}
if ($pair eq "tc"){$tm += 2.708;}
if ($pair eq "tg"){$tm += 3.047;}
if ($pair eq "tt"){$tm += 2.116;}
$charpos = $charpos +1;
}

$tm = $tm + 2.5;

print "Content-type: text/html\n\n";

print "<html>";
print "<head><title>tm calculator</title></head>";
print "<body>";

if ($var1 eq '') {
print "<form action=\"$SCRIPT_uri\" method=\"POST\">";
print "Sequence(up to 14 characters, no spaces inbetween or after): <input type=\"text\" name=\"var1\">\n";
print "<input type=\"submit\" value=\"submit\">\n";
print "</form>";

} else {
print "your sequence is $length characters.\n";
print "the tm of your sequence is $tm";
}

print "</body>";
print "</html>";

exit;

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3716999 posted 10:36 pm on Aug 13, 2008 (gmt 0)

bravo!

i would suggest one change:
if ($pair eq "aa"){$tm += 2.116;}
elsif ($pair eq "ac"){$tm += 3.068;}
.
.
.

perl_diver

5+ Year Member



 
Msg#: 3716999 posted 6:58 am on Aug 14, 2008 (gmt 0)

Some fool will send some gigantic response to that script and cripple your server. If you want them to send 14 characters you best check that before blindly processing the form data.

[edited by: phranque at 12:25 pm (utc) on Aug. 14, 2008]
[edit reason] tidying [/edit]

medranoenrique

5+ Year Member



 
Msg#: 3716999 posted 3:39 pm on Aug 15, 2008 (gmt 0)

ok but i took out the 14 limet cause it can do more then that but if the server overloads because some iddot but millions of characters then i'll put a limet.

perl_diver

5+ Year Member



 
Msg#: 3716999 posted 5:21 pm on Aug 15, 2008 (gmt 0)

The three rules of CGI scripting (no joke):

1. Never trust user input
2. Treat all user input like poison
3. See rule one and two

When you allow people to send data to your server that is used in a program, you better follow those three rules, because eventually some, uhh..... fool, will test the security of your script, by design or by accident, and then it might be too late.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved