PerlStalker's SysAdmin Notes and Tools | |
Home
/ Perl
/ Basics
|
SyntaxVariablesLike all imperative languages, Perl has variables to store the values of things.
Variable names may contain letters, numbers and the underscore but may not begin
with a number. This is similar to most languages but Perl has one other requirement,
variables are prepended with a $, @ or %. These funny characters, or sigils,
define what data type the variable is. The names Data TypesVariables can be one of three data types: scalars, arrays and hashes (also called associative arrays). ScalarsVariables that start with a $ are scalars. A scaler is a number or string. Numbers are just that numbers like 1, 2.5 and 32767. Strings are "hello", '4.5' and "Perl". Perl converts numbers to strings and vice versa as needed. (Scalars can also store references but I'll get into that later.) ArraysArray variables begin with an @. Arrays are lists of scalars. The first item
in an array is number 0 ans is accessed like a scaler. For example, to get the
first element of array HashesHashes, or associative arrays, begin with an %. Hashes are a mappings between
strings and scalars. For example, say you want to store the number of pets in
a store in the hash
$store{'dogs'} = 4;
$store{'cats'} = 7;
$store{'rabbits'} = 42;
That says that you have 4 dogs, 7 cats and 42 rabbits. (Again, notice the $ sigil. Like array elements, hash elements are scalars.) This makes for easy, readable, access to values. OperatorsOperators are the basic way of making things happen. The operators StatementsPerl programs are made up of statements. Most statements in Perl end with a
CommentsPerl has a bad reputation of being unreadable. While it's true that you can write ugly code in Perl -- like any language -- you can also write very clean code. However, no matter how clean the code, it is nice to be able to tell the next person who reads your code (perhaps you) what is going on. There are two ways of commenting your code. Inline CommentsThe first way to comment your code, is to use the # character. Perl will ignore
everything on the line following the #. For those with a background in C++ or
Java, this is similar to the Note: Perl lets you use # in ways that are not comments. Those cases are rare and can be confusing if you're not aware of them. PODPOD, or Plain Old Documentation, provides a way of documenting a program or
module that is available to users without requiring them to see the code. (Java
programmers may already be familiar with JavaDoc which serves a similar purpose.)
POD is explained in detail in |
|
<perlstalker AT falconsroost.alamosa.co.us> |
|