Tutorials Point

Google
Web This Site

  PERL Tutorial
  Advanced PERL
  PERL References
  Selected Reading

© 2006 TutorialsPoint.COM


  Home     References     About TP     Advertising  

PERL Syntax Overview

previous next AddThis Social Bookmark Button


A Perl script or program consists of one or more statements. These statements are simply written in the script in a straightforward fashion. There is no need to have a main() function or anything of that kind.

Perl statements end in a semi-colon:

    print "Hello, world";

Comments start with a hash symbol and run to the end of the line

    # This is a comment

Whitespace is irrelevant:

    print "Hello, world";

... except inside quoted strings:

    # this would print with a linebreak in the middle
    print "Hello
        world";

Double quotes or single quotes may be used around literal strings:

    print "Hello, world";
    print 'Hello, world';

However, only double quotes "interpolate" variables and special characters such as newlines (\n ):

    print "Hello, $name\n";     # works fine

    print 'Hello, $name\n';     # prints $name\n literally

Numbers don't need quotes around them:

    print 42;

You can use parentheses for functions' arguments or omit them according to your personal taste. They are only required occasionally to clarify issues of precedence. Following two statements produce same result.

    print("Hello, world\n");
    print "Hello, world\n";

PERL File Extension

A PERL script can be created inside of any normal simple-text editor program. There are several programs available for every type of platform. There are many programs designed for programmers available for download on the web.

Regardless of the program you choose to use, a PERL file must be saved with a .pl (.PL) file extension in order to be recognized as a functioning PERL script. File names can contain numbers, symbols, and letters but must not contain a space. Use an underscore (_) in places of spaces.

First PERL Program

Assuming you are already on Unix $ prompt. So now open a text file hello.pl using vi or vim editor and put the following lines inside your file.

    #!/usr/bin/perl
    # This will print "Hello, Workd" on the screen
    print "Hello, world";
#!/usr/bin is the path where you have installed PERL

Execute PERL Script

Before you execute your script be sure to CHMOD the script file and give execution priviledge, generally a setting of 0755 works perfectly.

Now to run hello.pl Perl program from the Unix command line, issue the following command at your UNIX $ prompt:

   $perl hello.pl

This will result in printing Hello World on your screen.


Perl Command Line Flags

Command line flags affect how Perl runs your program.


    $perl -v
    RESULT : This is perl, version 5.001
    ................
You can use -e option at command line which lets you execute Perl statements from the command line.
    $perl -e 'print 4;'
    RESULT: 4
    $perl -e "print 'Hello World!\n";'
    RESULT: Hello World!


previous next Printer Friendly



  

Advertisement