Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to test a perl script

         

BillDex

5:36 am on Sep 27, 2006 (gmt 0)

10+ Year Member



If I have a perl file/script in my 'www' folder, how can I test this script?
The Firefox asks me to download the file, but I already have it, I just want to test it.

[edited by: jatar_k at 5:37 pm (utc) on Sep. 27, 2006]

rocknbil

6:31 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it is asking you to download it, this most likely means it is not printing a content-type header, or that header is wrong. I suppose this is better than a server error. :-)

Executable web apps are not delivered directly, they are managed through the CGI interface. So when perl executes a print command, it is not actually printing to the browser, it is sent to the CGI interface which then passes it on to the browser.

If you do not provide a mime type to the CGI interface, the script will usually return a server 500 error to the browser. To provide a mime-type, you print this from the script before you print any other output:

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

The two newlines are important. This can be used (or abused) to "force" a download by printing an content-type that is not recognized by any browser:

print "content-type:bad-type\n\n";
or
print "content-type:whatever-you-might-have-error-here\n\n";

(There's more to this story, because sometimes browsers can "sniff out" the type, and if it's recognized, will display it accordingly.)

So that should shed some light on the download part.

To test your script, install a copy of perl on your computer. Google for the latest version of ActivePerl to install it. There are help files on usage, but generally to test a script in Windows you

1. Go to start->run
2. Type cmd or command in the box (varies from OS to OS)
3. In the command (DOS) window, navigate to the directory of your script by typing the following:
a. cd \ (press enter, this brings you to the root of your c drive)
b. cd path_to\your_directory\and_folder (press enter)
4. dir (press enter, this shows the files in the current folder and verifies you're in the right place)
5. perl scriptname.pl (press enter, this runs the script)

You should get any output from the script or, if it errors, see the error on the screen. If it does "nothing at all" this is bad, it means this script is running but not producing output (but you would get a 500 error on the web server if that is the case.)

The other possibility is your server is configured to only execute perl scripts of a specific extension: that is, if the script has a .pl extension, rename it with a .cgi extension.

Birdman

6:55 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To add to rocknbil's excellent answer, you usually need the Perl file to reside in the "cgi-bin". That's a specific directory in your public tree. Most host's configure the accounts to only process Perl files from within this folder.

I also agree that it's a good idea to have Perl on your PC but at some point, you'll want to test it live on the server. You should find some info on your webhost's site or your control panel.

[edited by: Birdman at 6:56 pm (utc) on Sep. 27, 2006]

lexipixel

3:42 am on Sep 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It appears from your question that you are very new to perl. I don't know what script your are trying to run, but it's better to see if any perl script will run --- and best to start with a simple one to test.

I'm assuming you want a perl script that runs on the web, (you can write scripts to run on a local machine also -- the example I supply below produces HTML output and should be run from a web browser).

Try the "hello world" script below... if it runs, then you can move on to more complicated scripts.

Script starts at line starting with #!/usr/bin/perl, cut and paste to a text editor and save file as helloworld.pl

1. upload perl file to the /cgi-bin/ folder on your server, (use FTP and upload as ASCII text)

2. set the permissions for the file to 755, (CHMOD 755)

3. enter URL [yourdomain.com...] in browser

4. if it says Hello World! perl is working.

#!/usr/bin/perl
#
# ===========
# helloworld.pl
# ===========
#
print "content-type:text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>Hello World</title>\n";
print "</head>\n";
print "<body>\n";
print "<b>Hello World!</b><br>\n";
print "</body>\n";
print "</html>\n";
#
#