More about PERL Syntax

From the last chapter, we now know how to execute a perl program, and few rules of how to write a basic perl script.

In this page, we will dive more into the syntax of perl and will find out how it looks and handles data.
A perl script contains multiple statements, and we know every statement in perl ends with a semi-colon.
A statement in perl, might contain zero or more expression.
From the previous perl example:


This is a statement contains an expression which is "chomp($name)", and this expression evaluates to some value which is known as the result.

For Perl, every data is SCALAR, which is the simplest kind of data.
SCALAR data might be a string or numbers. We might think of numbers or strings as an entirely different entity, but Perl uses them interchangeably.

Some example of SCALAR data:

"perlbasic".'perl', "456" (strings)
4,7.01, -2 (numbers)

Primary rules:
Strings are always surrounded by single or double quotes.
Numbers should not be surround by any types of quotes.

Strings in Perl are not a set of characters. We know C Language sees a string as a set of characters, but Perl sees a string as a single entity.
The word "perl", which in other programming language like C, "perl" is a combination of "p,e,r,l" which is an array of characters. But in Perl the word or SPECIFICALLY the string is a single entity.

Though strings are enclosed within single or double quotes, but the way Perl looks the string inside them is little different.

In a single quoted string ('string'), what we type is what we get,ie.,anything  starting from the single quote and until the next single quote found will be printed.

So what if we want a word to have a single quote? Example 'don't'.........
In this case or in similar ones, we need to use "\" before the single quote.
print 'don't'; <-- This is an error
print 'don\'t'; <-- Correct way

We know "\n" creates a new line, but when used within single quotes will literally print \n on the screen. This will not create a new line.

Single quoted string can span multiple lines i.e,
print 'twinkle twinkle
little star
how i wonder
what you are';

The above spanning of a single string to multiple lines is accepted.

For double quoted strings, certain ASCII characters have special meaning which we will talk later, for now lets say if we want to print email address or dollar symbol we need to escape the characters with backslash.

\n within double quotes will create a new line.

print ""; <-- Error
print "subir\"; <-- Correct

print "$5"; <-- Error
print "\$5"; <-- Correct

print "\n" will create a new line.
print "\a" will create a short beep.
print "\t" creates a horizontal tab
and so on...

So with all those in mind,  let us check this small perl script:

------ ------


use strict;
use warnings;

print 'Todays date is 10/08/2014';
print '\n';
print "\n";
print 'It\'s great to learn perl';
print "\n";
print "My email address : subirsutradhar2014\\n";
print "DON'T SPAM MY INBOX\n";
print 'This is
the end of
print "\n";

[gray@ckserver Perl Programming]$ perl
Todays date is 10/08/2014\n
It's great to learn perl
My email address :
This is
the end of
[gray@ckserver Perl Programming]$

That's all for this part, with a thought that you all learnt something new............ I'll be happy to read your views on it.

Thank you for reading.

Feel Free To Leave A Comment
If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!