Sunteți pe pagina 1din 98

Perl Training

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

First code debug


Now create another file program1_2 and copy the following: #!/usr/intel/bin/perl
$inputline = Hello world\n print $inputline

Run the script. What do you see?


Debug

Exercise 1:
Program1_1:

#!/usr/intel/bin/perl $inputline = <STDIN>; print( $inputline );


Modify program1_1 to read two input lines and print only the second one. Modify program1_1 to read and print two different input lines. Modify program1_1 to print the input line twice.

Basic Operators and Control Flow

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

Other Perl Operators

Definition of Scalar Variables


Legal scalar variable names: $x $var $my_variable $var2 $a_new_variable Illegal scalar variable names: variable # the $ character is missing $ # there must be at least one letter in the name $47x # second character must be a letter $_var # again, the second character must be a letter $variable! # you can't have a ! in a variable name Perl variables are case-sensitive. This means that the following variables are different: $VAR, $var, $Var

Assigning Basic operators


$name = "John Q Hacker"; $var = 42; Some operations: $var = 17 + 5 - 3; You can use the value of a variable in an arithmetic operation, as follows: $var1 = 11; $var2 = $var1 * 6; Now examine the following statements: $var = 11; $var = $var * 6; Question: What will $var store?

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.

Control flow - If loop


Program containing a simple example of an if statement. #!/usr/intel/bin/perl print ("Enter a number:\n"); $number = <STDIN>; chomp ($number); if ($number == 0) { print ("The number is zero.\n"); } print ("This is the last line of the program.\n");

Does it work?

Control flow - IF loop with comparison


#!/usr/intel/bin/perl print ("Enter a number:\n"); $number1 = <STDIN>; chomp ($number1); print ("Enter another number:\n"); $number2 = <STDIN>; chomp ($number2); if ($number1 == $number2) { print ("The two numbers are equal.\n"); } print ("This is the last line of the program.\n");

If Elsif - Else loop with comparison


#!/usr/intel/bin/perl print ("Enter a number:\n"); $number1 = <STDIN>; chomp ($number1); print ("Enter another number:\n"); $number2 = <STDIN>; chomp ($number2); if ($number1 == $number2) { print ("The two numbers are equal.\n"); } elsif ($number1 == $number2 + 1) { print ("The first number is greater by one.\n"); } else { print ("The numbers are not equal.\n"); } print ("This is the last line of the program.\n");

Control flow - While loop


Create the following code: #!/usr/intel/bin/perl $done = 0; $count = 1; print ("This line is printed before the loop starts.\n"); while ($done == 0) { print ("The value of count is ", $count, "\n"); if ($count == 3) { $done = 1; } $count = $count + 1; } print ("End of loop.\n"); Note: You can nest if inside while and while inside if (multiple if and while loops are allowed)

Control flow - Until loop


A program that uses the until statement. #!/usr/intel/bin/perl print ("What is 17 plus 26?\n"); $correct_answer = 43; $input_answer = <STDIN>; chomp ($input_answer); until ($input_answer == $correct_answer) { print ("Wrong! Keep trying!\n"); $input_answer = <STDIN>; chomp ($input_answer); } print ("You've got it!\n");

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

Exercises 3 (10 mins)


1. 2.

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;

Time to try out arrays


Code the following problem and execute: #!/usr/local/bin/perl @array = (1, "chicken", 1.23, "\"Having fun?\"", 9.33e+23); $count = 1; while ($count <= 5) { print ("element $count is $array[$count-1]\n"); $count++; } $length = @array; print (The length of array is $length\n);

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;

# Sort array of integers. => 1 16 2 32 4 64 8

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

Control flow for arrays


The foreach Control Structure The foreach structure is used to iterate over a list of values, one by one. Unlike the for structure, there is no separate counter control variable to manage the repetition structure. Example @array = qw(one, two, three); foreach $element (@array) { print $element ; } => one two three In this example, $element represents one item of the list during each iteration of the loop. So during the first iteration, it is equal to one, the second iteration two and the final iteration three.

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.

Time to code in a hashes


#!/usr/intel/bin/perl # Create a hash mapping layout layer names to their layer numbers. %layerNums = (ndiff => 1, poly => 2, contact => 3, metal1 => 4); print %layerNums, \n; # => metal14contact3poly2ndiff1 $layerNums{metal2} = 14; # Add a metal2 key-value pair to the hash. print %layerNums, \n; # => metal14contact3poly2metal214ndiff1 delete($layerNums{poly}); # Remove the poly key-value pair. print %layerNums, \n; # => metal14contact3metal214ndiff1 Are you able to print?

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

Looping Over Hashes


The following two examples illustrate how to loop over a hash with a foreach loop: foreach $key (keys %hash) { print $key -> $hash{$key}\n"; } foreach $value (values %hash) { print $value\n"; # print the values }

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

foreach $value (sort values %hash) {


print $value; } prints apple coke hamburger

The %ENV Variable


The %ENV variable is a special built-in hash that holds the current environmental variables. Environmental variables hold information about a shells environment. Examples print $ENV{HOST}; foreach (keys %ENV) { variables print $_ => $ENV{$_}\n; } Changing the value of an environmental variable from within a Perl script will change that variables value for the duration of the script. # prints host name # print all environmental

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: $!;

Reading From A File


To read a line from a file, use the specified FILEHANDLE surrounded by the diamond operator, <>. Example $line = <FILEHANDLE>; FILEHANDLE # Read one line from

When the contents of a file are exhausted, <FILEHANDLE> will return false.

Code: Reading From A File


#!/usr/intel/bin/perl # Open .alias file & print out contents. open (ALIAS, .alias) or die Could not open file : $!;# open .alias file in read mode while(<ALIAS>) { time print; } close(ALIAS) or die Could not close file: $!;#close .alias file # grab one line at a # print line

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

The @ARGV Variable


The @ARGV variable stores all command-line arguments. The first command-line argument is stored in the $ARGV[0] variable, the second in $ARGV[1], etc The name of the script is stored in the $0 variable. To loop over all command line arguments: foreach $arg (@ARGV) { print $arg\n; args } # print command line

The @ARGV Variable


#!/usr/intel/bin/perl # loop through command line arguments & print lines foreach $arg (@ARGV) { open(FH, $arg) or die Could not open $arg: $!\n; while(<FH>) { print $arg: $_\n; } close(FH) or die Could not close $arg: $!\n; }

File Test Operators


Perl provides several file test operators that allow a script to get information about a file. There are file test operators to check if files are readable, writable, executable, if the exist, etc For example, the -e operator tests whether or not a file exists. if(-e $filename && -w $filename) { print $filename exists\n; } else { print $filename does not exist.\n; }

File Test Operators


Operator -A -B -d -e -f -l -M -r -T -w -x -z Check time since file last accessed in days file a binary file file is a directory file exists file is a plain file file is a symbolic link time since file last modified file readable file a text file file writable file executable file has zero size

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.

Executing shell commands

Agenda
The System Command Backticks Pipes

The system Command


The system command is similar to exec in that it can be used to execute shell commands; however, the system command returns back to the original process. It does this by using a fork to create a separate child process which executes the shell command. The original process (i.e. the perl script) waits until that forked child process finishes before continuing execution. Examples system(clear); system(ls > dir.txt); system(mkdir scripts); # perform Unixs clear command # perform ls command & direct output to a file # make a scripts directory in cwd

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 xx@yy.com) 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

Regular Expressions Intro


A regular expression (RE) is a pattern of characters used to look for the same pattern of characters in another string. Regular expressions are powerful tools when searching through text files and databases.

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.; }

Quantifiers & Assertions


Perl provides several special symbols called quantifiers that tell Perl to match multiple occurrences of a pattern at a time. . match any character except newline ? match zero or one times * match zero or more times + match one or more times {n} match n times {n,} match at least n times {n,m} match between n & m times (inclusive) Perl also provides several special assertion characters which are used to anchor parts of a pattern. ^ match the beginning of the line $ match the end of the line (before newline) \b match a word boundary \B match a non-word boundary (matches only if pattern contained in a word)

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.; }

Match A Number Of A Certain Format


$str = Cost: \$40,999.95; if ($str =~ /\$\d+,?\d*\.?\d*/) { print Matches; }

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.; }

# match any single vowel

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;

=> There he is.

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;

=> goodbye, world

$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

} print "Formatted text: \n"; print "@var";

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; }

=> The total is 9.

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; }

# compute age # return value in $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 passed arguments

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); }

=> President George Bush

# return a list of arguments

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 a hash of name=>SSN

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 } }

S-ar putea să vă placă și