Presentation is loading. Please wait.

Presentation is loading. Please wait.

● Perl reference

Similar presentations


Presentation on theme: "● Perl reference"— Presentation transcript:

1 ● Perl reference http://www.rexswain.com/perl5.html

2 What is Perl good for?

3 ● Small programs ● Text file processing ● Scripts

4 Running programs

5 ● perl myprogram.perl ●./myprogram.perl

6 Running programs ●./myprogram.perl #!/usr/bin/perl chmod +x myprogram.perl

7 Running programs ● perl -e “print 1+2;” ● echo “print 1+2;” | perl

8 Running programs ● cat inputdata.txt |./myprogram.perl

9 Running programs ● cat inputdata.txt |./myprogram.perl > outputdata.txt

10 Running programs ●./myprogram.perl -i inputdata.txt -o outputdata.txt

11 Programming ● emacs myprogram.perl &

12 Programming #!/usr/local/bin/perl print (“Hello world\n”);

13 Programming #!/usr/local/bin/perl print (“Hello world\n”);

14 Programming #!/usr/local/bin/perl print (“Hello world\n”);

15 Programming #!/usr/local/bin/perl print (“Hello world\n”);

16 Programming #!/usr/local/bin/perl print “Hello world\n”;

17 Variables ● Scalars ● Lists/arrays ● Hashes

18 Variables ● Scalars $myvariable

19 Variables ● Scalars $myvariable

20 Variables ● Scalars $x = “100.000\n”; print $x; $x = $x + 1; print $x;

21 Variables ● Scalars $x = “100.000\n”; #string print $x; $x = $x + 1; print $x;

22 Variables ● Scalars $x = “100.000\n”; print $x; #string $x = $x + 1; print $x;

23 Variables ● Scalars $x = “100.000\n”; print $x; $x = $x + 1; #number print $x;

24 Variables ● Scalars $x = “100.000\n”; print $x; $x = $x + 1; print $x; #string

25 Variables ● Lists/arrays – @myarray – $myarray[0] – (1721, 2974, “blah”) – @myarray[0, 1, 2] – @myarray[0..2]

26 Variables ● Lists/arrays @names = (“Adam”, “Eve”); print($names[0].” likes “.$names[1]);

27 Variables ● Lists/arrays – scalar(@myarray) – $myarray[$x][$y] – @sortedcopy = sort(@myarray)

28 Variables ● Hashes – &myhash – $myhash{“blah”} – $myhash{$key1} = $value1;

29 Variables ● Hashes $darker{“white”} = “grey”; $darker{“grey”} = “black”; print ($darker{“white”}. “ is darker than white.”);

30 Variables ● Hashes – delete($myhash{$key}); – ($key, $value) = each(%myhash); – @mykeys = keys(%myhash); – @myvalues = values(%myhash);

31 Program flow ● Blocks { statement1; statement2; }

32 Program flow ● Conditionals if ($x == $y) { #... } elsif ($x == ($y+1)) { #... } else { #... }

33 Program flow ● Conditionals – True 1, (“a”, “b”), “ “, “hello”, “00” – False 0, (), “”, “0”

34 Program flow ● Conditionals Strings eq ne lt gt Numbers == != < >

35 Program flow ● Conditionals && || ! and or negation

36 Program flow ● Loops for ($t = 0; $t < 100; $t++) { #... }

37 Program flow ● Loops while ($x == $y) { #... }

38 Program flow ● Loops do { #... } while ($x == $y);

39 Program flow ● Loops foreach $key = keys(&myhash) { #... }

40 Program flow ● Loops – last; – next;

41 File handling ● Handles – STDIN – STDOUT

42 File handling open (INPUTFILE, “inputdata.txt”); open (OUTPUTFILE, “>outputdata.txt”); while ($line = ) { print(OUTPUTFILE $line); } close(OUTPUTFILE); close(INPUTFILE);

43 File handling ● Opening “filename” “<filename” “>filename” “>>filename” “+>filename” “| command” “command |” read write, create write, append read, write pipe to command pipe from command

44 File handling ● Tests if (-e “filename”) { #File exists... }

45 File handling ● Tests -r -x -e -d -t -T readable executable exists is a directory is a tty is a text file

46 Command line arguments – @ARGV – scalar(@ARGV)

47 Subroutines sub a_plus_b { exit($_[0] + $_[1]); } print(“1+2=“.&a_plus_b(1, 2));

48 Typical Bugs ● Mistyped identifiers ● Forgetting $ etc. ● Mixing strings and numbers ● Forgetting that variables are global

49 Additional features ● Useful functions (mathematics, strings, etc.) – ($a, $b) = split(“ “, “12 13”); ● Regular expressions ● UNIX system interaction ● Networking ● System VIPC (???) ● Debugger


Download ppt "● Perl reference"

Similar presentations


Ads by Google