[ ^ CSE 341 | section index | next -->]

CSE 341 -- 13 Apr. 2000


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:

The Truth about Truth

  1. Any string is true, except for "" and "0"
  2. Any number is true except for 0
  3. Any reference is true
  4. 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

Last modified: Wed Apr 12 14:10:27 PDT 2000