Professional Documents
Culture Documents
Beginning Perl: Written By: Abhishek G. Nair
Beginning Perl: Written By: Abhishek G. Nair
Written by:
ABHISHEK G. NAIR
INTRODUCTION TO PERL
Perl
WHY PERL?
Perl
Scripting
Data transformations
System administration
Network programming
Graphics programming
PERL RESOURCES
On
HELLO WORLD
#!/usr/bin/perl w
print Hello World./n
Anything
followed by # is a comment,
except for #!, which is called a
shebang. This tells UNIX where to look
for the Perl interpreter. On Windows, the
location of the Perl interpreter should be
saved as an environment variable.
The w option shows warnings, if any. For
Perl 5.6 and later, write: use warnings;
ADDING ENVIRONMENT
VARIABLES
When
Keywords-
Or
print(This, does, the, same, as
above\n);
ESCAPE SEQUENCES
\n
NUMBER SYSTEMS
Octal
VARIABLES
A
variable, as opposed to a
constant, is a placeholder for
data that may change.
Variables in Perl may be scalars,
lists or hashes.
Scalars are of two kindsnumbers and strings.
Numbers could either be integers
or floats.
PRINTING NUMBERS
#!/bin/usr/perl
use warnings;
use strict;
print(25,-4);
What do you think is the output? How
do you change it?
PRINTING NUMBERS
You could write:
#!/bin/usr/perl
use warnings;
use strict;
print(25_000_000, ,-4, \n);
The underscores are ignored by Perl
and are only for readability.
FLOATING POINT
NUMBERS
Just
STRINGS
Strings
print("C:\\Windows\\System\\\n");
would print:
C:\Windows\System\
What would:
print('C:\Windows\System\',"\n");
and:
print('ex\\ er\\','ci\' se\'',"\n");
print?
QUOTATION MARKS
There is no problem with putting single quotes
as easy as that.\n");
would print:
It's as easy as that.(Return)
print('"Stop,"
he cried.',"\n");
would print:
Stop, he cried. (Return)
ALTERNATIVE DELIMITERS
You can use qq// and q/
HERE-DOCUMENT
You can also place a large amount of text within
OPERATORS IN PERL
Perl performs calculations and manipulations using either
MORE OPERATORS
**- the exponentiation operator. Raises a
number to a power.
-, the unary minus operator. Negates the
operand.
The modulo operator, %, returns the
remainder of a division. It has the same
precedence as the / and * operators.
BITWISE OPERATORS
Bitwise operators process bits. On numbers, they
LOGICAL OPERATORS
Logical operators work on TRUE and FALSE.
OTHER LOGICAL
OPERATORS
&& and || perform logical AND and logical OR
STRING OPERATORS
Concatenation operator .- It works just like ,
STRING COMPARISON
To compare the values of two characters, we
PRECEDENCE LIST OF
OPERATORS
VARIABLES
Scalar Variables begin with a $ (this is called a
sigil). A
value, either numeric or string, is put in the variable with a
= (assignment operator).
The assignment operator has very low operator; everything
on its right is evaluated and put into what is on its left.
Modifying a variable is easy; just assign something
different into it.
Modifying and assigning at once is also possible, using the
shorthand which is popular in C:
a = a <operator> b;
a <operator>= b;
AUTOINCREMENT/DECREMENT
++ and -- operators work differently when they
MULTIPLE ASSIGNMENTS
$a = $b = $c = 1;
SCOPING
Scoping refers to the visibility of a variable.
INTERPOLATION
Interpolation of variable names occurs only in
ASSIGNMENT 1
Write a program to ask the user for a
ASSIGNMENT 1
Without using Perl, work out the values of the
following expressions:
(1) 2+6/4-3*5+1
(2) 17+-3**3/2
(3) 26+3^4*2
(4) 4+3>=7||2&4*2<4