Upcoming SlideShare
×

# ABC of Perl programming

893 views

Published on

Fundamental of Perl programming

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
893
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
27
0
Likes
1
Embeds 0
No embeds

No notes for slide

### ABC of Perl programming

1. 1. Perl ABC Part I David Young yangboh@cn.ibm.com Jan. 2011
2. 2. Perl ABC Data StructureData Structure Scalar List Hash Reference Filehandle Function
3. 3. Perl ABC Data StructureScalar A number A string A referenceList A list is ordered scalar data.Hash Associative arrays
4. 4. Perl ABC Data StructureList examples A list is ordered scalar data. #+begin_src perl  @a = ("fred","barney","betty","wilma"); # ugh! @a = qw(fred barney betty wilma);       # better! @a = qw(     fred     barney     betty     wilma );          # same thing                       #+end_src
5. 5. Perl ABC Data StructureHash examples "associative arrays" #+begin_src perl  #+begin_src perl %words = ( %words = qw(     fred   => "camel",     fred   camel     barney => "llama",     barney llama     betty  => "alpaca",     betty  alpaca     wilma  => "alpaca",     wilma  alpaca ); ); #+end_src #+end_src
6. 6. Perl ABC Data StructureHash continue … #+begin_src perl  @words = qw(     fred       camel     barney     llama     betty      alpaca     wilma      alpaca ); %words = @words; #+end_src
7. 7. Perl ABC Data StructureSpecial Things – Nested List There is NOT anythig like list of lists #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", @a, "jerry");  # be careful # what you actually get is ­­  @b = qw(tom fred barney betty wilma jerry);  #+end_src
8. 8. Perl ABC Data StructureSpecial Things – Nested List But … there is nested list in the real world What you really mean is #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     @b = ("tom", @a, "jerry"); #+end_src #+begin_src perl  \$c = [ @a ];  \$c = @a; @b = ("tom", \$c, "jerry");      #+end_src
9. 9. Perl ABC Data StructureSpecial Things – Nested Hash There is nested hash in the real world #+begin_src perl  \$words_nest{ mash } = {     captain  => "pierce", %words_nest = (     major    => "burns",     fred    => "camel",     barney  => "llama",     corporal => "radar", };     betty   => "alpaca",     wilma   => "alpaca",     jetsons => {         husband   => "george",         wife      => "jane",         "his boy" => "elroy",  # Key quotes needed.     }, ); #+end_src
10. 10. Perl ABC Data AccessData Access Access Scalar Data Access List Data Access Hash Data
11. 11. Perl ABC Data AccessData Access Access Scalar Data Access List Data Access Hash Data
12. 12. Perl ABC Data AccessScalar \$fred   = "pay"; \$fredday = "wrong!"; \$barney = "Its \$fredday";                             # not payday, but "Its wrong!" \$barney = "Its \${fred}day";                           # now, \$barney gets "Its payday" \$barney2 = "Its \$fred"."day";                         # another way to do it \$barney3 = "Its " . \$fred . "day";                    # and another way
13. 13. Perl ABC Data AccessData Access Access Scalar Data Access List Data Access Hash Data
14. 14. Perl ABC Data AccessList -- access individully #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     #+end_src #+begin_src perl  \$c = @a;                       # \$c = 4; \$c = \$b[0];                    # \$c = tom #+end_src
15. 15. Perl ABC Data AccessList -- slicing access #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     #+end_src #+begin_src perl  @c = @a;                    # list copy (\$aa, \$ab, \$ac @c) = @a;    # @c = qw(wilma); @c = @a[1,2,3];            # @c = qw(barney betty wilma); @c = @a[1..3];              # same thing   @a[1,2] = @a[2,1];          # switch value #+end_src
16. 16. Perl ABC Data AccessList – access as a whole foreach map grep
17. 17. Perl ABC Data AccessList – access as a whole foreach #+begin_src perl @a = (3,5,7,9); foreach \$one (@a) {     \$one *= 3; } # @a is now (9,15,21,27) Notice how altering \$one in fact altered each element of @a.  This is a feature, not a bug.
18. 18. Perl ABC Data AccessList – access as a whole map @a = (3,5,7,9); @b = map { \$_ * 3 } @a;            # @b is now (9,15,21,27) @c = map { \$_ > 5 } @a;            # @c is now (,1,1)  grep @a = (3,5,7,9); @c = grep { \$_ > 5 } @a;           # @c is now (7,9) @c = grep { \$_ > 5 ? \$_ : ()} @a;  # equivalent as map
19. 19. Perl ABC Data AccessList – access as a whole map and equivalent foreach @a = (3,5,7,9); @b = map { \$_ * 3 } @a;     # @b is now (9,15,21,27) # equivalent foreach  foreach my \$a (@a) {     push @b, \$a * 3;        # did not return values }
20. 20. Perl ABC Data AccessList – access as a whole sub time3 {  map and equivalent foreach    my \$num = shift;  @a = (3,5,7,9);    return \$num * 3 } @b = map { \$_ * 3 } @a;      \$func = sub {     my \$num = shift;     return \$num * 3 } # equivalents  sub my_map { @b = map &time3(\$_) @a;  my (\$func, \$data) = @_; @b = map &\$func(\$_) @a;  foreach \$a (@\$data) { @b = my_map &time3, @a;     push @b, &\$func(\$a);  @b = my_map \$func, @a;  }  return @b; }
21. 21. Perl ABC Data AccessHash -- access individully #+begin_src perl  %words = (     fred   => "camel",     barney => "llama",     betty  => "alpaca",     wilma  => "alpaca", ); #+end_src #+begin_src perl   \$c = \$words{"fred"};   # \$c = camel  \$d = "barney"; \$e = \$words{\$d};       # \$e = llama #+end_src
22. 22. Perl ABC Data AccessHash -- access as a whole#+begin_src perl  #+begin_src perl%words = (    @key_list = keys(%words);  fred   => "camel",    @value_list = values(%words);  barney => "llama", #+end_src  betty  => "alpaca",  wilma  => "alpaca", #+begin_src perl); foreach \$key (keys(%words){#+end_src      print \$words{\$key}, "n"; } #+end_src #+begin_src perl foreach \$value (values(%words){      print \$value, "n"; } #+end_src
23. 23. Perl ABC Data AccessList – access nested elements #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     @b = ("tom", @a, "jerry"); #+end_src #+begin_src perl \$a = \$b[1];              # \$a = [ @a ]   \$c = \$b[1]­>[1];         # \$c = barney \$c = @b[1][1];           # same thing \$c = @\$a­>[1];           # same thing \$c = \${\$a}[1];           # same thing #+end_src
24. 24. Perl ABC Data AccessHash – access nested elements %h_nest = (     fred    => "camel",     barney  => "llama",     betty   => "alpaca",     wilma   => "alpaca",     jetsons => {         husband   => "george",         wife      => "jane",         "his boy" => "elroy",     }, ); \$c = \$h_nest{"jetsons"}{"wife"};  # \$c = jane \$j = "jetsons";  \$w = "wife";  \$c = \$h_nest{\$j}{\$w};             # same thing \$jet = \$h_nest("jetsons"};        # \$jet has a hash \$d = \$jet{"husband"};             # \$d = george
25. 25. Perl ABC Data AccessReference# Create some variables\$a      = "mama mia";@array  = (10, 20);%hash   = ("laurel" => "hardy", "nick" =>  "nora");# Now create references to them\$r_a     = \$a;      # \$ra now "refers" to (points to) \$a\$r_array = @array;\$r_hash  = %hash;
26. 26. Perl ABC Data AccessAccess Reference Data # Now create references to them \$r_a     = \$a;      # \$ra now "refers" to (points to) \$a \$r_array = @array; \$r_hash  = %hash; # Now access the referenced data \$r_a;         # the address \$\$r_a;        # the \$a;       "mama mia"; \$r_array      # the address @\$r_array     # the array     (10, 20); @\$r_array[1]  # the element   20;
27. 27. Perl ABC Data AccessAccess Reference Data # Now create references to them \$r_a     = \$a;      # \$ra now "refers" to (points to) \$a \$r_array = @array; \$r_hash  = %hash; # Now access the referenced data \$r_a;         # the address \$\$r_a;        # the \$a;       "mama mia"; \$r_array      # the address @\$r_array     # the array     (10, 20); @\$r_array[1]  # the element   20;