Understanding Syntax in Perl Language

Understanding Syntax in Perl Language :

Similar to other programming language Perl script contains more than one statement, so it is very necessary to first understand syntax before proceeding further. So we will look at syntax used in Perl language To start with lets first consider syntax used in the program to print Hello World using Perl Script.

1. Comments are written using the hash symbol and is valid to end of the line only.
 # Perl program to print Hello World   

Now we will see the syntax related with print command.

2. As you can see in the print statement Perl statement always ends with semicolon.
print "Hello World"; 

3.White spaces don’t have any meaning and irrelevant in terms of coding perspective.
print        "Hello World"; 

4.As you can see the double quotes are used around the String literal in print statement. Similarly you can also use the single quote around the String Literal
print 'Hello World'; 

5.Numbers do not need double or single quotes around them.
print 420; 

6.Wrapping around using Double quotes is most preferred way as it can interpolate the special characters and variables such as new line character. Lets see using the example below in image:

In above example we are using double quotes and single quotes to print the Hello World but output comes as
worldHello\n $world

7.If we write print "Hello
world ";
Then it will introduce line break between Hello and world.

8.If we write print "Hello" "world ", it is not allowed it will throw error as
String found where operator expected at HelloWorld.pl line 5, near ""Hello" "world ""
(Missing operator before "world "?)
syntax error at HelloWorld.pl line 5, near ""Hello" "world ""
But if we add comma in between two Strings or arguments it will print Helloworld

9. Writing print("Hello, world\n"); and print "Hello, world\n"; is the same thing, parentheses for function is optional.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment