Perl Training Cvs
Perl Training Cvs
Chaitanya CVS
Getting Started
Perl is an acronym, short for Practical Extraction and Report Language. Parse tool reports OR build your own tools
First code
A Sample Perl Program Create a file called program1_1.pl #!/usr/intel/bin/perl $inputline = <STDIN>; print( $inputline ); Running a Perl Program
To run the program shown, do the following:
Make the file executable. chmod +x program1_1
Exercise 1:
Program1_1:
Basic Operators
Storing in Scalar Variables Assignment
The Definition of a Scalar Variable Scalar Variable Syntax Assigning a Value to a Scalar Variable
Performing Arithmetic
Example of Miles-to-Kilometers Conversion
Expressions
Assignments and Expressions
Exercise 2.1:
#!/usr/intel/bin/perl print ("Enter the distance to be converted:\n"); $originaldist = <STDIN>; chomp ($originaldist); $miles = $originaldist * 0.6214; $kilometers = $originaldist * 1.609; print ($originaldist, " kilometers = ", $miles, " miles\n"); print ($originaldist, " miles = ", $kilometers, " kilometers\n"); Does it work?
Exercises 2.2:
Use exercise 2.1 as a model and build a Rupee -> US Dollar converter. Use 40.50 rupees as the conversion factor for 1 dollar Write a program that reads two integers from standard input (one at a time), divides the first one by the second one, and prints out the quotient (the result) and the remainder.
Does it work?
More operators
Operator < > <= >= == != <=> Description Less than Greater Less than or equal to Greater than or equal to Equal Not equal to Comparison returning 1, 0, or -1
Write a Perl program that reads in a number, multiplies it by 2, and prints the result. Write a Perl program that reads in two numbers and does the following: It prints Error: can't divide by zero if the second number is 0. If the first number is 0 or the second number is 1, it just prints the first number (because no division is necessary). In all other cases, it divides the first number by the second number and prints the result.
Arrays
Variables: @Arrays
An array holds a list of scalar elements Arrays are preceded by the @ sign. Creating An Array $three = 3; @array1 = (1, 2, $three); # Set equal to 2 integers & 1 scalar. @array2 = @array1; # Copy @array1 contents to @array2. Printing an array print @array1; => 1 2 3 Accessing Individual Array Elements $first_element = $array[0]; $second_element = $array[1]; Using a -1 subscript is a fast way to get the value of the last element in an array. Length of array $length = @array;
Variables: @Arrays
Adding & Removing Elements From An Array, Part II The push function accepts a list of values to be inserted at the end of an array. The pop function removes a value from the end of an array. The pop function returns undef if given an empty array. Examples @list = (1,2,3); print @list; push(@list,4,5,6); # Add 4, 5, & 6 to end of the array. print @list; => 123456 $lastvalue = pop(@list); # Remove the last element & store in $lastvalue. print $lastvalue; => 6 print @list; => 12345
Variables: @Arrays
Adding & Removing Elements From An Array, Part III The shift & unshift functions do the same things to the left side of the array as push & pop do to the right side of the array. unshift inserts elements to the beginning of an array. shift deletes elements from the beginning of an array. Examples @list = (1,2,3); print @list; => 123 unshift(@list,4,5,6); # Add 4, 5, & 6 to the beginning of the array. print @list; => 456123 $firstvalue = shift(@list); # Remove the first element & store in $firstvalue. print $firstvalue; => 4 print @list; => 56123 Note: The push and pop functions are more efficient with large arrays.
Variables: @Arrays
Just to refreash
pop(@array) remove element from right hand side push(@array, $vars) add element[s] to right hand side shift(@array) remove element from left hand side unshift(@array, $vars) add element[s] to left hand side
Variables: @Arrays
Merging Two Arrays
@array(1, 2, 3); @array2 = (@array, @array); print @array2;
=> 123123
Reversing An Array
Use the reverse function to reverse the order of an array. @array = (1, 2, 3); @array2 = reverse(@array); print @array2;
=> 321
Variables: @Arrays
Sorting An Array The sort function sorts in ascending ASCII order. A chart listing the full ASCII character order is located in the backups section of this presentation. Examples @sizes = (small,medium,large); @array = sort(@sizes); # Sort array of strings. print @array; => large medium small @array2 = (1,2,4,8,16,32,64); @array3 = sort(@array2); print @array3;
Variables: @Arrays
Splitting A String Into An Array Use the split function to make an array out of a scalar string. $line = "moe::11:10:Mike:/home:/usr"; @fields = split(/:/, $line); # Split at colons, :. Store in @fields. @fields now contains: ("moe","","11","10",Mike", /home,/usr) $line = here; @chars = split(//, $line); # Split each character of $line. @chars now contains: (h",e",r",e") Joining An Array Into A String Use the join function to make a scalar string out of an array. $outline = join(":", @fields); # Join each element of an array with :. print $outline; => moe::11:10:Mike:/home:/usr
Exercise 4
1.
2.
Given the following array: @nums = (3, 2, 123, 34, 10, 1, 21, 134, 45, 0); Write a script to find the largest value of the array. Assign the line This perl training makes me sweat to a variable. Split the line based on spaces and count the number of words in the lines.
Hashes
Variables: %Hashes
Remember everything in Perl is a list Introduction To Hashes
A hash is an unordered collection of key-value pairs. A hash is similar to an array; however, hash elements are accessed with a string know as a key - not with a subscript value. Each key name must be unique. Accessing a non-existent hash key returns scalar zero. Hash variables are preceded by the percent symbol, %. Hashes require more memory than an array; however, they are useful when fast retrieval of values is required & are easier to use when handling chucks of key/value pairs.
Creating A Hash,
%hash = (key => value ); # Using the % sign $hash2{key} = value; # Using a scalar assignment.
Hash Functions
The exists function checks if a hash element exists.
%layerNums = (ndiff => 1, poly => 2, contact => 3); if (exists($layerNums{poly})) { print Element exists.; => Element exists. } else { print Element doesnt exist.; }
The each function returns one key-value pair of a hash as a list in which the first element is the key and the second element is the value.
The each function is normally used in a loop to cycle through all the key-value pairs of a hash. %layerNums = (ndiff => 1, poly => 2, contact => 3); ($key, $value) = each (%layerNums); # grab one keyvalue p air print "key: $key, value: $value\n; => key:ndiff, value: 1
Example Script
#!/usr/intel/bin/perl # This script loops through the key-value pairs of a hash & prints them out. %layerNums = (ndiff => 1, poly => 2, contact => 3, metal1 => 4); while(($key, $value) = each(%layerNums)) { print Layer: $key,\tLayer Number: $value\n; } Layer: metal1, Layer Number: 4 Layer: contact, Layer Number: 3 Layer: poly, Layer Number: 2 Layer: ndiff, Layer Number: 1
Coding again
#!/usr/intel/bin/perl # More hash looping examples. %layerNums = (ndiff => 1, poly => 2, contact => 3); foreach (keys %layerNums) { print "\%layerNums contains $_ layer info.\n"; } foreach (values %layerNums) { print "\%layerNums contains the layer number: $_.\n"; } Can you sort the keys and print?
Sorting Hashes
Sorting Hashes The following examples illustrate an ascii sort on a hashs keys & values. $hash{fruit} = apple; $hash{sandwich} = hamburger; $hash{drink} = coke; foreach $key (sort keys %hash) { print $key => $hash{$key}\n; } drink => coke fruit => apple sandwich => hamburger
Exercises
1. Write a script that given the following hash, prints out the number of existing key-value pairs: %greekAlphabet = (alpha => a, beta => b, gamma => g, delta => d); 2. Define the following array. @cellNames = (n4t, p4t, ntg4t, p4t, bufferA, bufferA, bandgap,ntg4t, vdnmos, driver, predriver, ntg4t); Write code, using a hash, that determines if there are any duplicate values in the array. Output the cell name of those cells that are duplicated.
File Processing
Agenda
File Processing Command Line Arguments File Input/Output The Diamond Operator File Test Operators Exercises
Opening A File
The open function allows a file to be opened for read, write and/or append access. open (FILEHANDLE, <file.txt) or die Could not open file: $!;
< > >> +< +> # open for read access # create for write access # open for append # open for read & write # create for read & write
Closing a file:
close(FILEHANDLE) or die Cannot close file: $!;
When the contents of a file are exhausted, <FILEHANDLE> will return false.
Writing To A File
To open a file for editing, use the open function and specify opening the file in write or append mode by using > or >>. To print data to a file, use the print function with two arguments: The first argument is the FILEHANDLE. The second argument is the string to be written to the file. Example #!/usr/intel/bin/perl open (FH, >file.txt) or die Could not open file : $!;# open for edit @array = (1, 2, 3); print FH Hello; # print Hello to file print FH @array; # print contents of @array to file close (FH) or die Cannot close file: $!; # close file
Example Script
#! /usr/intel/bin/perl # This program prints the size of a file in bytes. print Enter the name of the file:\n; chomp ($filename = <STDIN>); if (!(-e $filename)) { } else { $size = -s $filename; } print File $filename does not exist.\n; # file exists # obtain file size print File $filename contains $size bytes.\n; # grab file name # check if file exists
Exercises
1.
Write a script that takes numeric values from the command line, adds them together, and prints the result. Write a script that reads in a list of filenames from the command line and then displays which of the files are readable, writeable, executable and which ones dont exist. Write a script that copies the contents of one file into another new file (basically performing a copy operation). Make sure that the new file does not already exist.
2.
3.
Agenda
The System Command Backticks Pipes
Backticks
As seen in the String Manipulation slides, backticks can also be used to execute an external command. The difference between backticks and the system command is that backticks will return the output of the forked process. Examples: @result = `dir altr`; # perform dir & store output in @result $wordcount = `wc $file`;# perform word count command & store result A shorthand way to use backticks is with the qx() operator. @result = qx(dir altr);
Piping
Another way to invoke an external command is to use a filehandle. Writing to the filehandle will write to the standard input of the command. Reading from the filehandle will read the commands output. This is the same concept as opening a filehandle for a file, except that: An external command name is used in place of the file name. A pipe, |, is appended to the command name. Example Usage open(FILEHANDLE, command |); # read output of an external command open(FILEHANDLE, | command); Env Example open(ENV_FH, env |) or die Could not open pipe: $!\n; print $_ while(<ENV_FH>); # print env variables close(ENV_FH) or die Could not close pipe: $!\n; # write to an external command
Piping
Date Example open(DATE_FH, date |) or die Could not open pipe: $!\n; $date = <DATE_FH>; # grab date close(DATE_FH) or die Could not close pipe: $!\n; Sendmail (email) Example (works on HP & Linux) open(MAIL, | mail [email protected]) or die Could not open $!; print MAIL Subject: This is the subject line\n\n; print MAIL This is the body.\n; close(MAIL) or die Could not close pipe: $!\n;
Piping
Using the tee command to open multiple files at once which correspond to a single filehandle: open(FH, "|tee temp1 temp2 temp3") or die FH: $!\n"; print FH "hello\n"; close(FH);
Pattern Matching
Agenda
Regular Expressions Intro RE Operators Pattern Matching RE Modifiers Escape Characters Quantifiers & Assertions Grouping Character Classes Backreferencing Pattern Substitution Quantifier Greediness Pattern Translation Exercises
Coding time
#!/usr/intel/bin/perl
print Ask me a question politely: \n; $question = <STDIN>; if ($question =~ /please/i) { } else { print That was not polite.\n; } # search for please print Thank you for being polite.\n;
Escape Characters
Perl provides several special escape characters for use in regular expressions. \d match a digit character (0-9) \D match a non-digit character \s match a white-space character \S match a nonwhite-space character \w match a word character (a-z & A-Z) and _ \W match a non-word character All of Perls default escape characters (\a, \e, \E, \f, \l, \L, \n, \r, \t, \u, \U, etc) behave exactly the same as in doublequoted string interpolation.
Escape Characters
Regular Expression Escape Character Examples $str = There it is.; if ($str =~ /\w\w\w/) { # matches T, h, e print There are three word characters; } if ($str =~ /(\S\)s(\S)/) { print nonwhitespace_space_nonwhitespace; print $1 - $2\n; } if ($str !~ /\d/) { print There are no digits.; } if ($str =~ /\D/) { print There are no digits.; }
Pattern Matching
Match The Beginning Of A Line
$line = .Hello there!; if ($line =~ m/^\./) { print Dont start with a period; } Match The End Of A Line $line = My name is Jack.; if($line =~ m/Jack\.$/) { print End of line is Jack.; }
Pattern Matching
More Regular Expression Examples
/abc*/ /b{3}/ /\w{5}/ /de{1,2}f/ /de{3,}/ /\w{2,8}/ # ab, abc, abcc, abccc, abcccc # three b's # 5 word characters in a row # def or deef # d followed by at least 3 es # match 2 to 8 word characters
Regular Expressions - Word Boundary Examples /fred\b/ # fred, but not frederick /\bmo/ /\bFred\b/ /\Bfred/ /def\B/ /\Bfred\B/ # moe and mole, but not Elmo # Fred but not Frederick or alFred # matches abcfred, but not fred # matches defghi # cfredd but not fred, fredrick, or alfred
Grouping
Grouping Parentheses are used for grouping. Parentheses also have the side effect of remembering what they matched. We will look into this ability later. Use a pipe (vertical bar) to separate two or more alternatives. Example $text = Im busy today!; if ($text =~ /bus(y|ied|ier|isest)?/) { # match: busy, busied, busier, or busiest print Matches; } More Regular Expression Grouping Examples /(abc)+/ # match: abc, abcabc, abcabcabc /^(x|y)/ # match either x or y at beginning of a line /(a|b)(c|d)/ # match: ac, ad, bc, or bd
Grouping
Grouping can also be used without parentheses when you just trying to match a series of words. /now|then/ # matches now or then /^There|^Their/ # matches There or Their at the beginning of a line Grouping example using array elements as the search patterns: @patterns = qw(exit quit goodbye); $pattern = join(|", @patterns); # create string: exit|quit|goodbye while($line = <>) { if ($line =~ /$pattern/) { # look for exit, quit or goodbye print You entered: exit, quit or goodbye\n; exit; } else { print You entered: $_\n; } }
Character Classes
Character Classes A pattern-matching character class is represented by a pair of open and close square brackets and a list of characters between the brackets. Only one of the characters must be present at the corresponding part of the string for the pattern to match. Specify a range of characters with the - character. Backslashes apply inside character classes. Examples $text = Here he is.; if ($text =~ /[aeiou]/) { print There are vowels.\n; } $text2 = -3.1415; if($text2 =~ /^[+-][0-9]+\.\d*$/) { print Its correct.; }
Character Classes
Negated Character Classes There's also a negated character class that matches any character not in the list. Create a negated character class by placing a caret ^ immediately after the left bracket. Example $text = Hello; if ($text =~ /[^aeiou]/i) { # match any single non-vowel print There are consonants; }
Coding time
#!/usr/intel/bin/perl # This script validates variable names. print Enter a variable name: ; chomp($var = <STDIN>); if ($var =~ /^\$[A-Za-z][_0-9A-Za-z]*$/) { print $var is a legal scalar variable.\n; } elsif ($var =~ /^@[A -Za-z][_0-9A-Za-z]*$/) { print $var is a legal array variable.\n; } elsif ($var =~ /^%[A -Za-z][_0-9A-Za-z]*$/) { print $var is a legal hash variable.\n; } else { print $var is not a legal variable.\n; }
Backreferencing
Backreferencing Examples (Outside Regular Expressions) $text = David is 20 years old.; if ($text =~ /(\d+)/) { print He is $1 years old.\n; => He is 20 years old. } $text = I am busy today!; $text =~ /^(\w+)\s+(\w+)\s(\w+)/; print $1 $2 $3!;
=> I am busy!
Pattern Substitution
In addition to matching patters, regular expressions can be used to substitute matched patterns. Basic Substitution Example $var =~ s/error/warning/g; In this example, Perl substitutes any occurrences of the error string inside of the $var variable with the warning string. The s// operator is the substitution operator. It tells Perl to search for the pattern specified in its first two delimiters and replace that pattern with the pattern specified in its second two delimiters. The value inside the $string variable gets modified if a match is found. The substitution operators s prefix is required.
Pattern Substitution
All the previous operators, modifiers, assertions and so on, work with pattern substitution as well. Basic Examples $text = Im pretty young.; $text =~ s/young/old/; print $text; => Im pretty old. $text = Im young and youre young.; $text =~ s/young/old/; print $text; => Im old an youre young.
Pattern Substitution
More Basic Examples $text = Here he is.; $text =~ s/\w+/There/; print $text;
Note: Use + to match the whole word. $text = Today is Tuesday.; $text =~ s/./*/g; print $text; => ***************** $text = How are youuuuuuu?; $text =~ s/u+/u/g; print $text; => How are you?
Pattern Substitution
More Basic Examples $string = "hello, world"; $new = "goodbye"; $string =~ s/hello/$new/; print $string;
$text = Perl is on page 493.; $text =~ s/[^A-Za-z\s]+/500/; print $text; => Perl is on page 500.
Pattern Substitution
Obtaining The Number Of Substitutions Made $text = How are youuuu?; $subs = ($text =~ s/u/z/g); print $subs; => 4
Backreferencing With Pattern Substitution $text = snakes eat rabbits; $text =~ s/^(\w+) *(\w+) *(\w+)/$3 $2 $1/; print $text; => rabbits eat snakes
Pattern Substitution
Remove Leading & Trailing White Space
$text = Im leaving.; $text =~ s/^\s+//; print $text; leaving. $text = Im leaving. ; $text =~ s/\s+$//; print $text; leaving.
=> Im
=> Im
Example Script
#!/usr/intel/bin/perl # This script cleans up white space. It removes all extraneous leading spaces and tabs, # and replaces multiple spaces and tabs between words with a single space. while(<>) { last if(/^\s*$/); s/^\s+//; s/\s+$//; s/\s+/ /; push (@var, $_); # exit loop if nothing entered on input line # leading white space # trailing white space # mid white space # add modified line to @var array
Pattern Translation
Basic Examples of translation $text = Hello Tammy.; $text =~ tr/a/o/; # translate as into os print $text; => Hello Tommy. $text =~ tr/Tm/Dn/; print $text; => Hello Donny. $text =~ tr/a-z/A-Z/; # upper case everything print $text; => HELLO DONNY. $O_count = ($text =~ tr/O/O/); # Count the number of Os in $text. print $text; => HELLO DONNY print $O_count; => 2 Notice the usage of = vs =~.
Pattern Translation
Basic Examples Using The Options (d) Delete found but unreplaced characters (d) $text2 = zed sandy tabey; $text2 =~ tr/a-z/A-N/d; print $text2; => ED AND ABE Note: Spaces were unaffected because they didnt appear in the old list search pattern. $letters =~ tr/AEIOU/ae/d; Replace every A and E to a and e respectively, but deletes every I, O, and U found in $letters.
Exercises
What do the following REs match? a) /\\*\**/ e) /abc{1,4}/ b) /\bc[aou]t\b/ f) /ab*c/ c) /a|bc*/ g) /^hello|adios$/ d) /(xy+z)\.\1/ h) /^(hello|adios)$/
Exercises
If $var = abc123, indicate whether the following conditional expressions return true or false: a) $var =~ /./; b) $var =~ /\w{3,6}/; c) $var =~ /abc$/; If $var = abc123abc, what is the value of $var after the following substitutions: a) $var =~ s/abc/def/; b) $var =~ s/B/W/i;
Subroutines
Agenda
Subroutine Intro Passing Arguments Default Values Return Values Variable Scope Exercises
Subroutine Intro
A subroutine is a block of code separate from your main program, designed to perform a particular task. A subroutine is basically a user-defined function. Once written, they can be used just like the built-in Perl functions such as join or chomp. The idea behind subroutines is divide and conquer. Subroutines allow you to divide your code into manageable parts, making the overall programming easier to write and maintain. Other benefits to writing subroutines are: They help to avoid writing repetitive code in a program They make debugging easier They can be reused in other programs
Subroutine Intro
#!/usr/intel/bin/perl # Obtain inputted numbers & print the total value. $total = 0; getnumbers(); # call subroutine that grabs input foreach $number (@numbers) { $total += $number; } print The total is $total\n; # Subroutine to grab inputted numbers. sub getnumbers { $line = <STDIN>; # get input $line =~ s/^\s+|\s*\n$//g; # removing leading & trailing white space @numbers = split(/\s+/, $line);# break input into numbers & place in array }
Passing Arguments
#!/usr/intel/bin/perl # Example of passing multiple arguments to a subroutine. $title = Hannibal; $author = Thomas Harris; fav_book($title, $author); # call subroutine & pass arguments # Subroutine to print favorite book. sub fav_book { print My favorite book is $_[0] by $_[1]\n; } => My favorite book is Hannibal by Thomas Harris
Passing Arguments
#!/usr/intel/bin/perl # Example of passing an array. @bookInfo = ("Hannibal", "Thomas Harris"); fav_book(@bookInfo); # call subroutine & pass arguments # Subroutine to print favorite book. sub fav_book { print "My favorite book is $_[0] by $_[1]\n"; } => My favorite book is Hannibal by Thomas Harris
Passing Arguments
#!/usr/intel/bin/perl # Another example. Passing an array & an integer. @array = (3, 3); add_all(@array, 3); # call subroutine & pass @array # Function to sum passed values. sub add_all { $sum = 0; foreach $element (@_) { $sum += $element; } print The total is $sum\n; }
Passing Arguments
Note when used in a subroutine, the shift function defaults to working on the @_ array. #!/usr/intel/bin/perl # Example of passing an array. @bookInfo = ("Hannibal", "Thomas Harris"); fav_book(@bookInfo); # call subroutine & pass arguments # Subroutine to print favorite book. sub fav_book { $book = shift; # grab first argument & place in $book $author = shift; # grab second argument & place in $author print "My favorite book is $book by $author\n"; } => My favorite book is Hannibal by Thomas Harris
Return Values
When the subroutine completes, data can be returned to the main program with the return keyword. Subroutines can return scalar or list values. Perl will exit from a subroutine immediately after the return statement executes. Any code in the subroutine that appears after the return statement does not get executed. The value of the last expression evaluated by the subroutine is automatically considered the return value if the return keyword is not used. To ensure the correct return value from a subroutine, use the return keyword.
Return Values
#!/usr/intel/bin/perl $name = Rocky; $years_old = 3; $years = dog_years($years_old);
# call subroutine, return value # placed in $years variable print My dog $name is $years dog years old.; # subroutine to compute dog years sub dog_years { $yrs_old = shift; $age = 7 * $yrs_old; return $age; }
Return Values
#!/usr/intel/bin/perl # Example of returning a list/array from a subroutine. @values = dumb_sub(a, b); print I passed @values.; sub dumb_sub { return (@_); }
=> I passed a b .
Return Values
#!/usr/intel/bin/perl # Second example of returning a list/array from a subroutine. ($first, $last) = list_sub(); print President $first $last\n; sub list_sub { return (George, Bush); }
Return Values
#!/usr/intel/bin/perl # Example of returning a hash from a subroutine. %ssn = name_ssn(); foreach $key (keys %ssn) { print $key -> $ssn{$key}\n"; } sub name_ssn { return ( pairs John Smith => 441-36-0889, Jane Doe => 104-21-2094, Dave Bishop => 394-84-2940, ); }
Return Values
#!/usr/intel/bin/perl # Example of using multiple return statements in a subroutine. $result = arithmetic(plus, 2, 2); print Result is: $result\n; => Result is: 4 sub arithmetic { $operation = shift; $arg1 = shift; $arg2 = shift; if($operation eq plus) { return $arg1 + $arg2; # return sum of values } elsif($operation eq minus) { return $arg1 - $arg2; # return remainder of values } else { return $arg1 * $arg2; # return product of values } }