Perl Basics
Variables in Perl are weakly typed. They do not need to be declared before use. Use of any variable before it is assigned a value, however, will result in undef (or a warning if the -w flag is on).
All variables are preceded by a special character, which Larry Wall likens to "articles" in natural languages:
- $foo is like "the foo" (scalars)
- @foo is like "those foos" (arrays)
- %foo is like "that hash of foo" (hashes)
- &foo is like "to foo" (subroutines)
The Truth about Truth
- Any string is true, except for "" and "0"
- Any number is true except for 0
- Any reference is true
- Any undefined value is false
Contexts
Perl expressions are context-sensitive. There are list contexts, and scalar contexts. This has several consequences that will confuse you for many years to come. For example, arrays have no boolean value because any time a list is evaluated in a scalar context, some magic happens to convert it to a scalar (usually the length of the list).
Interpolation
Double-quoted strings (and most other kinds of "quote-like" constructs, including patterns) will interpolate escape sequences and variable names. Single-quoted strings do not, except for \'.
my $str = "hello"; print "$str\n" . '$str\n'; => hello $str\n