department of electrical and computer engineering introduction to perl by hector m lugo-cordero...

14
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo- Cordero August 26, 2008

Upload: ruth-howard

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

Department of Electrical and Computer Engineering

Introduction to Perl

By Hector M Lugo-CorderoAugust 26, 2008

Page 2: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

2

Outline

• What is Perl• Variables• Operators• Flow Control

– Conditions

• Subroutines• File Handling• IO• System Calls• Regular Expressions

Page 3: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

3

What is Perl

• Scripting language developed in C– Does not compile– Is run by an interpreter

• #!/bin/perl

• Useful for processing text files• Can be embedded to C/C++

Page 4: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

4

Variables

• Scope– Global (our)– Local (my)

• Type– Scalar $– Array @– Hash %

• Predefined– $^O– $_– $!– @_

Page 5: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

5

Operators

• Addition (+)• Subtraction (–) • Multiplication (*)• Division (/)• Power (**)• Modulus (%)• Increment/Decrement (++/--)• String Concatenation (.)

Page 6: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

6

Flow Control

• If, Elsif, Else• While

– while(<expr>)

• Do-While– do{ … } while(<expr>);

• For – for(my $i = 0; $i < 10; ++$i)

• Foreach– foreach my $var (@myarr)

• unless• or die

Page 7: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

7

Conditions

• Numerical– Equal == | Not equal !=– Greater > | Greater or equal >=– Less < | Less or equal <

• String– Equal eq– Not equal ne

• Complex conditions– and (all conditions must be true)– or (at least one condition must be true)– not (negates an expression)

Page 8: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

8

Sub Routines

• Small blocks of code that perform an action.– Factorial– Absolute Value

• Are user defined– May return a value

• Syntax– sub <subroutine_name>(){

• Code

– }– Parameters are passed using @_

• my $firstarg = shift(@_) or shift()

– Value is returned using return statement• return 0

Page 9: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

9

File Handling

• Reading and writing is done sequentially using file handles.

• open(<FILEHANDLE_NAME>, “<filename>”)

• $var = <FILEHANDLE_NAME>• @array = <FILEHANDLE_NAME>• while(<FILEHANDLE_NAME>){

– chomp($_)– $_

• }• close(<FILEHANDLE_NAME>)

Page 10: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

10

IO

• Reading from the standard input– $number = <STDIN>;

• Writing to the standard output– print “<MESSAGE>”;

• print “$variable”; #displays the value of $variable• print ‘$variable’; #displays the word $variable

• Reading from file handle DATA– $number = <DATA>;

• Writing to file handle DATA– print DATA “<MESSAGE>”;– print <<DATA;

• Any string

– DATA

Page 11: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

11

System Calls

• mkdir• rmdir• chdir• rename• system• exec• Shell Commands• open(FILE, “ls |”);

Page 12: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

12

Regular Expressions

• Search a string for a given pattern • $var =~ /hello/; is true if $var contains the

string hello• $num =~ /([0-9]*)(.*)/

– $1 contains a string full of numbers– $2 contains the string after the first pattern of

numbers

• $str =~ s/hello(.*)/$1/;– Replaces the string in the first // for the one in

the second //

• if($_ =~ /$name/i) searches for a the value inside of $name in $_ ignoring the case

Page 13: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

13

Regular Expressions

• Characters that must be escaped– /– .– $– @– %– \– *

Page 14: Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008

The Department of Electrical and Computer Engineering

14

Questions?