Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make this Perl script into CGI?????????????

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hello all, I'm very pleased with my new perl script, which is designed for the web.
However I have never migrated a perl script to CGI, and I was wondering if anyone knew how I would do it.
The script is fairly small and simple:
-----------------------------------------------
#!/usr/bin/perl -w

print &quot;\n\n username 2 create... &quot;; $username = <STDIN>; print &quot;\n&quot;;

chomp($username);
open (USERS, &quot;< C:/users.dat&quot;) || die;
@users = <USERS>;
close(USERS);
$size = $#users;

if ($username gt $users[$#users]) {
open (USERS, &quot;>> C:/users.dat&quot;) || die;
print USERS $username, &quot;#&quot;, $#users + 1, &quot;\n&quot;;
close(USERS);
}
else {
$x = 0;
until ($username le $users[$x]) {
$x++;
}

@half = (splice(@users, $x));
$username .= &quot;#&quot;; $username .= $size + 1; $username .= &quot;\n&quot;;
push (@users, $username);
push (@users, @half);
open (USERS, &quot;> C:/users.dat&quot;) || die;
print USERS @users;
close(USERS);
}
print &quot;\n\n password 2 create... &quot;; $password = <STDIN>; print &quot;\n&quot;;

$val = int(rand(59));
chomp($password);
$len = (length($password));
$z = 0;
open (PASS, &quot;>> C:/passwords.dat&quot;) || die &quot;can't open PASS file!\n&quot;;
until ($z == $len) {
$letter = substr($password, $z, 1);
$letter2 = $val * (ord($letter));
$z++;
print PASS $letter2, &quot;#&quot;;
}
print PASS $val, &quot;\n&quot;;
close(PASS);
-------------------------------------------------
Thankyou to anyone and everyone who responds. After I learn how the script changes for CGI I will be able to learn more and do it myself.
Thankyou very much!!!!!!
JoE we are all of us living in the gutter.
But some of us are looking at the stars.
 
You just need to wrap you output in a little http stuff
so the web server and browser can make sense out of it.
You need:
1 - a compliant header (Content-type.......) followed
by a blank line, ergo the two '\n' chars on the end.
2 - wrap you output in a little html.
3 - also remember the code will not run as 'you'. It will
run as the web server daemon. Therefore, any opening
of files or other such stuff must be doable for the
web server daemon (user).

Code:
#!/usr/bin/perl -w

# 1 - You must output a content-type line FIRST.
print &quot;Content-type: text/html\n\n&quot;;

# 2 - Now start some basic html to wrap your output
print qq(<html><head><title>simple CGI</title></head>
<body><pre>);
Code:
print &quot;\n\n username 2 create... &quot;; $username = <STDIN>; print &quot;\n&quot;;

chomp($username);
open (USERS, &quot;< C:/users.dat&quot;) || die;
@users = <USERS>;
close(USERS);
$size = $#users;

if ($username gt $users[$#users]) {
    open (USERS, &quot;>> C:/users.dat&quot;) || die;
    print USERS $username, &quot;#&quot;, $#users + 1, &quot;\n&quot;;
    close(USERS);
    }            
else {
    $x = 0;
    until ($username le $users[$x]) {
        $x++;
    }

@half = (splice(@users, $x));
$username .= &quot;#&quot;; $username .= $size + 1; $username .= &quot;\n&quot;;
push (@users, $username);
push (@users, @half);
open (USERS, &quot;> C:/users.dat&quot;) || die;
print USERS @users;
close(USERS);
}
print &quot;\n\n password 2 create... &quot;; $password = <STDIN>; print &quot;\n&quot;;

$val = int(rand(59));
chomp($password);
$len = (length($password));
$z = 0;
open (PASS, &quot;>> C:/passwords.dat&quot;) || die &quot;can't open PASS file!\n&quot;;
until ($z == $len) {
    $letter = substr($password, $z, 1);
    $letter2 = $val * (ord($letter));
    $z++;
print PASS $letter2, &quot;#&quot;;
    }
print PASS $val, &quot;\n&quot;;
close(PASS);

# 2 - close the html tags.
print qq(</pre></body></html>);
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top