We got familiar with the basics of Perl in our last tutorial: first perl program. In this tutorial, we will learn few important points regarding Perl syntax and naming convention.
Perl file naming convention
Perl file name can contain numbers, symbols, letters and underscore (_), however spaces are not allowed in the file name. for e.g. hello_world.pl is a valid file name but hello world.pl is an invalid file name.
Perl file can be saved with .pl or .PL extension.
Difference between single and double quotes in Perl
Single and double quotes behaves different in Perl program. To understand the difference, lets have a look the code below:
#!/usr/bin/perl print "Welcome to\t Beginnersbook.com\n"; print 'Welcome to\t Beginnersbook.com\n';
This would produce the following output:
Welcome to Beginnersbook.com Welcome to\t Beginnersbook.com\n
You can clearly see the difference that double quotes interpolated escape sequences “\t” and “\n” however single quotes did not.
#!/usr/bin/perl $website = "Beginnersbook"; print "Website Name: $website\n"; print 'Website Name: $website\n';
Website Name: Beginnersbook Website Name: $website\n
Use of Single quotes
You may be thinking of avoiding single quotes in perl program, however there are certain cases where you would want to use single quotes.
For e.g. If you want to store email address in a variable, in that case double quotes would throw an error, you need to use single quote in that case. For e.g.
$email = “[email protected]”; would throw an error
$email = ‘[email protected]’; would work fine.
Backslash in Perl
The backslash can perform one of two tasks:
1) it takes away the special meaning of the character following it. For e.g. print “\$myvar”; would produce $myvar output instead of variable myvar value because the backslash(\) just before the $ takes away the special meaning of $
2) It is the start of a backslash or escape sequence. For e.g. \n is an escape sequence used for newline
what is the use of it?
Believe me, you would use it frequently while developing an application in perl. Lets say you want to print the string that contains double quotes. For e.g. If i want to print: Hello This is “My blog” then I would need to use following logic:
#!/usr/bin/perl $msg = "Hello This is \"My blog\""; print "$msg\n";
Hello This is "My blog"