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.
AN EXPRESSION IS A PIECE OF PERL CODE THAT EVALUATES TO SOME VALUE.
From the previous perl example:

chomp($name);

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.
Example:
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.
Eg:
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.

Example:
print "subir@computerkorner.org"; <-- Error
print "subir\@computerkorner.org"; <-- 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...

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

------ scalar_usage.pl ------

#!/usr/bin/perl

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\@gmail.com\n";
print "DON'T SPAM MY INBOX\n";
print 'This is
the end of
this
tutorial';
print "\n";

[gray@ckserver Perl Programming]$ perl scalar_usage.pl
Todays date is 10/08/2014\n
It's great to learn perl
My email address : subirsutradhar2014@gmail.com
DON'T SPAM MY INBOX
This is
the end of
this
tutorial
[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!

0 comments: