|
||
Home Articles Authors Links Useful Tips Polls HOWTOs |
|
Perl for the Absolute NewbieArticle 1: Running Your First Perl ProgramPerl is a programming language usually associated with the Web and CGI programs, but is in fact a powerful general-purpose tool for systems administration, network programming, and a host of other uses. In this article, I'll begin a step-by-step tutorial in Perl, starting with your first Perl program. Testing PerlBefore going any further, make sure that Perl is installed on your computer by entering the following incantation on your command line:perl -e 'print "Perl Works!\n"' if this prints Perl Works! then you're ready to move on. If not, either the Perl interpreter is not in your path, or you need to install perl. See the excellent perl.com download page for more information on getting Perl for your machine. Our First ProgramLet's begin by jumping right into our first Perl program, the ever-popular 'Hello World'. The source code for the program looks like this:#!/usr/bin/perl -w # print our greeting print "Hello World!\n";Type this program into a file called named hello.pl. Make the file executable using the command chmod +x hello.pl , and run it by typing ./hello.pl . You should see the message Hello World! appear on your console.
Three Easy PiecesNow let's pick the program apart. The first line, #!/usr/bin/perl -wis known as the shebang line. This tells the operating system how to deal with the file; in this case, it looks in the /usr/bin directory for a program named perl (the Perl interpreter), and gives the file to it for execution. The -w switch at the end tells the Perl interpreter to turn on verbose warnings, which you always want to do while you're learning Perl. Note that this must be the first line in your Perl program. The next line, # print our greetingis an example of a Perl comment. Comments start with the # character and are not run as part of the program per se, but give the programmer a way to document what the code is supposed to be doing.
The last line, print "Hello World!\n";is the heart of this program. We start with the function print(), which takes a text string as its argument, and prints it out (usually to the console where the program was invoked). We give print() the string "Hello World!\n" . We see here our first example of string interpolation, which is a fancy way of saying that we replace something in the string with something else that it represents.
In this case, the characters The last thing that needs mentioning is the semicolon ( Your TurnExperiment with hello.pl by using different text in the quotes, changing the double quotes to single quotes, omitting the In the next issue we'll take a look at how to store data in your Perl programs using variables. Until then, enjoy! |
0.4.0 | Copyright to all articles belong to their respective authors. Everything else © 2024 LinuxMonth.com Linux is a trademark of Linus Torvalds. Powered by Apache, mod_perl and Embperl. |