PERL Syntax Overview


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, $namen";     # works fine

print 'Hello, $namen'; # prints $namen 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, worldn");
print "Hello, worldn";

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 y
ou 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, World" 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 change the mode of 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 produce following result:
Hello, World

Perl Command Line Flags:

Command line flags affect how Perl runs your program.
$perl -v
This will produce following result:
This is perl, v5.001 built for i386-linux-thread-multi
................
You can use -e option at command line which lets you execute Perl statements from the command line.
$perl -e 'print 4;n'
RESULT: 4
$perl -e "print 'Hello World!n";'
RESULT: Hello World!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s