CGI/PERL INTERACTIVITY

PERL is used in CGI sripts 99% of the time. Easiest way for interactivity. Send Email, set cookies, read and write from and to test files. + added security. /almost always found on Unix or Linux Web Servers. Perl used for Databases - but needs modules. PHP and mySQL is becoming the deFacto standard for mySql.

Creating and uploading CGI scripts. Stored on the server in cgi-bin or occasionally cgi. Extension pl or cgi. Here we go. Result! Lycos site - no Perl enabled. One&One - Perl enabled! and working fine, brill. Upload with WS-FTP Pro. Set ftp address, login and password - and your ready to go! Upload Perl scripts, select in WS_Ftp, r-click, ftp-commands, CHMOD(change mode - the file permissions. Group, USER, Author read,write, execute permissions. rwe --- --- and all that.) Set to 755 to make generallly executable.

Lovely, we're all set to do a bit of Perl. find host who lets you do perl - no excuse nowadays, indicates ignorance if problems. You need full access for full interactivity - get clients to change hosts oif problems. Need to read and write to dat files. If Telnet, command is - chmod 755 filename (pl or dat file).

Basic PERL Scripts
#!/usr/bin/perl // normal path to Perl proggy on server.
$somenumber=5;
#This sets a var we can access
print "Content- type:text/html\n\n";
//essential line content type.
print "This is where we can send some data to Flash<br>";
print "For example the number $somenumber";
exit;
//allways the very last line in perl

Upload to site - ftp chmod 755 - access with browser. (http - cgi_bin/file.pl). allscripts begin with the standard path to Perl. All likes of code end with(;). Comments(#). (####) Use to block off lines of code.

Content type line is essential, in order to display data on a web page or get it back into Flash correctl - you must set the type of data you are outputting. It's an essential scripting element, not output to the page. Print is sef-evident. Don't need to concatinate quoted text with avariable using +, like in AS. (_root.ouput = _root.pet+"= A Great Pet to own") Perl recognises strings present within a quoted print statement(is that clear?).

Need to escape certain characters, obviously the $ sign, via (\) backslash. Need to escape all Perl reserved chars that you intend to print in a print statement. exit; is always the last line in Perl.

Perl Commands are like actionscript. Basic Perl commands:
print - print data to the Web or e-mail
open - Open file or e-mail for read write or append.
for - for loop
foreach - for loop that iterates over an array
sort - values in array to alphabeticla order
split - Splits array with char specified
if - else - same as AS
elsif - Perl version - missing e.
read - Reads the STDIN (strandard input), usually data via POST
\n - important escape char used to designate newline, or carriage return.

Vars and Arrays
$ variable. @ Array. (@favColors = ("red", "green", "blue");) print"$favColors[0]"; #prints red.

another array type is hash or associative array. Ref with keys instead o fnumbers. Hash array as a whole is ref by (%). Ex:

%carColors = ("Camero", "Red", "Mustang", "Blue", "Corvette", "Yellow");
print"$carColors('Corvette')";

or

%carColors = (Camero => "Red", etc)

A key can be ref with another variable eg:

$osmeCar = "Mustang";
print"The Color of the $someCar is $carColors{$someCar}';

Perl is case sensitive - make sure AS is too.

Special Perl Environment Vars
HTTP_COOKIE - List of all cookies set for the current domain
HTTP_REFERER - The URL of th eWebsite and Page that requested the script
QUERY_STRING - Data string sent by GET request from flash or Web page
CONTENT_LENGTH - Length of data string sent by POST request.

always available as long as data present. to retrieve data: $ENV{ 'environment_variable'}

Test Link to environment.pl on Johns Site here    Excellent it works! Code in the environment.pl file::

#!/usr/bin/perl
#This displays the URL from where
#this script was called
print "Content-type:text/html\n\n";
print "This script was called from $ENV{'HTTP_REFERER'}";

AH! - So you could tell if someone has linked to you. It's a powerful tool the env Vars. Can test for cookies, prevent people from calling you from thier own websites or desktops.

Moving data from Flash to Perl. Straight forward. Perl file on server: Checks to see POST or GET. POST - neads the extra code read(), puts what it finds in var $allPostData (can call what you like). Sends it all back as a string.
if ($ENV{'REQUEST_METHOD'} eq "GET"){
print"$ENV{'QUERY_STRING'}";
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST"){
read(STDIN, $allWebData, $ENV{'CONTENT_LENGTH'});
(print"$allWebData";
)//not if going on to below
                
But we want it split, flash auto does it, Perl needs this code, so it can manipulate individual vars, as above +:
@variables = split(/&/, $allWebData);  
#Split up the pairs at each ampersand

foreach $pair (@variables) {          
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
#Translates spaces back into spaces
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#Translates all url encoded characters back to themselves
$DATA{$name} = $value;
}

# Display the final output on the web page
foreach $i (keys %DATA) {
print "$i: $DATA{$i} <br><br>";

}

This is Crucial so Line by Line.

@variables = split(/&/, $allWebData); // Perl takes the long string and breaks it at every (&), ending up with pairs in @variables. :: favColor1=Blue. But it still looks like a string to Perl not name/value pairs , so it has to split again. Next code splits and puts into proper associative hash array::

foreach $pair(@variables){ //goes through each value in the array specified in the parenthesis - and performs the operations in the curly braces on each value(naming the values as $pair).
 ($name, $value) = split(/=/, $pair); // The temporary holding variables $name and $value are each assigned half of each pair in an array, which is - each variables name and it's value.
$value =~ tr/+/ /; //this is the perl 'translate' function, it's turning all spaces back into themselves and - -
$value =~ s/%([a-fA-F)-9][a-fA-F0-9])/pack("C",hex($1))/eg; //What fun! Using a regular expression to change special chars that were URL encoded into themselves(value side of pair).What the fuck 'pack' is or what the fuck hex($1) is doing fucking fuck knows - or 'eg'. They're not serious teachers - just fucking hacks.- annoys me somewhat.
$DATA{$name} = $value; //loads the final, usable name/value variables into a hash array. Bunch or indiciferable stuff(P.75). But it says you could now rename favColor1 by inserting
$DATA{'favColor1'}="Pink";


Keys
Name part of ass array. $arrayname{$keyname} = $somevalue;

foreach $i (keys %DATA) {
print "$i: $DATA{$i} <br><br>";

}


Last bit uses foreach along with a ref to each key to go one by one through the hash array and display the name and value of the variables. The reseved word 'key' in perl references each key in the array name following it ($DATA). Don't need the <br> tags when dealing with flash.

 

Now Perl can deal with the vars on individual basis. Change code:

foreach $i (keys %DATA) {
$DATA{$i}+=$DATA{$i};
  //adds each var to itself.
print "$i: $DATA{$i} <br><br>;
}

Upload, change textfields to number data - see perl perform. All this is to show what goes on behind the scenes. Sometimes breaking up data unecessary, but need to know how it's done.

This is Difficult, so I've spelled it out on the next page, you need to fully understand what'sgoing on - we're getting there!

 

back to index  next