Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Server Side Programming
User Name:
Password:
 

Closed Thread
Thread Tools   Display Modes
  View First Unread
 
Old 06-21-2004, 09:17 PM
  #16
kevin
Super Deity (Level 18)
 
Join Date: Sep 2000
Posts: 10,217
iTrader: (0)
kevin is on a distinguished road
How can I run/test Perl scripts on my Windows PC?

** This FAQ is contributed by Agent002

Besides a Perl interpreter, you also need a web server software installed. If you're running Windows, I can tell you how to install Apache and ActivePerl, to run Perl on your own computer.

First, download the Apache 2 binary distribution from apache.org. When the installer prompts you, set "Network Domain" and "Server Name" to localhost, and "Administrator's Email Address" to your own email addy. Choose the "Typical" setup mode, the default installation folder (C:\Program Files\Apache Group\) is good. When installation is finished, Apache should start automatically as a Windows service. You can check if it's working by going to http://localhost/ (or if you prefer, http://127.0.0.1/) in your web browser. You can now save your files in the /htdocs directory inside the directory you installed Apache in, and access them through http://localhost/filename.

Then, download the ActivePerl package from www.activestate.com. Notice that registering is voluntary. Choose the newest build and Windows / MSI (a Windows installer package). When installing, leave the Custom Setup settings as they are, as well as the installation directory (C:\Perl\). In the next screen, leave "Enable PPM3 to send profile info to ASPN" unchecked. In the next screen, check both "Add Perl to the PATH environment variable" and "Create Perl file extension association". Then the installer will copy all files and generate the HTML documentation - generating the documentation will take a good while so just be patient.

Now you have the Perl interpreter installed, and you can run Perl scripts in it, but to run them through your web browser (as CGI scripts), you still need to configure Apache a little. Go to the Windows start menu -> Programs -> Apache HTTP Server 2.0.xxx -> Configure Apache Server -> Edit the Apache httpd.conf Configuration File, and some code should open up in Notepad. Hit Ctrl+F or choose Edit -> Find (in Notepad), type cgi-script and hit "Find Next". You should come to a line that says #AddHandler cgi-script .cgi. Uncomment it, i.e. remove the pound sign (#) from the beginning of it. Also set it to interpret .pl files as CGI scripts, so you have AddHandler cgi-script .cgi .pl. Then save the file and restart Apache from Start menu -> Programs -> Apache HTTP Server 2.0.xxx -> Control Apache Server -> Restart. You can now save your Perl scripts in the /cgi-bin directory, that is located in the same dir as /htdocs. You run them in your browser through http://localhost/cgi-bin/filename.cgi.

You will also have to use a different shebang line when running Perl scripts on your localhost server. Instead of the typicl web host server shebang line that is similar to this:

#!/usr/bin/perl

you will use:

#!/perl/bin/perl.exe

or

#!C:/perl/bin/perl.exe

or

#!perl

whichever one works for your setup.


If you wish to be able to run Perl scripts from the /htdocs directory too, edit the httpd.conf file again (Start menu -> Programs -> Apache HTTP Server 2.0.xxx -> Configure Apache Server -> Edit the Apache httpd.conf Configuration File), hit Ctrl+F, and search for <Directory, you probably need to search several times before coming to a line that says <Directory "C:/Program Files/Apache Group/Apache2/htdocs"> (the path you see depends on where you installed Apache, that is the default installation directory). Everything below that until a closing </Directory> (reminds you of HTML doesn't it) are instructions for the /htdocs directory. Inside that block, find a line saying Options Indexes FollowSymLinks, amend it to Options Indexes FollowSymLinks ExecCGI, save the file and restart Apache (Start menu -> Programs -> Apache HTTP Server 2.0.xxx -> Control Apache Server -> Restart). You can now run your CGI scripts in /htdocs too.


At the moment, only .cgi files are ran as CGI scripts, but you may want to run other extensions, such as .pl, .py, .tcl or .rb as CGIs too. Edit the httpd.conf configuration file again, find the line that says AddHandler cgi-script .cgi, change it to AddHandler cgi-script .cgi .pl (you can add as many extensions as you want), then save the file and restart Apache.


There is still one thing you may want to edit; setting Apache to see index.cgi (and index.pl) as directory index files, just like index.html and index.htm are seen. Edit httpd.conf again, and find DirectoryIndex from the file. You should come to a line saying DirectoryIndex index.html index.html.var, amend it to DirectoryIndex index.html index.html.var index.cgi index.pl. You can also add index.htm to it, if you prefer the .htm extension to .html. Then save the file and restart Apache.


Good luck
__________________


1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman

Last edited by kevin : 06-24-2004 at 11:29 PM.
kevin is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it?
Old 06-23-2004, 03:26 PM
  #17
kevin
Super Deity (Level 18)
 
Join Date: Sep 2000
Posts: 10,217
iTrader: (0)
kevin is on a distinguished road
Perl Programming and Memory Usage

Here are some basic tips for keeping memory usage low.

Don't do what is sometimes called "slurping" files:

Code:

open (FILE,"yourfile");
@data = <FILE>;
foreach (@data) {
   #do something
}
close(FILE);
this reads the entire file into memory. If its just a small file then its not really a big deal.

But this is much better as far as memory consumption is concerned:

Code:

open (FILE,"yourfile");
while (<FILE>) {
   #do something
}
close(FILE);
If you have files with thousands and thousands (or more) of lines you probably should be using a while loop.

Use "grep" and "map" only when you really have to as they also slurp files into memory. You can probably use a while loop instead of "grep" or "map" in many situations.

Don't use double-quotes when you don't need to. See: Should I always quote my "$variables"? FAQ.
__________________


1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
kevin is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it?
Old 06-24-2004, 11:23 PM
  #18
kevin
Super Deity (Level 18)
 
Join Date: Sep 2000
Posts: 10,217
iTrader: (0)
kevin is on a distinguished road
How Do I Check the Length of a String?

To check the length of a string using Perl you use the length() function. Some examples:

Code:

$name = 'Jennifer';

$length = 8;
if (length($name) > $length) {
print "Too long";
}

$num = length($name);
if ($num > 8) {
print "Too long";
}
__________________


1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman

Last edited by kevin : 06-25-2004 at 02:02 AM.
kevin is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it?
Old 06-25-2004, 12:58 AM
  #19
kevin
Super Deity (Level 18)
 
Join Date: Sep 2000
Posts: 10,217
iTrader: (0)
kevin is on a distinguished road
How Do I Select a Random Element From an Array?

To select a random element from an array using perl you use the rand function. Some examples:

Code:

@DATA = qw(cat dog fish cow horse pig camel giraffe); 

#example 1
$random_element = $DATA[int(rand @DATA)];

#example 2
$index = rand @DATA;
$random_element = @DATA[$index];

if you are using a version of Perl older than 5.004 you must use srand() before trying to select a random element.

Code:

srand():

@DATA = qw(cat dog fish cow horse pig camel giraffe); 

#example 1
$random_element = $DATA[int(rand @DATA)];

#example 2
$index = rand @DATA;
$random_element = @DATA[$index];
you can only call srand() once per program.

Perl version 5.004 and higher automatically calls srand() unless srand() has already been called.
__________________


1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
kevin is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it?
Old 06-25-2004, 01:09 AM
  #20
kevin
Super Deity (Level 18)
 
Join Date: Sep 2000
Posts: 10,217
iTrader: (0)
kevin is on a distinguished road
How Can I Determine the NUmber of Elements in an Array?

To determine the number of elements in an array you assign the array to a scalar variable:

Code:

@DATA = qw(cat dog fish cow horse pig camel giraffe); 
$number_of_elements = @DATA;
print $number_of_elements;
$number_of_elements will equal 8 for the above example. $DATA[0] through $DATA[7] equals 8 elements.
__________________


1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman

Last edited by kevin : 06-25-2004 at 01:11 AM.
kevin is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it?
Old 06-25-2004, 01:31 AM
  #21
kevin
Super Deity (Level 18)
 
Join Date: Sep 2000
Posts: 10,217
iTrader: (0)
kevin is on a distinguished road
Are Perl and CGI the same thing?

CGI is actually a standard for sending data to a web server. CGI means Common Gateway Interface. The forms you see on web pages are examples of CGI.

Perl was used so often to process the data that CGI forms sent to a server that Perl scripts were called CGI scripts. In the early days many servers forced you to use the .cgi extension with Perl scripts instead of the .pl extension and you had to put them in the cgi-bin folder of your web hosting server (you still do have to put them in the cgi-bin). The name CGI stuck and Perl scripts came to be known as CGI/Perl scripts on the internet.

While technically CGI and Perl are two different things, CGI became commonly associated with Perl (or maybe that's the other way around). You can use any server side script to process CGI form data, such as PHP or ASP, but when people say CGI script they always mean a Perl script.
__________________


1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman

Last edited by kevin : 06-25-2004 at 11:17 PM.
kevin is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it?
Closed Thread
KEEP TABS
SPONSORS
 
Boxedart

 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 03:19 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.