PerlStalker's SysAdmin Notes and Tools

- Skip Navigation- Home / Perl / Basics
- Syntax

Printer Friendly

Join the Blue Ribbon Online Free Speech Campaign

 

Syntax

Variables

Like 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 $name, @scores, %dictionary and $_a_N_n_O_y_I_n_G__v_A_r__N_a_M_e are all valid variable names. However, name, @3kings, %sill$y are not. (There is also a & sigil that is used to dereference code references, which I won't go into here.)

Data Types

Variables can be one of three data types: scalars, arrays and hashes (also called associative arrays).

Scalars

Variables 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.)

Arrays

Array 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 @array, you would access it as $array[0]. Notice the $ on the front. That is because elements of an array are always scalars.

Hashes

Hashes, 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. You would store the counts like so:

$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.

Operators

Operators are the basic way of making things happen. The operators +, -, * and / allow your to do math on your variables while = lets you store values in variables. A complete list of operators can be found in perldoc perlop.

Statements

Perl programs are made up of statements. Most statements in Perl end with a ;.

Comments

Perl 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 Comments

The 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 // comments you know and love.

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.

POD

POD, 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 perldoc perlpod.

Copyright © 2003-2008 Randall B. Smith
<perlstalker AT falconsroost.alamosa.co.us>