iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 11, 2008
Improving accessibility for motor impaired users
WebDevTips UK
 
May 11, 2008
10 ways to orientate users on your site
WebDevTips UK
 
May 11, 2008
Web Design Clinic - Rros restoration camp 2006
About
 
May 10, 2008
The Moods of Facebook
About
 
May 9, 2008
CSS 1 properties are a great start
About
 
May 9, 2008
Reader Question: How do you get fancy fonts?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /CGI and Perl

Your first Perl script 

  Views:    11502
  Votes:    0
by Spyder Co 3/03/05 Rating: 

Synopsis:

There's no better way to jump right in than to create your very first Perl script.
Pages: 
The Article

Perl and CGI Tutorials > Hello World! Your First Perl Script

In this section we will be going over the basics of setting up and running your first perl script. Before we continue, if you have not installed Perl on your system, download the latest MSI version from http://www.activestate.com under ActivePerl

On your desktop, Right Click and select New>Text Document. Rename the file to test.pl and click enter.  ".pl" is the Perl file extension and any text document can be saved as a .pl file.  .cgi is nothing more than a perl file written for the web, neither extension is better or worse than the other and both will work in either environment.

The first line of every Perl script is called the shebang line.  This line begins with the pound symbol and continues with the location of Perl on your system. If you are using Windows and did a default install, your shebang will look like this:

#!/usr/bin/perl 


Okay, let's begin your first Perl script.  Double click on your test.pl file on your desktop, it should open up in Notepad for you.

Type in your shebang line on the first line and click enter a few times. On the next line, type the following line:

print "Hello World!"; 





To print in Perl, whether it be information to a database or to the screen, we call upon the print command. We usually use quotes around our phrase and end it with a semi-colon. Nearly ALL Perl lines will end in a semi-colon, this tells Perl that our command or line of text is complete and to begin reading the next. 

Click on File>Save to save your script.  Open up your cmd or command prompt by click on your Start icon>Run> then type in 'cmd' if you are on Win2000+ or type 'command' if you are on an older version of windows.

To run a perl script from your desktop, you have to travel through the command prompt to your desktop directory.  On some version of Windows it'll open up to Desktop on default, other's (like my computer), it doesn't. 

To change a directory in this window, use 'cd'.  CD stands for 'change directory' and it lets you change up a directory or back a directory.  Here is an example of me working on Windows XP.

Opens to:               C:\Windows\Documents and Settings\
I type:                     cd desktop
Now open::            C:\Windows\Documents and Settings\desktop


To switch back a directory, you type "cd ..".  Here's another example:

Now open:            C:\Windows\Documents and Settings\desktop
I type:                    cd ..
Now open:           
C:\Windows\Documents and Settings\


Now to run your perl script, call the PERL command followed by your file name.  If our file was test.pl, we'd type in "perl test.pl" without the quotes.

If this was successful, your program should write the phrase "HELLO WORLD!!" inside of your command window.  Congratulations, you have written your first Perl script

Now it should read HELLO WORLD! Yay!!! You now wrote your first Perl script and you'll now be losing countless of hours of sleep because of it :)

Complete example:

#!/usr/bin/perl
print "Hello world!";
Pages: 

Similar/related articles:


 
  Sponsors