Hello World! ~ Your first Perl script
by pixelatedcyberdust, 1/13/04
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
1. 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.
2. 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:
3. 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.
4. 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.
6. 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 :)
print "Hello world!";