Home arrow Perl Programming arrow Page 2 - First Steps in Perl

Our First Perl Program - Perl

In this first part of a three-part series, you'll learn the fundamentals of programming in Perl. This article is excerpted from chapter one of the book Beginning Perl, Second Edition by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. First Steps in Perl
  2. Our First Perl Program
By: Apress Publishing
Rating: starstarstarstarstar / 0
January 05, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Assuming that you now have a copy of Perl installed on your machine (perhaps having followed the instructions in the Introduction), you are ready to start using Perl. If not, go back and follow the instructions. The next step is to set up a directory for all the examples used in the rest of the book, and to write our first Perl program.

Here’s what it will look like:

#!/usr/bin/perl -w

print "Hello, world!\n";

We highly suggest that you type this example in and try to make it work, so before we go any further, a quick note on editors. Perl source code is just plain text, and should be written with a plain text editor, rather than a word processor. Your operating system, whether Unix or Windows, comes with a selection of text editors. You may have a favorite already, so feel free to use it. If not, may we suggest. vi(www.vim.org),emacs(www.xemacs.org), andnedit(www.nedit.org). Windows provides WordPad and Notepad, but they lack many features of modern text editors, so they should be avoided.neditis the most WordPad- and Notepad-like, so give it a try.

After an editor is chosen, we need to create a new directory for our work. If you are in Windows, a simple way to do this is to start up a command shell (Start ➤ Run ➤ cmd) and enter

c:> mkdir begperl
c
:> cd begperl

If you are working in any Unix variant, start a shell and enter

$ mkdir begperl
$
cd begperl

This directory will hold all the examples that we will do as we go through this book.

The next step is to fire up your editor of choice, type in the code shown previously, and save it into a file namedhelloworld.plin the directory we just made. Then, to execute it, type

$ perl helloworld.pl
Hello, world!
$

Congratulations. You have successfully written and executed your first Perl program!


Note  Note that from this point on, we’ll not run through these steps again. Instead, the name we’ve given the file will be shown as a comment on the second line of the program.

You may also have noticed that the output forhelloworld.plon Windows and Unix differs in that Windows adds a blank line at the end of the output for all its Perl programs. From now on, we’ll only print the Unix output that is without the additional blank line. Windows users please be aware of this.


Let’s look at this program in detail by going through it a line at a time. The first line is

#!/usr/bin/perl -w

Normally, Perl treats a line starting with#as a comment, and ignores it. However, the#and!characters together at the start of the first line tell Unix how the file should be run—in this case the file should be executed by the Perl interpreter, which lives in/usr/bin/perlin this example. In the Unix world, this line is known as the shebang (short for “hash bang”), and it must be located on the first line starting in the first column.


Note  Note to Unix users: your version of Perl may reside in a different location than/usr/bin/perl. Common alternative locations are/usr/local/bin/perland/opt/bin/perl. If your version of Perl resides somewhere other than/usr/bin/perl, you will have to adjust your shebang line to point to it.

Unix users can use the invocation shown previously to execute Perl programs:

  $ perl helloworld.pl

But they can also execute Perl programs by making the file executable with the chmodcommand and executing it by name like this:

  $ chmod +x helloworld.pl 
  $ ./helloworld.pl


Perl also reads the shebang line regardless of whether you are on Unix, Windows, or any other system. The reason for this is to see if there are any special options it should turn on. In this case, -w is present and it instructs Perl to turn on the reporting of warnings (we’ll talk more about warnings later in this chapter).

The second line of this program is a blank line. Perl, like C, C++, and many other programming languages, treats blank lines, extra spaces, and tabs as whitespace. In Perl, whitespace can be added to the program to make it more readable.

The third line of the program is

print "Hello, world!\n";

Theprint()function tells Perl to display the given text, without the quotation marks. The text inside the quotes is not interpreted as code, and is called a string. As we’ll see later, strings start and end with some sort of quotation mark. The\nat the end of the quote is a type of escape sequence which stands for “new line.” This instructs Perl to finish the current line and take the prompt to the start of a new one.

You may be wondering why-wis so helpful. Suppose we altered our program to demonstrate this, and made two mistakes by leaving out-wand modifying our code so it looks like this:

#!/usr/bin/perl

print "Hello, $world!\n";

The string that we are printing now contains the text $world. As we will see in the next chapter,$worldis a variable, and this variable has not been assigned a value. If we attempt to print a variable that has no value, we simply print nothing.

Save these changes inhelloworld2.plbefore exiting your editor. Now let’s get back to the command prompt, and type the following:

$ perl helloworld2.pl

Instead of getting the expected  

Hello, world!
$

the output would be

Hello, !
$

If we now correct one of the mistakes by including-win our program, thenhelloworld2.pllooks like this:

#!/usr/bin/perl -w

print "Hello, $world!\n";

Once we have saved this new change into the program, we can run it again. The output that we get now contains a warning as well as the text printed, so the screen looks like this:

$ perl helloworld2.pl
Use of uninitialized value in concatenation (.) or string at helloworld2.pl line 3. Hello, !

On the surface of things, it may seem that we have just given ourselves another line of output, but bear in mind that the first line is now a warning message, and is informing us that Perl has picked something up that may (or may not) cause problems later on in our program. Don’t worry if you don’t understand everything in the error message at the moment, just so long as you are beginning to see the usefulness of having an early warning system in place.

For Perl versions 5.6.x and higher, the-wswitch can be replaced with ause warningsdirective that follows after the shebang line:

#!/usr/bin/perl
use warnings;

print "Hello, world!\n";

However, this code will not turn on warnings in Perl for versions pre-5.6. For backward compatibility, we will stick with the-woption.

Please check back for the next part of this series.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: