PERL scripting
PERL SCRIPTING
Perl is a programming language developed by Larry Wall, especially designed for text processing. It stands for Practical Extraction and Report Language. It runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This tutorial provides a complete understanding on Perl.
Why to Learn Perl?
Perl is a stable, cross platform programming language.
Though Perl is not officially an acronym but few people used it as Practical Extraction and Report Language.
It is used for mission critical projects in the public and private sectors.
Perl is an Open Source software, licensed under its Artistic License, or the GNU General Public License (GPL).
Perl was created by Larry Wall.
Perl 1.0 was released to usenet's alt.comp.sources in 1987.
At the time of writing this tutorial, the latest version of perl was 5.16.2.
Perl is listed in the Oxford English Dictionary.
Perl Features
Perl takes the best features from other languages, such as C, awk, sed, sh, and BASIC, among others.
Perls database integration interface DBI supports third-party databases including Oracle, Sybase, Postgres, MySQL and others.
Perl works with HTML, XML, and other mark-up languages.
Perl supports Unicode.
Perl is Y2K compliant.
Perl supports both procedural and object-oriented programming.
Perl interfaces with external C/C++ libraries through XS or SWIG.
Perl is extensible. There are over 20,000 third party modules available from the Comprehensive Perl Archive Network (CPAN).
The Perl interpreter can be embedded into other systems.
Applications of Perl
As mentioned before, Perl is one of the most widely used language over the web. I'm going to list few of them here:
Perl used to be the most popular web programming language due to its text manipulation capabilities and rapid development cycle.
Perl is widely known as "the duct-tape of the Internet".
Perl can handle encrypted Web data, including e-commerce transactions.
Perl can be embedded into web servers to speed up processing by as much as 2000%.
Perl's mod_perl allows the Apache web server to embed a Perl interpreter.
Perl's DBI package makes web-database integration easy.
Perl is Interpreted
Perl is an interpreted language, which means that your code can be run as is, without a compilation stage that creates a non portable executable program.
Traditional compilers convert programs into machine language. When you run a Perl program, it's first compiled into a byte code, which is then converted ( as the program runs) into machine instructions. So it is not quite the same as shells, or Tcl, which are strictly interpreted without an intermediate representation.
It is also not like most versions of C or C++, which are compiled directly into a machine dependent format. It is somewhere in between, along with Python and awk and Emacs .elc files.
To write a perl script we shall get into few commands:
To interface perl in file: #! /usr/bin/perl
To start perl environment: type “perl” in the prompt
perl -v : in prompt gives the package details if installed, if not then we need to install the package.
There are few options with perl:
perl -e: it runs files with programs
perl file: runs perl script from given file
perl -d: runs programs under debugger
The perl files have .pl as their extension.
As in tcsh and tcl we need to give executable permission to the perl file to run it.
To print some text: print “text”;
semicolon (;) it is used to break the line and run.
To assign a value to a variable:
This can be done using 3 methods:
scalar, array,hash,
Scalar
Scalars are simple variables. They are preceded by a dollar sign ($). A scalar is either a number, a string, or a reference. A reference is actually an address of a variable, which we will see in the upcoming chapters.
syntax: my $variable = value;
print $a;
Arrays
Arrays are ordered lists of scalars that you access with a numeric index, which starts with 0. They are preceded by an "at" sign (@).
syntax: my @arrayname = qw(array list);
print @a;
Hashes
Hashes are unordered sets of key/value pairs that you access using the keys as subscripts. They are preceded by a percent sign (%).
syntax: my %hash_name = (‘key1’, value, ‘key2’, value,...);
print “$hash_name{‘key’}”;
Scalar variables:
i. To assign value to a scalar variable:
syntax: my $varaible = value
eg: my $a = 10
print $a
ii. we can do maths using scalar variable:
my $a = 10 + 20
print $a
output: 30
iii. scalar concatenation:
We can concatenate two different variables using “.”
iv. The white space is not a problem in perl
v. accessing the list from scalar variable:
syntax: my $variable = (list)[index];
print $variable;
Array variables:
i. To assign value to array variable.
syntax: my @array = qw(array_list);
print @array
ii. accessing the index of array
The index starts with 0 and the array is converted into scalar while accessing each index of an array.
syntax: my @a = (1, 2, 3, 4);
print $a[1];
print $a[-1];
output: 2 4
Therefore, -1 starts printing output from end of the array elements.
iii. To work on sequence of numbers or alphabets, i.e. range operator:
.. -> is known as range operator, it prints all the values within the specified range.
Eg: my @a = (1..5);
Print @a;
Output: 1 2 3 4 5
iv. array size:
To get size of array we need to convert it into scalar using “$” and use “#” to get the count.
v. adding and removing elements of an array:
push: It adds element to end of array items.
pop: It deletes last element from array list and prints removed element.
unshift: adds one element at the beginning of an array.
shift: remove element from the beginning of an array.
vi. array slicing:
You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array.
vii. Replacing array elements:
syntax: splice@array, offset[,length[,list]];
This function will remove the elements of @ARRAY designated by OFFSET and LENGTH, and replaces them with LIST, if specified. Finally, it returns the elements removed from the array.
viii. sorting array:
Sorting occurs in ASCII order.
syntax: @array = sort(@array);
ix. merging arrays:
Hash:
A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets.
To assign a value to the key we can use “=>” or separate each list of item with “,”
syntax: my %hash = (‘key’=>value,’key’=>value);
i. slicing hash elements:
We need to convert hash list into array to slice the elements.
syntax: my %hash = (‘key’=>value,’key’=>value);
my @array = @hash{‘key’}’
print @array;
ii. We can extract keys from hash.
This is done by taking key and hash name to an array and accessing each key as index of the array and printing using scalar variable.
syntax: my %hash = (‘key’=>value,’key’=>value);
my @array = keys %hash;
print $array[index];
iii. To print size of hash:
iv. To add or remove an element from hash
syntax: my %hash = (‘key’,value,’key’,value)
$hash{‘new_key”} = value;
print %hash;
delete $hash{key_to_delete};
print %hash;
Conditional statements:
Perl supports decision making using if, if-else, if-elseif-else, unless statements accessing over boolean expression or statements.
i. if :
syntax: if(boolean expression) {
#statement
}
ii. if-else :
if(boolean expression) {
#true statement
} else {
#statement
}
iii. if-elseif-else:
syntax: if (boolean expression) {
#statement
} elsif(boolean expression) {
#statement
} else {
#statement
}
iv. unless:
syntax: unless (boolean expression) {
#statement
}
unless statement prints if the given expression is false .
We can work on unless, unless-elsif-else, unless-else
v. switch:
vi. conditional operator:
syntax: expression1 ? true:false;
Loops:
Perl supports to run a block for multiple times using few keywords.
i. while loops:
syntax: initialization
while(condition) {
#statement
increment
}
ii. for loop:
syntax: for(initialization; condition; increment) { #statement }
iii. foreach loop:
syntax: foreach variable (list) { #statement }
iv. do-while loops:
syntax: do { #statement }while(condition);
v. until loops:
until(condition) { #statement }
When the condition is true the loop stops before the previous condition and prints output.
vi. nested loops:
Any loops discussed above can have nested i.e. having loop inside a loop.
Loop control statements:
We can have control over the iteration using few keywords.
i. next:
It skips the particular condition that has been given.
ii. last:
The condition that has been mentioned with last command is the final iteration and output prints till the previous iteration.
iii. continue:
syntax: while(condition) { #statement } continue { increment }
iv. redo:
It skips the given condition and continues from next condition.
v. goto:
There are three types in goto : a. label b. expr c. goto&name
goto LABEL:
goto EXPR
Operators:
To do specific mathematical or logical operations perl supports few in-built operators.
i. Arithmetic operator: It does addition(+), subtraction(-), multiplication(*), division(/), modulus(%) and exponent operations(**)
ii. Equality operators: It checks equality, greater than , less than between two operands.
The most used equality operators are ==,!=,<,>,>=,<= but perl supports another operator for comparison i.e. <=> if both are equal output will be 0, if LHS is less than RHS then prints -1, if LHS is greater than RHS prints 1.
Operator description: For doing equality operations we have even the descriptive keywords. lt,gt,le,ge,ne,eq,cmp.
iii. Assignment operators: The RHS value is assigned to LHS and can be performed upon doing arithmetic operation parallel with assigning.
It uses operators like: ==, +=, -=, *=, /=, %=, **=
iv. bitwise operators: It performs binary operations on the operands. The operators used are &,|,^,~,<<,>>
v. logical operators: It performs logical operations like and, or, xor, not etc.
Date and time:
Using keyword “localtime” with scalar variable prints the current date, month,year,day and time.
same as localtime() we have “gmtime()” which “Green which” time zone and prints the date, time, year, month, day of that time zone.
With the help of “printf” we can format the time and print.
Using “time()” we can print how much time has been elapsed till now since the given date.
Subroutines:
Perl supports performing certain tasks multiple times without writing code each time when it has to be executed. This process is replaced with a single calling keyword which is defined in subroutine making it handy for the users.
It has two parts :
definition: It has the code inside.
calling: it has the subroutine name, once we call the name the compiler goes to line where the name has be defined and executes the code.
syntax for definition: sub subroutine_name { #statement }
syntax for calling: subroutine_name (arguments);
i. To pass an argument:
ii. To pass hash:
iii. global variable:
The subroutine checks for the variable if it is declared inside the definition then that value will be taken and the value declared outside definition will be taken only if it is not declared in the definition.
iv. local variable:
The local is mostly used when the current value of a variable must be visible to called subroutines. If more than one variable or expression is given to local, they must be placed in parentheses. This operator works by saving the current values of those variables in its argument list on a hidden stack and restoring them upon exiting the block, subroutine, or eval.
References: It is a scalar that refers to a value. The largest value can be scalar, array, hash or a function. You can create a reference with “\”.
syntax to create reference: $var = \$scalar or \@array or \%hash or \&func.
syntax to print the reference type: ref($var)
syntax to print the scalar or array or hash values: print $$var -> scalar
print @$arr -> array
print %$hash -> hash
syntax:= to de-reference:
Function reference:
Formats:
File handling: This is text processing; each record is converted into a list. We can display or even edit a file. There are three basic file handling methods : STDIN, STDOUT, STDERR.
We have few access modes which indicates what actions to be performed on that file.
Access modes : > = write, < = read, >> = append, +< = read & write, +> = write & read.
Write and append modes creates a file if it does not exist but read mode requires pre-existing file. Write mode overwrites data into the file.
We have user defined channel ID which is used to perform the action on a file.
To open a file: open (my $fh, “< $filename”) or die “$!”;
note: $! is error operator.
example: open(my $fh1 “<file1”) or die “$!”;
open(my $fh2 “>$file2”) or die “$!”;
while(my $line = <$fh1) {
print $fh2 $line;
}
close $fh1;
close $fh2;
Note: if a file is opened then it has to be closed to perform further action.
i. File information:
We can check if the file exists or if it has any of the access mode using perl file information operators.
-B = is it a binary file
-O = is file owned by real user ID
-R = is file readable to real user ID or the group
-T = is it a text file
-X = is file executable by real user ID or the group
-b = is it a block special file
-d = is it file or directory
-e = does file exists
-f = is it plain file
-s = returns size of file
-w = does file have writable access to effective user or the group
-z = is file size zero.
Error handling: When we are working on a file and there occurs some errors like file may not exist or the content in the file may have errors at that situation one has to know how to handle them and perl is versatile that is giving us an option to handle such errors.
“$!” is known as an error handling operator.
if you want to open a file and do not if it exists or not then we can go with the option of opening if exists and if not prompt an error.
eg: open (my $fh “>file”) or die “$!”;
so die kills my operation and $! will prompt what was the error.
We have two error handling functions : die and warn
die: It kills the execution and exits.
warn: it skips that particular execution and continues with the rest.
Regular expressions: It is used to match strings of patterns.
i. To match pattern : =~ /match word/
ii. To ignore case : =~/word/i
iii. Special variables in regular expression:
$`: contains if last grouping match has been matched.
$& : contains entire matched string.
$’: contains everything after matched string.
matches end of line
^: matches start of the line
. : matches any single character except new line, to match new line use “m”.
[...] : it matches any character inside the [].
[^...] : matches any character that is not in [].
* : matches 0 or more occurrences.
: match 1 or more occurrence
? : matches 0 or 1 occurrence
Sending mail: Perl has the facility to send mail without opening the mailbox.
Format to send mail:
$to = <to address>;
$from = <from address>;
$subject= <subject>;
$message= <message>;
open(MAIL, “|path of mail -t”);
#email header:
print MAIL “TO: $to\n”;
print MAIL “FROM: $from\n”;
print MAIL”Subject: $subject\n”;
#email body:
print MAIL $message;
close(MAIL);
print “email sent successfully\n”;
Format to attach file and add cc and bcc:
$to = <to address>;
$cc = <cc address;>;
$bcc = <bcc address>;
$from = <from address>;
$subject= <subject>;
$message= <message>;
$msg->attach (type => ‘.img or.pdf’,
path => ‘path of file’,
filename => ‘name’,
disposition => ‘attach’,
);
$msg->send;
print “email sent successfully”;
Comments
Post a Comment