Home arrow Perl Programming arrow Page 2 - Structure and Statements in Perl

Statements and Statement Blocks - Perl

In this second part of a three-part series on Perl programming, you'll learn how to structure your programs, and how to use statements. This article is excerpted from chapter one of the book Beginning Perl, Second Edition by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Structure and Statements in Perl
  2. Statements and Statement Blocks
By: Apress Publishing
Rating: starstarstarstarstar / 0
January 11, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If functions are the verbs of Perl, then statements are the sentences. Instead of a period, a statement in Perl usually ends with a semicolon, as we saw earlier:

print "Hello, world!\n";

To print some more text, we can add another statement:

print "Hello, world!\n";
print "Goodbye, world!\n";

We can also group together a bunch of statements into a block—which is a bit like a paragraph—by surrounding them with curly braces{...}. We’ll see later how blocks are used to specify a set of statements that must happen at a given time, and also how they are used to limit the effects of a statement. Here’s an example of a block:

{
print "This is ";
print "a block ";
print "of statements.\n";
}

Notice how indentation is used to separate the block from its surroundings. This is because, unlike paragraphs, you can put blocks inside of blocks, which makes it easier to see on what level things are happening. This:

print "Top level\n";
{
print "2nd level\n";
{
print "3rd level\n";
}
print "Where are we?";
}

is easier to follow than this:

print "Top level\n";
{
print "2nd level\n";
{
print "3rd level\n";
}
print "Where are we?";
}

As well as curly braces to mark out the territory of a block of statements, you can use parentheses to mark out what you’re giving a function. The set of things given to a function are the arguments; and you pass the arguments to the function. For instance, you can pass a number of arguments to theprint()function by separating them with commas:

print "here ", "we ", "print ", "several ", "strings.\n";

The print()function happily takes as many arguments as it is given, and it produces the expected answer:

here we print several strings.

Surrounding the arguments with parentheses clears things up a bit:

print("here ", "we ", "print ", "several ", "strings.\n");

In the cases where parentheses are optional, the important thing to do is to use your judgment. Sometimes something will look perfectly understandable without the parentheses, but when you’ve got a complicated statement and you need to be sure of which arguments belong to which function, putting in the parentheses is useful. Always aim to help the readers of your code, and remember that these readers will more than likely include you.

Please check back for the conclusion to this article.



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

blog comments powered by Disqus
   

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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: