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:
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:
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!";