Perl Programming
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 1/122
Perl Programming
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 2/122
5 May 2013
What you can do with Perl
What you cannot do with Perl …
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 3/122
5 May 2013
Smallest perl script
#!/usr/bin/perlprint “Hello Symmetrix QA\n”;
Perl file extensions
.pl (perl script)
.pm( perl module)
perl <filename.pl> to execute a perl script
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 4/122
Programming Constructs
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 5/122
5 May 2013
Variables
All variables (scalars) starts with $.
A variable name may contain alphanumeric characters and underscore.
Case matters.
A simple variable can be either a string or floating point number, but does not need to be declared as any specific type.
Perl has a number of predefined variables (sometimes used), consisting of $ and a single non-alphanumeric
character.
Examples: $var1, $i, $MyCount, $remember_this.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 6/122
5 May 2013
Numbers and operators
Numbers are assigned in a ”natural” manner;
$num = 1;
$num = 324.657;
$num = -0.043;
Standard numeric operators:
+ - * / ** %
Bitwise operators:
| (or) & (and) ^ (xor) ~ (not) >> (rightshift) << (leftshift)
increment and odecrement:
++ --
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 7/122
5 May 2013
Strings
Strings are assigned with quotes:
$string = ’This is a literal string’;
$string = ”This is an $interpolated string\n”;
Interpolated strings are searched for variables and special character combinations that has meaning, like \n fornewline and \t for tab.
If a number is used in a string context then it is changed to a string and vice versa.
String operators:
. (concatenation) x (repetition)
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 8/122
5 May 2013
Example
#!/usr/bin/perl
$foo="foobar";
$str1='$foo'; # a Variable enclosed in single quotes
$str2="$foo";# a variable enclosed in double quotes
print "[$str1]\n";
print "[$str2]\n";
$wait=<STDIN>;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 9/122
5 May 2013
example
#!/usr/bin/perl
my ($str1,$str2,$str3);
$str1="hello";
$str2="world";
$str3=$str1.$str2;
print "\$str3=[$str3]\n"; # helloworld
$str4=$str1 x 5; #hellohellohellohellohello
print "\$str4=[$str4]\n"; #
$wait=<STDIN>;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 10/122
5 May 2013
Conditional statement
A standard if statement
if ( predicate ) {
# this will be executed if the predicate is true
}
if statements exists in various forms in perl
if ( predicate ) {
# this will be executed if the predicate is true}
elsif ( predicate2 ) { # no spelling mistake# this will be executed if this predicate is true
}else {
# finally this is excuted if no predicates where true
}Can be turned around
unless ( predicate ) {
# this will be executed if the predicate is false
}
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 11/122
5 May 2013
Predicates
Predicates are simple boolean expressions that can be stringed together via boolean operators forming complex
logic.
Numerical comparison operators:
< > <= >= == != <=>
String comparison operators:
lt gt le ge eq ne cmp
Boolean operators:
&& and || or ! not xor
Examples:
$age > 18 and $height < 1.4
($name eq ’Peter’ or $name eq ’Chris’) and $wage <= 25000
Perl is using short-circuit (lazy) evaluation.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 12/122
5 May 2013
Loops - while
The standard while loop.
# some initialization
while ( predicate ) {
# code which is executed while the predicate is true
}
There are various forms of the while loop:
until ( predicate ) {
# code which is executed while the predicate is false
}
do {
# code
} while ( predicate );
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 13/122
5 May 2013
Loops - for Perl has the standard for loop:
for(init; predicate; increment) {}
for($i = 0; $i < 10; $i++) {
# code executed 10 times
}
foreach $i (1..10)
{
# code executed 10 times
}
A infinite loop is often written as
for (;;) {
# code executed forever
}
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 14/122
5 May 2013
Loops - control
There are 3 loop control primitives that can be used in all forms of loops:
last
breaks (ends) the loop
next
starts the loop from the top and executes the predicate
redo
starts the loop from the top, do not execute the predicate
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 15/122
5 May 2013
Shorthand notation
Often if statements and sometimes loops only has one line of code to be executed in the block. Perl has a
shorthand notation for that.
if ($age > 80) {
print ”Old \n”;
}
Shorthand
print ”Old \n” if $age > 80;
$x = 0 unless $x > 0;
print ”$i\n” for ($i = 1; $i <= 10, $++);
As seen the structure of the statement is turned around.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 16/122
5 May 2013
Output – printing to screen
The print statement prints a comma separated list of values.
print ”Hello world\n”;
print ’Result is ’, $num1 + $num2, ” \n”;
print ”My name is $name\n”;For better output formatting use printf, which is similar to the C function.
printf (”%02d/%02d %04d\n”, $day, $month, $year);
printf (”Sum is %7.2f\n”, $sum);
The output of print(f) goes to the last selected filehandle unless otherwise specified. This is usually STDOUT,
which is usually the screen.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 17/122
5 May 2013
Input – getting it from the keyboard
The keyboard is usually STDIN unless redirection is in play.
Lines are read from the keyboard like any lines are read from a filehandle.
$line = <STDIN>;
Perl is mostly used in applications where linebased I/O makes sense, even though Perl can do other types of
I/O.
When reading a line, it is important to realize that a line ends with a newline, which is part of what is read.
You have to get rid of that newline so often that Perl has a function for that:
chomp $line;
If there is no input on the line (EoF, EoT) then $line is assigned the undefined value. There is a function for
checking that, too.
if (defined $line) {}
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 18/122
5 May 2013
Executing External Programs
system, exec and backtiks are used for executing external programs:
system LIST;
system PROGRAM LIST;
ex:
$cmd=“type example1.pl example2.pl”;
$rval=system($cmd);
OR
@cmd2=(“type”,”example1.pl”, “example2.pl”); $rval=system(@cmd2);
Note: System returns the control to the program
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 19/122
5 May 2013
exec
exec LIST
exec PROGRAM LIST
exec is similar to system.
NOTE: exec if successful does not return control to the program
BACK TICKS ( accent graves) ` `
executing an external command using backticks helps to retrive the output.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 20/122
5 May 2013
A simple Perl program#!/usr/bin/perl
print ”Menu\n”;
print ”1. Host Info\n”;
print ”2. List files\n”;
print ”Enter Choice : ”;
$ch=<STDIN>; # STDIN to take input from Keyboard
chomp $ch; #remove the trailing \n at the end
if( $ch == 1)
{
system(”ver”);
}
elsif($ch==2)
{
system(”dir”);
}
else
{
print ”INVALID CHOICE\n”;
}
print ”Done..\n”;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 21/122
5 May 2013
another simple program ...
#!/usr/bin/perl
use strict;
my ($cmd,@output,$d,$wait);
$cmd="dir";
@output=`$cmd`;
foreach $d (@output)
{
print "\$d = $d\n";
}
$wait=<STDIN>;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 22/122
5 May 2013
Strict Perl
Perl is a rather loose and forgiving language. This can be improved somewhat by using strict.
#!/usr/bin/perl – w
use strict;
This will enforce variable declaration and proper scoping, disallow symbolic references and most barewords.This is a good thing as some stupid errors are caught and the code is more portable and version independant.
Variables are declared by the key word my and are private (local) to the block in which they are declared.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 23/122
5 May 2013
Scope (lexical)
A block can be considered as the statements between { and }.
A variable declared with my is known only in the enclosing block.
Only the ”most recent” declared variable is known in the block.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 24/122
5 May 2013
Opening files
The modern open is a three parameters function call.
open(FILEHANDLE, $mode, $filename)
The usual file modes are:
< reading
> writing
>> appending
+< reading and writing
|- output is piped to program in $filename
-| output from program in $filename is piped (read) to Perl
open(IN, ’<’, ”myfile.txt”) or die ”Can’t read file $!\n”;
close IN;
#!/usr/bin/perl
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 25/122
5 May 2013
puse strict; my(@ips,$ip,$invalid,$rec,$wait);@ips=qw(127.0.0.1 10.243.188.36 10.243.199.99);
foreach $ip (@ips){
$invalid=0;
@res=`ping $ip`;
# print "@res\n";
foreach $rec (@res){
if( $rec =~ /unreachable/){
$invalid=1;}
}if($invalid==1){
print "$ip is not reachable\n";}else{
print "$ip is reachable\n";}
}$wait=<STDIN>;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 26/122
5 May 2013
Simple program to Read from a FILE
#!/usr/bin/perl – w# Summing numbers in a file
use strict;
print "Enter a FILENAME "; my $filename = <STDIN>;chomp $filename;
open(IN, ’<’, $filename) or die "Error: $!\n";
my $sum = 0;# standard way of reading a file line by line in Perl
while (defined (my $line = <IN>)){
chomp $line;$sum += $line;
}
print "The sum is $sum\n";
<<data1>>1020
304050
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 27/122
5 May 2013
File system functions
exit$optional_error_code; #to exit a program with err code
die ”This sentence is printed on STDERR”; # error message printing
unlink $filename; # do delete a file
rename $old_filename, $new_filename; # to rename a file
chmod 0755, $filename; # to change file permissions
mkdir $directoryname, 0755; # to create a directory
rmdir $directoryname; # to remove a directory
chdir $directoryname; # to change a directory
opendir(DIR, $directoryname); # to open a directory
readdir(DIR); # to read a dir
closedir DIR; # to close a dir
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 28/122
5 May 2013
File test operators
There is a whole range of file test operators that all look like –X.
print ”File exists” if –e $filename;
Some of the more useful are:
-e True if file exists
-z True if file has zero size-s Returns file size
-T True if text file
-B True if binary file
-r True if file is readable by effective uid/gid
-d True if file is a directory
-l True if file is a symbolic link
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 29/122
5 May 2013
String functions 1
Remove a trailing record separator from a string, usually newline
my $no_of_chars_removed = chomp $line;Remove the last character from a string
my $char_removed = chop $line;
Return lower-case version of a string my $lstring = lc($string);
Return a string with just the first letter in lower case
my $lfstring = lcfirst($string);Return upper-case version of a string
my $ustring = uc($string);Return a string with just the first letter in upper case
my $ufstring = ucfirst($string);
Get character this number represents
my $char = chr($number);Find a character's numeric representation
my $number = ord($char);
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 30/122
5 May 2013
String functions 2
Strings start with position 0
Return the number of charaters in a string my $len = length($string);
Find a substring within a string
my $pos = index($string, $substring, $optional_position);Right-to-left substring search
my $pos = rindex($string, $substring, $optional_position);Flip/reverse a string
my $rstring = reverse $string;Formatted print into a string (like printf)
sprintf($format, $variables…);
Get or alter a portion of a string
my $substring = substr($string, $position);my $substring = substr($string, $position, $length);substr($string, $position, $length, $replacementstring);
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 31/122
5 May 2013
Arrays
Arrays are denoted with @. They are initalized as a comma separeted list of values (scalars). They can contain
any mix of numbers, strings or references. The first element is at position 0, i.e. arrays are zero-based. There is
no need to declare the size of the array except for performance reasons for large arrays. It grows and shrinks as
needed.
my @array;
my @array = (1, ’two, 3, ’four is 4’);
Individual elements are accessed as variables, i.e. with $
print $array[0], $array[1];
Length of an array.
scalar(@array) == $#array + 1
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 32/122
5 May 2013
some examplesforeach $i (1..99)
{
$sum+=$i;
print "\$i = $i\n";
}
print "\$sum = $sum\n";
foreach $i ('a'..'z')
{
print "\$i = $i\n";
}
my $z;
my @l2=qw(jan feb march);print (@l2)."\n";
print $l2[0], $l2[1], $l2[2];
print "there are $#l2 items in @l2\n";
my @list1=(1,2,3,5,7,11,13,17,19,23);
my @list2=@list1[3..5];print "\@list2 = @list2\n";
@greets=qw(hi hello bye);
print "$greets[1] $greets[11] $greets[-1]\n";
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 33/122
5 May 2013
Array slices
You can access a slice of an array.
my @slice = @array[5..8];
my @slice = @array[$position..$#array];
my ($var1, $var2) = @array[4, $pos];Or assign to a slice.
@array[4..7] = (1, 2, 3, 4);
@array[$pos, 5] = @tmp[2..3];
Printing arrays
print @array, ”@array”;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 34/122
5 May 2013
Iterating over arrays
A straightforward for-loop.
for (my $i = 0; $i <= $#array; $i++) {
print $array[$i]*2, ” \n”;
}The special foreach-loop designed for arrays.
foreach my $element (@array) {
print $element*2, ” \n”;
}
If you change the $element inside the foreach loop the actual value in the array is changed.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 35/122
5 May 2013
bubble sort#!/usr/bin/perl
my ($n,@list,$i,$j);
print "Enter n\n";$n=<STDIN>;
print "Enter $n numbers \n";for($i=0;$i<$n;$i++){chomp ( $list[$i]=<STDIN>);
} print "List before sorting \n";foreach $i (@list){
print "$i \n";}
#sorting the arrayfor($i=0;$i<$n-1;$i++){for($j=0;$j<$n-1-$i;$j++){
if($list[$j]>$list[$j+1]){
($list[$j],$list[$j+1])=($list[$j+1],$list[$j]);}
}}
print "List after sorting\n"; map {print "$_\n"} @list;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 36/122
5 May 2013
Array functions 1
Inserting an element in the beginning of an array
unshift(@array, $value);
Removing an element from the beginning of an array
my $value = shift(@array);
Adding an element to the end of an array
push(@array, $value);Removing an element from the end of an array
my $value = pop(@array);
Adding and/or removing element at any place in an array
my @goners = splice(@array, $position);my @goners = splice(@array, $position, $length);
my @goners = splice(@array, $position, $length, @tmp);
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 37/122
5 May 2013
Array functions 2
Sorting an array.
@array = sort @array; # alphabetical sort
@array = sort {$a <=> $b} @array; # numerical sort
Reversing an array.
@array = reverse @array;
Splitting a string into an array.
my @array = split(m/regex/, $string, $optional_number);
my @array = split(’ ’, $string);
Joining an array into a string
my $string = join(” \n”, @array);
Find elements in a list test true against a given criterion @newarray = grep(m/regex/, @array);
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 38/122
5 May 2013
Predefined arrays
Perl has a few predefined arrays.
@INC, which is a list of include directories used for location modules.
@ARGV, which is the argument vector. Any parameters given to the program on command line ends up
here.
./perl_program 1 file.txt
@ARGV contains (1, ’file.txt’) at program start.
Very useful for serious programs.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 39/122
5 May 2013
Hash data structure
A hash also called an associative array, an element of the structure represents a pair of twoparts – a key and a value
Defining a hash :
my %variable;initializing a hash:
my %months = (“jan” => 31, feb =>28,
“mar” =>31); assiging a value to a hash
$months{apr}=30;
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 40/122
5 May 2013
functions : keys & values
syntax: keys HASH
will return the keys in the hash in a list context and will return the count in scalar context
ex:my %months = (“jan” => 31,
feb =>28,
“mar” =>31);
@lst=keys %months; # will return (jan, feb, mar)
syntax: values HASHwill return the values in the hash in a list context and will return the count in scalar context
ex:
my %months = (“jan” => 31,
feb =>28,
“mar” =>31);
@lst=values %months; # will return (31, 28, 31)
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 41/122
5 May 2013
printing the hash (note: there is no definite order)
method 1:
foreach $k ( keys %hash1)
{
print “KEY = $k VALUE = $hash1{$k}\n”;
}
method 2:
while( ($k,$v) = each(%hash1))
{
priny “KEY = $k VALUE = $v\n”;
}
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 42/122
5 May 2013
ex:
#!/usr/bin/perl
my (@list, $rec,%hash1, $osname, $count);
open(IN,"<","data2");
@list=<IN>;
foreach $rec (@list)
{
chomp $rec;@rs=split / /,$rec;
$hash1{$rs[1]}+=1;
}
while( ($osname,$count)=each (%hash1) )
{print "$osname = $count\n";
}
<<DATA FILE>>
lqtf101 unix
lqtf103 aixlqtf112 unix
lqtf114 solaris
lqtf119 linux
lqtf121 aix
lqtf128 solarislqtf139 unix
lqtf142 linux
lqtf144 aix
lqtf149 aix
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 43/122
Pattern MatchingRegular Expressions
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 44/122
5 May 2013
Pattern matching operators
Operator Description
m/PATTERN/ This operator returns true if PATTERN is found in
$_ .
s/PATTERN / REPLACEMENT/ This operator replaces the sub- string matched byPATTERN with REPLACEMENT.
tr/CHARACTERS / REPLACEMENTS/ This operator replaces characters specified byCHARACTERS with the characters inREPLACEMENTS.
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 45/122
5 May 2013
examples
ex 1:
$needToFind = "bbb"; $_ = "AAA bbb AAA";
print "Found bbb\n" if m/$needToFind/;
ex 2:
$target = “EMC"; open(INPUT, "<data.dat");
while (<INPUT>){
if (/$target/)
{
print "Found $target on line $.";
}
}close(INPUT);
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 46/122
5 May 2013
The Substitution Operator (s/// )
$needToReplace = "bbb";
$replacementText = "1234567890";
$_ = "AAA bbb AAA";$result = s/$needToReplace/$replacementText/ ;
exa:
#!/usr/bin/perl
open (IN,"tmpdata.txt");
@list=<IN>;
$str=join ("\n",@list);
print "$str\n";
$str=~ s/\b(\w+)\b/ucfirst($1)/ge;
print "$str\n";
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 47/122
5 May 2013
Options for s///
Option Description
e This option forces Perl to evaluate the replacement pattern as an expression.
g This option replaces all occurrences of the pattern in the string.
i This option ignores the case of characters in the string.
m This option treats the string as multiple lines. Perl does some optimization by assuming that$_ contains a single line of input. If you know that it contains multiple newline characters,use this option to turn off the optimization.
o This option compiles the pattern only once. You can achieve some small performance gainswith this option. It should be used with variable interpolation only when the value of thevariable will not change during the lifetime of the program.
s This option treats the string as a single line.
x This option lets you use extended regular expressions. Basically, this means that Perl ignoreswhitespace that is not escaped with a backslash or within a character class. I highly
recommend this option so you can use spaces to make your regular expressions mor ereadable. See the section "Example: Extension Syntax" later in this chapter for moreinformation.
The Translation Operator (tr///) for translating single
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 48/122
5 May 2013
The Translation Operator (tr/// ) for translating singlecharacterstr/a/z/
tr/ab/z/;
tr/WWW/ABC/;
Option Description
c This option complements the match character list. In other words, thetranslation is done for every character that does not match the character list.
d This option deletes any character in the match list that does not have acorresponding character in the replacement list.
s This option reduces repeated instances of matched characters to a singleinstance of that character
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 49/122
5 May 2013
Regular Expression Meta-Characters, Meta-Brackets, and Meta-Sequences
Meta-Character Description
^ matches the beginning of a line.
. match any character except for the newline unless the /s option is specified.
$ match the end of a line
| alternation - lets you specify two values that can cause the match to succeed. For EXAMPLE:, m/a|b/.
* Zero or more matches of any thing
+ matched 1 or more times
? 0 or 1 times
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 50/122
5 May 2013
$_ = "AAABBBCCC";
$charList = "ADE";
print "matched" if m/[$charList]/;
• \w - alphanumeric character or the underscore character. It is equivalent to the character class [a-zA-Z0-9_].
• \W - every character that the \w symbol does not. In other words, it is the complement of \w. It is equivalent to [^a-zA-Z0-9_].
• \s - any space, tab, or newline character. It is equivalent to [\t \n].
• \S - any non-whitespace character. It is equivalent to [^\t \n].
• \d - any digit. It is equivalent to [0-9].
• \D - any non-digit character. It is equivalent to [^0-9].
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 51/122
5 May 2013
Quantifier Description
* The component must be present zero or more times.
+ The component must be present one or more times.
? The component must be present zero or one times.
{n} The component must be present n times.
{n,} The component must be present at least n times.
{n,m} The component must be present at least n times and no more than m times
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 52/122
5 May 2013
you may need to match an exact number of components. The following match statementwill be true only if five words are present in the $_ variable:
$_ = "AA AB AC AD AE";m/^(\w+\W+){5}$/;
(?= ) This extension lets you match values without including
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 53/122
5 May 2013
(?=...) This extension lets you match values without includingthem in the $& variable.For example, if you have some data that looks like this:
David Veterinarian 56
Jackie Orthopedist 34Karen Veterinarian 28
and you want to find all veterinarians and store the value of the first column,
For example:
while (<>)
{
push(@array, $&) if m/^\w+(?=\s+Vet)/;
}
print("@array\n");
This program will display:
David Karen
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 54/122
5 May 2013
• find repeated characters in a string like the AA in "ABC AA ABC",
• then do this:
• m/(.)\1/;
• find the first word in a string, then do this:
• m/^\s*(\w+)/
S li i
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 55/122
5 May 2013
Splitting
• Use the split operator split// to divide up text at with a given pattern as adelimiter returning the result as an array
• Syntax for the split operator:
split (/pattern/, $text);
• The pattern defines the delimiter that separates the elements of $text.
E l
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 56/122
5 May 2013
Example
Assume we have the following password file : /home/alice/password
ram_82:127465
abishek_ns:jU7345
Karishma_21:sksf87d
rishikeshRaj:43565sd
madhanram_82:127465
Call sub routine as follows: verifyPassword ram_82 127465 (or)
verifyPassword(ram_82,127465)
E l ith h
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 57/122
5 May 2013
Example with anchors
sub verifyPassword {my ($user, $password) = @_;
open(PWD, "/home/alice/password");while (my $line = <PWD>) {if ($line =~ /^$user:$password$/) {
return 1; #found user & password}
}
return 0;}
M E l
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 58/122
5 May 2013
More Examples
ab?c # matches ac or abc
(ab)+ # matches ab, abab, etc.
a(b|c)d # matches abd or acd
•The following matches a string such as "From", or "Subject"followed by a colon, an optional space, and some text
/^[^:]+: ?.+$/
Thi t b f l f
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 59/122
5 May 2013
Things to be careful of
•Regular expression operators are greedy
•They match:
the largest element they can find,
at the first position possible
my $val = "Greetings, planet Earth!";
$val =~ /G.*t/ # matches "Greetings, planet Eart"
$val = "This 'test' isn't successful";
$val =~ /'.*'/ # matches "'test' isn'"
Thi t b f l f
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 60/122
5 May 2013
Things to be careful of
• Regular expression operators are eager
• The engine is very eager to return you a match as quickly as possible
$string = "good food";
$string =~ s/o*/e/;
geod food
geed food
geed feed
Thi t b f l f
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 61/122
5 May 2013
Things to be careful of
The match is
egood food because the earliest point at which zero or more occurrences of "o"
could be found was right at the beginning of the string.
Surprised!!!
Regular expressions can do that to you.
It ti th h M t h
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 62/122
5 May 2013
Iterating through Matches
•The g modifier allows iteration through all occurrences of a pattern in astring
Example counts words you should avoid
my $buts;
while ($text =~ /but/g) {
$buts++;}
print "Your text contained $buts buts.\n";
The Solution
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 63/122
5 May 2013
The Solution
• A fix is to tell the matcher not to be greedy
• Add a "?" to the greedy operator my $var = "Greetings, planet Earth!";
$var =~ /G.*?t/; # matches Greet
• Alternatively could exclude specific characters (i.e. making it more
specific)$var = "This 'test' isn't successful"; $var =~ /'[^']*'/; #
matches 'test' only
Few Case Studies
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 64/122
5 May 2013
Few Case Studies
Case Study 1:
You would like to find the word preceding the third occurrence of "fish":
One fish two fish red fish blue fish
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 65/122
5 May 2013
Solution:
$WANT = 3;$count = 0;
while (/(\w+)\s+fish\b/gi)
{
if (++$count == $WANT){ print "The third fish is a $1 one.\n";
}
}
Case study 2
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 66/122
5 May 2013
Case study 2
Write a script that can read a configuration file and fetch into a hash map.
A sample configuration file:# set class C net
NETMASK=255.255.255.0
MTU = 296
DEVICE = cua1RATE = 115200
MODE = adaptive
Solution:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 67/122
5 May 2013
Open (CONFIG,”my_config_file.cfg”);
while (<CONFIG>){ chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/\s*=\s*/,$_ );$User_Preferences{$var} = $value;
}
close (CONFIG);
Case Study 3
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 68/122
5 May 2013
Case Study 3
You want to check whether a string represents a valid number. This is a
common problem when validating input, as in a CGI script.
Solution 1:
if ($string =~ /^\d+$/)
{ # is a number }else { # is not }
But this solution rejects -1 .
Solution 2:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 69/122
5 May 2013
Solution 2:
if ($string =~ /^-?\d+$/)
{ # is a number }
else { # is not }
But this solution rejects +1 .
Solution 3:
if ($string =~ /^[+-]?\d+$/)
{ # is a number }
else { # is not }
But this solution rejects a decimal number
Solution 4:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 70/122
5 May 2013
Solution 4:
if ($string =~ /^-?\d+\.?\d*$/)
{ # is a number }else { # is not }
But this solution rejects .2 .
Solution 5:
if ($string =~ /^[+-]?\d*\.?\d*$/ )
{ # is a number }
else { # is not }
Case Study 4
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 71/122
5 May 2013
Case Study 4
Search for two digits in same line, and switch their positions
S l ti
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 72/122
5 May 2013
Solution:
print "Tagging Parts and Switching Places\n";foreach(@strings)
{ $pattern = '(\d)(.*)(\d)';
if (/$pattern/)
{
print "Grabbed pattern: $pattern \$1 = $1 \$2 = $2 \$3 =$3\n";
s/$pattern/$3$2$1/;
}
print $_ ;
}
Case Study 5
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 73/122
5 May 2013
Case Study 5
Open all the files passed in command prompt. Identify the numbers inthe files and replace them by twice their value.
Solution:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 74/122
5 May 2013
#!/usr/local/bin/perl
$count = 0; while ($ARGV[$count] ne "") {open (FILE, "$ARGV[$count]");@file = <FILE>;$linenum = 0;
while ($file[$linenum] ne "") {$file[$linenum] =~ s/\d+/$& * 2/eg;$linenum++;}close (FILE);open (FILE, ">$ARGV[$count]");
print FILE (@file);close (FILE);$count++;}
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 75/122
Modules
Modules
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 76/122
5 May 2013
Modules
• Library of Perl code that can be included in your Perl program.
• Include the module to script to access functionality of that module.
• Standard modules are installed at time of perl installation.• Other modules need to be installed.
• All the modules are available at www.cpan.org
• Invoking module
use Module;
Accessing the Module Database on theWeb
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 77/122
5 May 2013
Accessing the Module Database on theWeb
•You can browse the module list athttp://www.cpan.org/modules/01modules.index.html.
•use http://search.cpan.org because the interface is moreneatly, if you know part of the module name already.
•If you don't know the name of a module and would like tosearch through the module description, then
http://kobesearch.cpan.org would be more useful to you,and it is actually more powerful.
•You may download the source packages and read thedocumentation online. If you intend to install the modules inthe traditional way (but manually), you may also download
the source packages there.
Modules
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 78/122
5 May 2013
Modules
• Some Widely used modules
– Strict
– DBI – CGI
– Net::FTP
– MIME::Lite
– Net::SMTP
More modules to read
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 79/122
5 May 2013
More modules to read
• Refer the perldoc for the following modules
• List::Util, Switch, File::Find, File::Basename, File::Copy, Getopt::Long, Net::FTP, POSIX,
Text::Wrap, List::MoreUtils, File::Stream, Regexp::Common, LWP::Simple, Mail::Send
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 80/122
Subroutines
SUBROUTINES
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 81/122
5 May 2013
SUBROUTINES
What Is a Subroutine?
• In Perl, a subroutine is a separate body of code designed to perform a particular
task. A Perl program executes this body of code by calling or invoking thesubroutine; the act of invoking a subroutine is called a subroutine invocation.
Subroutines serve two useful purposes:
• They break down your program into smaller parts, making it easier to read andunderstand.
• They enable you to use one piece of code to perform the same task multiple
times, eliminating needless duplication.
SUBROUTINES
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 82/122
5 May 2013
SUBROUTINES
• Perl doesn‟t distinguish between subroutines and functions.
• Even object-oriented methods are just subroutines that are called using aspecial syntax
First Subroutine
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 83/122
5 May 2013
First Subroutine
sub foo {
"Hi All";
}
Pretty useless, but a valid subroutine.
No return statement, but the return value is "Hi All"
Arguments!
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 84/122
5 May 2013
gu e ts
• You don‟t specify arguments (or even how many there are)!
• The values passed in all come packaged up in the array @_; – the subroutine gets it‟s own @_ array, this is not the same one available
elsewhere in the Perl program.
Return Value
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 85/122
5 May 2013
• A Perl subroutine can return a single scalar or a list (array) or a hash.
• There is a return keyword, but it is not compulsory to use it! – The value of the last expression evaluated in the subroutine is returned.
Calling a Perl subroutine
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 86/122
5 May 2013
g
• In the older versions of Perl (< 5), the name of a subroutine started with '&'(doesn't have to any more).
• These are all valid calls of the subroutine:
&foo; foo(1,2,3,4);
&foo("Hello"); foo 11.75;
O fi b i i l
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 87/122
5 May 2013
Out first subroutine foo is useless$x = foo(3.141593,"Pi");
• Our subroutine doesn't do anything with the values passed in. – it's not an error to pass some value anyway, even though the sub-routine
never uses it!
Global Variables
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 88/122
5 May 2013
• Any variable that is created outside of a subroutine isglobal.
•Perl subroutines can access/change global variables.
A real subroutine
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 89/122
5 May 2013
# here we define the subroutine printcelsius
sub printcelsius {
$degc = ($degf - 32) * (5/9);
print "$degf degrees fahrenheit" .
" is $degc degrees celsius\n";
}
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 90/122
5 May 2013
# perl program to calculate degrees celsius
# using a subroutine.
# ask the user for a temp in degrees fahr.
print "Enter degrees fahrenheit:\n";
# read in the value from stdin
# (and chop off the newline)
chop($degf = <STDIN>);
# call the subroutine printcelsius printcelsius();
Subroutine Location
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 91/122
5 May 2013
• Subroutine definitions can be placed anywhere in the file.
• Typically you group them all together in one place.
– This way the "main program" is also in one place". – This is nice for humans, Perl doesn't care!
Getting at parameter values
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 92/122
5 May 2013
g p
When a subroutine is called, Perl collects the parameter values and puts them into a special version of @_ that is usable only by the subroutine.
foo(1,"Hi");
inside foo,
$_[0] is 1 and $_[1] is "Hi"
Private Variables
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 93/122
5 May 2013
• „my‟ keyword can be used to declare a variable as private to thesubroutine
• „local‟ keyword can be used to declare a variable as private to thesubroutine and all the subroutines it calls my($a,$b);
my($size) = 11;
local(@foo) = (1,3..11);
Typical Subroutine
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 94/122
5 May 2013
sub min {
my($a,$b) = @_;
if ($a < $b) {
$a;
} else {
$b;}
}
Another Version of min
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 95/122
5 May 2013
sub min {
my($a,$b) = @_;
if ($a < $b) {
return $a;
} else {
return($b);
}
}
perlish versions of min
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 96/122
5 May 2013
sub min {
($_[0]<$_[1])?$_[0]:$_[1];
}
What about passing an array?
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 97/122
5 May 2013
• You can pass an entire array to a subroutine:
– foo(@somevals);
• Or a few scalars followed by an array:
– foo($a,$b,@somevals);
• Everything is put in the @_ array that the subroutine gets…
Functions that change their arguments
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 98/122
5 May 2013
@nums = (1.4, 3.5, 6.7);trunc_em(@nums); # @nums now (1,3,6)
for(@nums) { print $_; }
sub trunc_em {for (@_)
{ $_ = int($_) } # truncate each argument}
Don't pass constants to this kind of function, as in trunc_em(1.4,3.5, 6.7). If you try, you'll get a run-time exception saying Modification of a read-only value
Functions that leave their arguments intact
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 99/122
5 May 2013
# The Argument @nums remains unaffected
@nums = (1.4, 3.5, 6.7);@ints = int_all(@nums); # @nums unchanged
sub int_all {
my @retlist = @_;#make safe copy for return
for my $n (@retlist){ $n = int($n) }
return @retlist; }
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 100/122
5 May 2013
•The built-in functions chop and chomp work like this, modifying their caller's variables and returning the character(s) removed. People are
used to such functions returning the changed values, so they often writethings like:
$line = chomp(<>); # WRONG
• Given this vast potential for confusion, you might want to think twice before
modifying @_ in your subroutines.
Case Study - 1
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 101/122
5 May 2013
• You want a variable to retain its value between calls to a subroutine but not be
visible outside that routine. For instance, you'd like your function to keep track of how many times it was called.
Solution:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 102/122
5 May 2013
Wrap the function in another block, and declare my variables in that block's scoperather than the function's:
{ my $variable;
sub mysub { # ... accessing $variable } }
If the variables require initialization, make that block a BEGIN so the variable is
guaranteed to be set before the main program starts running:
BEGIN {
my $variable = 1; # initial value
sub othersub { # ... accessing $variable } }
Sharing of a static variable
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 103/122
5 May 2013
• Generally, you should use a BEGIN for the extra scope. Otherwise, you could call thefunction before its variable were initialized.
BEGIN { my $counter = 42;
sub next_counter { return ++$counter }
sub prev_counter { return --$counter } }
This technique creates the Perl equivalent of C's static variables. Rather than being
limited to just one function, both functions share their private variable.
Case Study 2
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 104/122
5 May 2013
• You want to determine the name of the currently running function. This is useful
for creating error messages that don't need to be changed if you copy and pastethe subroutine code.
Solution:
$this_function = (caller(0))[3];
Discussion:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 105/122
5 May 2013
Discussion:
• Code can always find the current line number in the special symbol __LINE__ , thecurrent file in __FILE__ , and the current package in __PACKAGE__ . But there's nosuch symbol for the current subroutine name
• The built-in function caller handles all of these. You can also pass it a number indicatinghow many frames (nested subroutine calls) back you'd like information about: 0 is your own function, 1 is your caller, and so on.
($package, $filename, $line, $subr, $has_args, $wantarray )=caller($i); # 0 1 2 3 4 5
Case Study 3
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 106/122
5 May 2013
• You want to pass a function more than one array or hash and have each remain distinct.Try to add the corresponding elements in two different arrays.
Solution:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 107/122
5 May 2013
Solution:@a = (1, 2);
@b = (5, 8);
@c = add_vecpair( \@a, \@b ); print "@c\n";
sub add_vecpair {
# assumes both vectors the same length
my ($x, $y) = @_; # copy in the array references
my @result;for (my $i=0; $i < @$x; $i++)
{ $result[$i] = $x->[$i] + $y->[$i]; }
return @result; }
6 10
Discussion:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 108/122
5 May 2013
Discussion:
• A potential difficulty with this function is that it doesn't check to make sure it got exactlytwo arguments that were both array references.
You could check explicitly this way
unless (@_ == 2 && ref($x) eq 'ARRAY' && ref($y) eq 'ARRAY')
{ die "usage: add_vecpair ARRAYREF1 ARRAYREF2"; }
Case Study 4
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 109/122
5 May 2013
• You want to make a function with many parameters easy to invoke so that programmersremember what the arguments do, rather than having to memorize their order.
Solution:
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 110/122
5 May 2013
Solution:
thefunc(INCREMENT => "20s", START => "+5m", FINISH => "+30m");
thefunc(START => "+5m", FINISH => "+30m");thefunc(FINISH => "+30m");
thefunc(START => "+5m", INCREMENT => "15s");
sub thefunc {
my %args = ( INCREMENT => '10s', FINISH => 0,START => 0, @_, # argument pair list goes here );
# Your code follows }
Skipping Selected Return Values
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 111/122
5 May 2013
• You have a function that returns many values, but you only care about some of them.
($a, undef, $c) = func();
($dev,$ino,undef,undef,$uid) = stat($filename);
Returning More Than One Array or Hash
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 112/122
5 May 2013
• Just as all arguments collapse into one flat list of scalars, return values do, too
($array_ref, $hash_ref) = somefunc();
sub somefunc {
my @array; my %hash; # ...
return ( \@array, \%hash ); }
Handling Exceptions
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 113/122
5 May 2013
• Sometimes you encounter a problem so exceptional that merely returning an error isn'tstrong enough, because the caller could ignore the error. Use die STRING from your function to trigger an exception:
die "some message"; # raise exception
• The caller can wrap the function call in an eval to intercept that exception, andthen consult the special variable $@ to see what happened:eval { func() };
if ($@) { warn "func raised an exception: $@"; }
Overriding global variables
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 114/122
5 May 2013
my @nums = (0 .. 5);
sub first {
local $nums[3] = 3.14159;
second(); }
sub second { print "@nums\n"; }
first(); # 0 1 2 3.14159 4 5second(); # 0 1 2 3 4 5
Redefining a Function
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 115/122
5 May 2013
•You want to temporarily or permanently redefine a function
{ local *grow = \&shrink;
# only until this block exists
grow(); # calls shrink()
}
Anonymous Subroutines
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 116/122
5 May 2013
sub outer {
my $x = $_[0] + 35;local *inner = sub { return $x * 19 };
return $x + inner();
}
This essentially creates a function local to another function
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 117/122
System InteractionCommand execution
system() and exec()
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 118/122
5 May 2013
• The system() and exec() functions both execute system commands.
• system() forks, executes the commands given in its arguments, waits for them to return,then allows your Perl script to continue. exec() does not fork, and exits when it‟s done.
system() is by far the more commonly used.
% perl -we ‟system("/bin/true"); print "Foo\n";‟
Foo
% perl -we ‟exec("/bin/true"); print "Foo\n";‟
• Statement unlikely to be reached at -e line 1.(Maybe you meant system() when you saidexec()?)
• If the command specified by system() could not be run the error message will be availablevia the special variable $!. This value is not set if the command can be run but fails duringruntime.
• The return status of the command can be found in the special variable $?, which is alsothe return value of system(). This value is a 16-bit status word which needs to beunpacked to be useful, as demonstrated in the example below.
System() - eg
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 119/122
5 May 2013
system("/bin/true");
if ($?) { # A non-zero exit code means failure
# -1 means the program didn‟t even start!
if($? == -1) {
print "Failed to run program: $!\n";
} else {
print "The exit value was: " . ($? >> 8) . "\n";
print "The signal number that terminated the program was: "
. ($? & 127) . "\n";print "The program dumped core.\n" if $? & 128;
}
}
Using Backticks
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 120/122
5 May 2013
• Backticks ` ` are more flexible as it simulates the command line.
• What you need to do is to construct the command and enclose it in backticks
instead of single or double quotes.• In US keyboards, the backtick is located underneath the Escape key, sharingthe key with the tilde (~). An example is
$output = `ls –l`; # get long directory listing
• Text that are sent to the standard output are collected and assigned to $output.
Security concern over the use
7/30/2019 Perl – Training QA
http://slidepdf.com/reader/full/perl-training-qa 121/122
5 May 2013
• Traditional form of calling system and exec have security issues due to shell expansion.For example consider the following code:
my $filename;
print "Please give me a file you want to see: ";
$filename = <>; # lets pretend: $filename="fred; rm -rf /home/pjf;"
chomp($filename);
system("cat $filename");
# or
exec("cat $filename");• In this case, due to shell expansion, the shell will receive the commands:
cat fred
rm -rf /home/pjf
• and if our program had sufficient permissions to delete pjf‟s home directory, it would
References