Perl language syntax is like other normal computer language like C, Linux shell etc. But like other there are some specific features in Syntax which make it different from another language and has same features regarded its strong points over another computer languages. In this post we will try to cover some of vital points of Perl language Syntax which should get highlight when we talk about Perl language syntax.
In perl language we have declaration and statements towards bottom. Various control structures like conditions, loops, subroutines etc allow us to come in between code anytime. These are main declarative statements used within code to decide its direction.Every simple statement end with semicolon (;) expect declarative statements.
we can use Perl statements in interactive shell like below mentioned example in which we tried to print Perl Hello World program.
# perl -e 'print "Hello World\n"' Hello World
So this way we can see Perl is an Interactive Mode Programming which could use while working with unix/Linux Shell.
Script Mode Programming
We can also use Perl language for scripting purpose, we can draft perl statements in file with correct execution permission which could be executed to complete various instruction like below Hello World again in script.
# cat Hello_world.pl #!/usr/bin/perl print "Hello World\n";
Now we need to provide execution permission to file owner like below.
# chmod u+x Hello_world # ll Hello_world.pl -rwxr--r-- 1 root root 39 Jan 14 16:51 Hello_world.pl
Now we can run this script like below.
# ./Hello_world.pl Hello World
We could create perl script with any text editor , like most of time i used vi/vim in Linux/Unix machine and BlueFish in Windows machine. Perl script extension should be .pl or .PL
Like above in mentioned perl script, we used Shebang which basically nothing just perl command path, which will tell script to treat it as perl script.
# which perl /usr/bin/perl
Shebang is used in other languages as well like python, Shell etc. It could be different in some other systems.
Comments in Perl
In every Computer language, comments are always useful for any developer, it make user friendly program in which any level of developer can understand codes easily which make them easy to analyze and debug for long process and always good for software and code versioning. In perl, we could easily provide comments with start of # like below
#!/usr/bin/perl #Below line will print Hello world print "Hello World\n";
Please understand line start with # is comment but not shebang which present in first line. Comments are not only one line, we could use multi-line comments as well with below example.
# cat Hello_world #!/usr/bin/perl #Below line will print Hello world print "Hello World\n"; =begin This is start of multi-line comment We can use these comments for documentation purpose we can write as many as comment till line start with =cut =end This is end of comment =cut
Any sentence between =begin and =end will act as comments and will ignore through Perl.This script will produce below result.
# ./Hello_world Hello World
We can also run perl script with Perl command-line.
# perl Hello_world Hello World
Space Tab New-line character
Any Perl program having space, tab or new-line will not effect its function. Actually Perl doesn’t care about any white space like below examples
# perl -e 'print "Hello World\n"' Hello World perl -e 'print "Hello World\n"' Hello World perl -e 'print > "Hello World\n"' Hello World