Академический Документы
Профессиональный Документы
Культура Документы
- A quick-and-dirty Language
Somesh Saraf
20-21 Dec 2006
Table of Contents
Prerequisites
Perl Script
First Perl Script
Data Types
Flow Control
Subroutines
Regular Expressions
File Handling
Prerequisites
Perl Interpreter
ActivePerl- a freely available pre-configured, ready-to-
install package - of Perl on Windows.
Editor
Any conventional Text Editor- NotePad, Wordpad, Editplus
etc.
Running Perl
perl sample.pl
/usr/local/hin/perl sample.pl (Unix)
c:\NTperl\perl sample.pl (Windows)
Perl Script
Anordinary text file containing a series of Perl
commands.
my index = 0;
local count = 0;
my : variables declared using my can live only within
the block it was defined and but are not visible in
the subroutines called inside the block
local : variables declared using local are also visible
in the subroutines called inside the block.
Data Types
Small number of data types:
scalars and arrays
Creative Perl
$Num = 10;
$Str = “Perl Session”;
$Session = $Str.$Num;
Data Types
Numbers
100; # The number 42
100.5; # A Foating point number
100.000; # 100
10E2; # 100
Strings
Can be of any length
Can contain any characters, numbers, punctuation,
special characters (like `!', `#', and `%'),
Can contain special ascii formatting characters like newline,
tab, and the \bell" character.
String Functions
length
Returns the length of the string in bytes.
chomp
Removes line ending characters from a string or array of strings.
chop
Removes the last character from a string or array of strings.
lc
Converts all characters in the string to lower case.
uc
Converts all characters in the string to upper case.
index
This function returns the position of the first occurance of the specified SEARCH
string. If POSITION is specified, the occurance at or after the position is returned.
The value -1 is returned if the SEARCH string is not found.
String Functions
substr
Returns a substring from a string.
Has 3 variants
substr (STRING,OFFSET)
returns all characters in the string after the designated offset from
the start of the passed string.
substr (STRING,OFFSET,LEN)
returns all characters in the string after the designated offset from
the start of the passed string up to the number of characters
designated by LEN.
subst (STRING,OFFSET,LEN,REPLACEMENT)
replaces the part of the string beginning at OFFSET of the length LEN with
the REPLACEMENT string.
Data Types - Arrays
A collection of scalars.
Array Initialization:
@MotoPhones = (“V3i", “Ming", “MotoQ");
Creative Perl
$Weekend = $Saturday or $Sunday;
$Solvent = ($income > 3) and ($debts < 10);
$value > 10 || print "Oops, low value $value ...\n";
$value > 10 && print "OK, value is high enough…\n";
Operators
Relational Operators
Operator Numeric Context String Context
Equality == eq
Inequality != ne
Creative Perl
$a = 10;
Print “Value of input is $a\n” if $debug;
Conditional Expressions
unless :
The statement does not execute if the logical
expression is True and executes otherwise.
while The statement executes repeatedly until the logical expression is False.
unless($directory eq “vxworks” )
{
perl statement;
….
}
Loops – while
while(<condition>)
{
perl statements;
}
The statement executes repeatedly until the logical expression is True.
my $a = 0;
while ($a < 10)
{
print "a -> $a\n";
$a++;
}
Creative Perl
my $a = 0;
my $a = 0;
until ($a > 10)
{
print "a -> $a\n";
$a++;
}
Creative Perl
my $a = 0;
Declaration
sub subroutine-name { statements }
sub PrintInfo
{
print “This is first perl session\n”;
}
Subroutines
Subroutine call
By prefixing the name with the & character.
&PrintInfo;
sub PrintInfo
{
print “This is first perl session\n”;
}
Subroutines
Returning Values from Subroutines
sub GetValue
(@return-a1, @return-a2) = &egsub4;
{
sub egsub4
return $value\n”; {
local(@a1) = (a, b, c);
} local(@a2) = (d, e, f);
return(@a1,@a2);
}
Subroutines
Passing Values to Subroutines
sub PrintError
{
($ValToPrint,$ValLength) = @_;
print “Value is $ValToPrint of Length ,$ValLength \n”;
}
Subroutines
Pass by Reference : Directly use @_
sub Increment
{
@_[0]++;
print “Value after increment $_[0]\n”;
}
Opening a file
open (<FILEHANDLE>, “<Mode><FileName>");
open (LOGFILE, ">/etc/logs/reclaim.log");
File Handling
Symbol Meaning
< Opens the file for reading. This is the default action.
> Opens the file for writing.
>> Opens the file for appending.
+< Opens the file for both reading and writing.
+> Opens the file for both reading and writing.
| (before file Treat file as command into which Perl is to pipe text.
name)
| (after file Treat file as command from which input is to be piped to Perl.
name)
File Handling
Closing a File
close <FILEHANDLE>;
close LOGFILE;
Match Options
Switch Meaning
i Case-insensitive matching
Match Options
Switch Meaning
i Case-insensitive matching
URLS
http://books.perl.org/onlinebooks
Free Perl Books -
freeprogrammingresources.com
http://www.comp.leeds.ac.uk/Perl/