Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008."— Presentation transcript:

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

2 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

3 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++

4 The Department of Electrical and Computer Engineering 4 Variables Scope –Global (our) –Local (my) Type –Scalar $ –Array @ –Hash % Predefined –$^O –$_ –$! –@_

5 The Department of Electrical and Computer Engineering 5 Operators Addition (+) Subtraction (–) Multiplication (*) Division (/) Power (**) Modulus (%) Increment/Decrement (++/--) String Concatenation (.)

6 The Department of Electrical and Computer Engineering 6 Flow Control If, Elsif, Else While –while( ) Do-While –do{ … } while( ); For –for(my $i = 0; $i < 10; ++$i) Foreach –foreach my $var (@myarr) unless or die

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

8 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 (){ Code –} –Parameters are passed using @_ my $firstarg = shift(@_) or shift() –Value is returned using return statement return 0

9 The Department of Electrical and Computer Engineering 9 File Handling Reading and writing is done sequentially using file handles. open(, “ ”) $var = @array = while( ){ –chomp($_) –$_ } close( )

10 The Department of Electrical and Computer Engineering 10 IO Reading from the standard input –$number = ; Writing to the standard output –print “ ”; print “$variable”; #displays the value of $variable print ‘$variable’; #displays the word $variable Reading from file handle DATA –$number = ; Writing to file handle DATA –print DATA “ ”; –print <<DATA; Any string –DATA

11 The Department of Electrical and Computer Engineering 11 System Calls mkdir rmdir chdir rename system exec Shell Commands open(FILE, “ls |”);

12 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

13 The Department of Electrical and Computer Engineering 13 Regular Expressions Characters that must be escaped –/ –. –$ –@ –% –\ –*

14 The Department of Electrical and Computer Engineering 14 Questions?


Download ppt "Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008."

Similar presentations


Ads by Google