What Is PERL Syntax?

What Is PERL Syntax? thumbnail
PERL syntax includes special characters that identify program variables.

The syntax of a programming language – any programming language – are the grammar and formatting rules that make its commands unique. What sets PERL apart, however, is an easy “do what I mean” syntax style and only a few strict formatting rules that make it easy to learn and write. If you know how to code in languages such as C, Smalltalk or Lisp, understanding PERL syntax – also called perlsyn - may be even easier as it “borrows” syntax rules from each.

  1. Getting Started

    • Every PERL script should start with a statement that directs it toward the interpreter installed on your computer. The syntax of the statement depends, however, on whether your operating system is UNIX or Windows. For a UNIX OS, the opening statement appears as #!/usr/local/bin/perl and for a Windows OS it appears as #!C:\Perl\bin\perl, or #!D:\Perl\bin\perl if you install the interpreter on your D: drive.

    Syntax Constants

    • Elements of perlsyn that remain constant are syntax rules for ending PERL statements, adding comments, indicating program variables and the manner in which each script treats whitespace. A semi-colon is the standard for ending a PERL declaration or statement. This includes statements that print text, statements that evaluate variables and statements that control program flow. For example, print “Hello World!”; and if $x == $y; always end with a semi-colon. A hash symbol – “#” indicates a comment line. If your comment spans multiple lines, include a hash symbol at the beginning of each line. Use the symbols '$', '@' or '%' to identify a program variable. Finally, the only time perlsyn considers whitespace relevant is when it is part of a string surrounded by quotation marks. Otherwise, include as much or as little whitespace as you need to make your program easier to read as perlsyn considers it irrelevant.

    Syntax Variables

    • How you use quotation marks and whether they are even necessary depends on the type of statement as well as what it contains. Choose between using single or double marks in literal strings and use either or none when including numbers. But always use double quotation marks when a statement contains variables or special characters such as the new line character. In the statement Hello World, “Hello World” and ‘Hello World’ are both correct as is displaying the number using 42 or “42.” However, a statement such as print "Hello, $name\n"; that displays a personalized greeting and then begins a new line must include double quotation marks. Including parentheses as a part of statement syntax can be a matter of preference or a way to set precedence. The statements print("Hello, world\n");and print "Hello, world\n"; are exactly the same. Control the order in which statements run by enclosing them in parentheses, as you would in a math equation.

    Control Syntax

    • Use perlsyn to control the flow of a PERL script by enclosing code in brackets. This is especially important when placing statements in blocks or using control statements such as loops, if/when and switch statements. Brackets separate blocks of code so the statements run as a group. In a loop statement, this can mean a statement or group of statements within the brackets run until a condition evaluates to true or false before an alternate statement takes over:

      LOOP: {
      do {
      last if $x = $y**2;
      # do something here
      } while $x++ <= $z;

Related Searches:

References

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured