First ... Back ... Next ... Last ... (Notes home)

Perl Syntax and Flow of Control

Statements and whitespace

  • Whitespace does not matter; usually either one or no spaces are required to separate syntactic elements (enough to make them unambiguous). Tabs and multiple spaces count as a single whitespace. In most cases, new lines may be started any time. Indentation can help to keep your code neat, but is not required.
  • Comments start with a pound sign, #, and go to the end of the line.
  • There are numerous reserved words for built-in functions, variable types, etc.
  • User-defined functions are allowed, and may be prototyped.
  • Additional functionality is gained at interpret/run time through use or require statements, which operate similarly to #include statements or run-time libraries in languages like C and Java.

Variables and Operators

  • Variables take a symbol telling what sort of variable they are:
    • $ for scalar variables (may be text or numeric)
    • @ for arrays (use $ to refer to one element)
    • % for associative arrays, aka hashes
  • Operators are typical, although the lack of variable typing can yield some unexpected results.
    • Arithmetic operators are + - * / with % for modulus
    • && and || are Boolean AND and OR
    • man perlop for the complete list; most are used very consistently with other programming languages.

    Functions

    • There are many built-in functions. man perlfunc for a listing (but more functions may be added through added-in modules)
    • Functions may have required or optional arguments; generally, variables passed to functions are made local to that function (i.e., "pass by value"). But you can also pass references, and return values.
    • Perl is not a strongly typed language. When needed, the type of a variable is infered by context (for example, adding 1 to a variable indicates it's a number).
    • The programmer may define functions. You can also provide for overloaded functions, classes (including both functions & data), and other object-oriented techniques. More on this later in the course.

    Flow of Control

    • Perl is a fairly linear language. Processing for a Perl program starts at the top, though function calls can take the flow of control anywhere.
    • Advanced flow of control, such as threads or forked/child processes, are supported.
    • Loops include while, for/foreach, and until.
    • if/else/elseif is available for conditional execution.
  • First ... Back ... Next ... Last ... (Notes home)

    UAF Computer Science
    Prof. Greg Newby