Tinkering with Perl

Jonathan's Corner (Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  31  32  33  34  35  36  37  38  39  40  Next  Skip Forward
Printer-Friendly Version

Blocks

For both conditionals and loops, it is useful to think of a cluster of statements taken together. A block of code begins with a left curly brace ('{'), and ends with a right curly brace ('}'). It should also be indented by four spaces or a tab. It doesn't matter how far you indent, but you should indent to the same depth most of the time. Here are a few statements by themselves:

print "Type something in: "; # The lack of a \n is intentional, so that the
                             # cursor stays on the same line.
$UserInput = <>;
chomp $UserInput;
print "You typed in " . $UserInput . ".\n";

Here is that same group of statements in a block:

    {
    print "Type something in: "; # The lack of a \n is intentional, so that
				 # the cursor stays on the same line.
    $UserInput = <>;
    chomp $UserInput;
    print "You typed in " . $UserInput . ".\n";
    }

In short, to make a block, you put a left curly brace before it, type in the statements in the block, and close with a right curly brace. The whole thing should be indented four spaces.

See also:

Statements - Flow control - If-then - If-then-else - If-then-else chains - Loops - Foreach loops - While loops - For loops - Functions

Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.

Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)

Read more...

Top

Jonathan's Corner (Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  31  32  33  34  35  36  37  38  39  40  Next  Skip Forward
Printer-Friendly Version