< Perl Tutorial

Perl fundamentals

← Previous Next →

Perl Fundamentals


A Perl program consists of a sequence of declarations and statements, which run from the top to the bottom. Loops, subroutines, and other control structures allow you to jump around within the code. Every simple statement must end with a semicolon (;). Perl is a free-form language i.e. we can format and indent it however we like. Whitespace serve mostly to separate tokens.

  1. File Extension - A Perl script can be created any simple file editor like notepad. Only thing that must be kept in mind is that a perl file must have an extension of .pl. File names can contain numbers, symbols, and letters but must not contain a space.
  2. Comments - Comments in Perl are written using an Octothorp(#). Anything after the # symbol will be ignored by the interpreter.
    An example of a comment is:
    print "Hello World"            # prints the specified string


  3. Whitespaces - A Perl program does not care about whitespaces. But if spaces are inside the quoted strings, then they would be printed as is. All types of whitespace like spaces, tabs, newlines, etc. are equivalent for the interpreter when they are used outside of the quotes. A line containing only whitespace, possibly with a comment, is known as a blank line, and Perl totally ignores it.
  4. Single and Double Qoutes - There is an important difference in single and double quotes. Only double quotes interpolate variables and special characters such as newlines \n, whereas single quote does not interpolate any variable or special character. Anything inside a single qoute is printed as is without any interpolation.
  5. Escape Sequences - Perl uses the backslash (\) character to escape any type of character that might interfere with our code.
  6. Identifiers - A Perl identifier is a name used to identify a variable, function, class, module, or other object. A Perl variable name starts with either $, @ or % followed by zero or more letters, underscores, and digits (0 to 9). Perl does not allow @, $, and % within identifiers. Perl is a case sensitive programming language. Thus $Manpower and $manpower are two different identifiers in Perl.
  7. Here Documents - Effectively, it means that we can write a large amount of text within our program, and it will be treated as a string provided it is identified correctly.
← Previous Next →