Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS 311 - Operating Systems I.

Similar presentations


Presentation on theme: "CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS 311 - Operating Systems I."— Presentation transcript:

1 CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS 311 - Operating Systems I

2 2 Perl Is a high-level, general-purpose, interpreted, dynamic programming language. Borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. Facilitates manipulation of text files. Is also used for graphics programming, system administration, network programming, web development Lecture 072CS 311 - Operating Systems I

3 Getting Perl CPAN – Comprehensive perl Archive Network contains all the perl modules. Download instructions for perl are available on www.perl.comwww.perl.com The CPAN provides separate commands which can be used to install various perl modules. www.cpan.org. www.cpan.org. For windows users download Strawberry Perl. Perl already installed in flip server. Lecture 073CS 311 - Operating Systems I

4 Running perl General syntax – perl [-cv] filename.pl -c syntax is checked but the script is not executed -v version information. Shabang character #! can be used at the first line of the script to indicate that the script is a perl script. – Ex., #!/bin/perl Lecture 074CS 311 - Operating Systems I

5 Variables, strings and integers Perl is a loosely typed language, i.e. the type of a variable may not be declared explicitly. The compiler for perl takes the variable as a string, character or number depending upon the context. $ - denotes a variable – Ex., $i = n, $data = 0; Declaring a variable with “my” keyword makes the variable local to the block (Eg: my $var = 10;) Lecture 075CS 311 - Operating Systems I

6 Range operator and Comments “..” is known as the range operator. – Ex., print 1.. 10; will print the numbers from 1 to 10. In perl comments are given using “#” – print “A”, “BC”, “\n”; #strings – $i = “A”.”B”; # Concatenation Lecture 076CS 311 - Operating Systems I

7 Arrays Similar to C except that the index of an array can be negative. Dynamically allocated @ - denotes an array – Ex., @arr = (1,23,4,5); – @arr = (1..5); An array is accessed using the indices starting from 0. Predefined functions for arrays – push(array, element) – add elements to the end of array – pop(array) – remove the last element in the array – shift(array, element) – add element to the front of array – unshift(array) – remove the front-most element Lecture 077CS 311 - Operating Systems I

8 String operators “.” is for concatenation =~ is used when using regular expressions value=“ABCDEF” To match a substring within another string – $value=~ m/pattern/;($value =~ m/ABC/;) To access the matched pattern use the in-built variable $&. (In the example above “print $&” will print “ABC”) To substitute a substring inside another string – $value =~ s/ABC/def/; #change ‘ABC' to 'def‘ in $value Translate characters – $value =~ tr/A-Z/a-z;#Translate all upper case to lower Lecture 078CS 311 - Operating Systems I

9 Comparison operators Either the numerical value or the string value can be used for an operator Ex. – Equal to --> == or eq – Not equal to --> != or ne – Greater than --> > or gt – Lesser than or equal to --> <= or le Lecture 079CS 311 - Operating Systems I

10 Conditional statements if (condition) { statements } elsif (condition) { statements } else { statements } Lecture 0710CS 311 - Operating Systems I Ex: if ($i 0) { print $i; } elsif ($i > 10) { $i = $i – 10; print $i; } else { print $i.” is negative.”; }

11 Subroutines In perl you can define subroutines in the following format sub function_name(){ statements } A subroutine can be called as follows &function_name(args) Access the parameters to the subroutine using @_ array (Example: @_[0] first arg, @_[1] second arg,…) Lecture 07CS 311 - Operating Systems I11

12 Looping statements while (condition) { statements } for (variable; condition; inc/decrement){ statements } foreach variable (range or array) { statements } Ex., foreach $n (1..15) { } foreach $line (@para) { }#read every array index Lecture 0712CS 311 - Operating Systems I

13 File I/O Filehandles - name in A Perl program for I/O connection between your Perl process and the outside world. Opening a file open(MYDATA, "data.txt"); open NEWDATA, "<moredata.txt"; open RESULT, ">output.dat"; open(LOG, ">>myprog.log"); < - open for reading < - for writing >> - append Default is open for reading. Lecture 0713CS 311 - Operating Systems I

14 File I/O We can reuse filehandles by opening another file using already opened filehandle. If we do this, Perl automatically closes the file currently opened with this filehandle. Closing a file close MYDATA; close(RESULT); Lecture 0714CS 311 - Operating Systems I

15 Opening a file If Perl cannot open the file we requested it returns a false value, true otherwise. my $opened = open(MYDATA, "data.txt"); if( ! $opened ){ print "Error!"; exit 1; } or open(RESULT, ">output.dat") or die("Error");#Handling Exceptions or unless( open LOGFILE, ">>myprog.log" ){ die "Error"; } Lecture 0715CS 311 - Operating Systems I

16 Reading from files To read one line from an open file we can use the <> operator providing the filehandle for the file: Ex. $str = ; This will read the entire line along with the new line character at the end. To remove the newline character use chomp($str); Read the complete file into an array Ex., @lines = ; Lecture 0716CS 311 - Operating Systems I

17 Command Line Arguments $#ARGV – number of command line arguments provided to the script. @ARGV array holds all the command line inputs. Example: @ARGV[0] - first command line arg @ARGV[1] – second arg @ARGV[2] – third arg Lecture 07CS 311 - Operating Systems I17

18 Printing a file if( $#ARGV < 1 ){#access the number of command line args die("Not enough arguments\n"); } open(INP, "<@ARGV[0]") or die("Cannot open file ‘@ARGV[0]' for reading\n"); open(OUTP, “>@ARGV[1]") or die("Cannot open file ‘@ARGV[1]' for writing\n"); my @content = ;#read contents from a file print OUTP sort(@content); #write contents to a file close INP; close OUTP; Lecture 0718CS 311 - Operating Systems I


Download ppt "CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS 311 - Operating Systems I."

Similar presentations


Ads by Google