Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.

Similar presentations


Presentation on theme: "Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting."— Presentation transcript:

1 Perl: Lecture 1 The language

2 What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting language – Ability to change everything during runtime – Fast to employ, one-liners possible – Slower than C

3 What Perl is Easy to learn – Learning curve similar to human language – More difficult things possible Tell it to be more strict Object orientation Esp. suited for the web & text processing – Regular expressions

4 How to get & use it http://www.perl.com – ActiveState makes port for Microsoft Windows Current Version is 5.8.0 Comprehensive documentation included – C:\Perl\html\index.html – Perldoc Running perl scripts – perl –w script.pl – #!/usr/bin/perl + chmod (Unix)

5 Variables Scalars$ Arrays@ Hash% No need to declare variables Namespace for each variable type Case sensitive

6 Scalars Number String Reference Automatic conversion of numbers and strings $i = 1; $j = "2"; print "$i\n"; print "$j\n"; $k = $i + $j; print "$k\n"; print $i. $j. "\n";

7 Scalar Comparison Operators Number String ==eg <>ne <lt >gt <=le =>ge

8 Truth Any String is true except for „“ and „0“ Any number is true except for 0 Any reference is true Any undefined variable is false

9 The Perl if statement if ($var==1) { commands1; } elsif ($var==2){ commands2; } else{ commands3; } unless ($var==3) {commands4; }

10 Arrays Multivalued Variable Lookup by number List Assignments Accessing @home = ("couch", "chair", "table", "stove") ($one, $two, $three, $four) = @home ($one, $two) = ($two, $one) $home[0] - $home[3]

11 Hashes Multivalued Variable Lookup by name List Assignments Accessing %longday = ("Sun" => "Sunday", "Mon" => "Monday", "Tue" => "Tuesday" ); @list = %longday $longday{"Sun"}, $longday{"Mon"}

12 Quoting in Perl Double Quotes ““ interprete variables and backslashes Single Quotes ‘‘ don‘t Own Quoting characters q//, qq// $one=“two”; $two=“four”; print ‘$one + $one is $two \n’; print “$one + $one is $two \n”;

13 Operators (1) Arithmetic String Logical %Modulus **Exponentiation.String concatenation xRepeat operator &&, andAND ||, orOR !, notNOT

14 Operators (2) File test operators -eExists -rReadable -wWritable -dIs a directory -fIs a regular file -TIs a Text file

15 Iterative Structures For While Foreach while ($x<0.5) { $x = rand; } for ($x=0; $x<10; $x++) { print “$x\n“; } foreach $line (@lines) { if ($line eq ““) last; print reverse $line; }

16 Next and Last next ( continue in C) : start the next iteration of the loop. last ( break in C) : exit the loop.

17 Working with Files open FILEHANDLE, EXPRESSION – Filehandle : means to access opened file – Expression : path of file to open, file mode „<“read (default) „>“ truncate and write „>>“append close FILEHANDLE open LOG, ‘>>/var/log/mylog‘; close LOG;

18 Input and Ouput Print FILEHANDLE OUTPUT „ “ line reading operator print LOG “Debug Message“; $line = ; @wholefile = ; while ($line = ) { print $line; }

19 Default Variable „$_“ default variable for functions $_ = „222“; print log; print while(<>);

20 Subroutines Equivalent of C functions Argument list, result list, no need to specify length sub NAME {}# declaration &NAME();# invocation sub arguments { my @params = @_; return @params; } @result = &arguments(@input);

21 Subroutines 2 „Named Parameters“ sub printtimes { my $string = shift; my $times = shift; while ($times-->0) { print $string; } $printtimes(„BA Stuggi\n“, 10);

22 Subroutines bonus „@_“ for the main program : „@ARGV“ if (defined $ARGV[0]) { $loga = log $ARGV[0]; print $loga; } print log (shift or exit);

23 Perl Regular Expressions (1) (perlrequick)

24 Simple Pattern Matching =~ Operator : boolean – TRUE for a match – FALSE for no match "Hello World" =~ /World/; # matches regex delimiters expression to match

25 Simple Pattern Matching Usage 1.if ("Hello World" =~ /World/) print "It matches\n " ; 2.if ("Hello World" !~ /World/) print " No match\n " ; 3.$teststring= " Hello World "; $greeting = "World"; if ($teststring =~ /$greeting/) print "It matches\n " ; 4.$_ = "Hello World"; if (/World/) print "It matches\n";

26 Simple Pattern Matching Bonus Delimiters changable if „m“ is introduced Match always at the earliest point possible Special characters („metacharacters“) require masking "Hello World" =~ m!World!; # matches {}[]()^$.|*+?\ "2+2=4" =~ /2\+2/; # matches, 'C:\WIN32' =~ /C:\\WIN/; # matches

27 Scalar Manipulation (1) chop VAR / chomp VAR – Remove last character / Only if newline and return it lc EXPR – Returns a lowercased EXPR length EXPR – Returns number of characters in EXPR index STR, SUBSTR [, POS] – Returns first position of SUBSTR in STR [after POS] rindex STR, SUBSTR [,POS] – See above, uses last occurence

28 Scalar Manipulation (2) sprintf FORMAT, LIST %c a character with the given number %sa string %d a signed integer, in decimal %u an unsigned integer, in decimal %x an unsigned integer, in hexadecimal %f a floating-point number, in fixed decimal notation substr EXPR,OFFSET [,LENGTH] – return the substring of EXPR starting from OFFSET uc EXPR – Returns an uppercased EXPR


Download ppt "Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting."

Similar presentations


Ads by Google