First stab at 5.003 -> 5.004 integration.
[p5sagit/p5-mst-13.2.git] / pod / perldsc.pod
index 6d31976..48750dd 100644 (file)
@@ -30,7 +30,7 @@ with three dimensions!
 Alas, however simple this may appear, underneath it's a much more
 elaborate construct than meets the eye!
 
-How do you print it out?  Why can't you just say C<print @LoL>?  How do
+How do you print it out?  Why can't you say just C<print @LoL>?  How do
 you sort it?  How can you pass it to a function or get one of these back
 from a function?  Is is an object?  Can you save it to disk to read
 back later?  How do you access whole rows or columns of that matrix?  Do
@@ -41,14 +41,14 @@ of the blame for this can be attributed to the reference-based
 implementation, it's really more due to a lack of existing documentation with
 examples designed for the beginner.
 
-This document is meant to be a detailed but understandable treatment of
-the many different sorts of data structures you might want to develop.  It should
-also serve as a cookbook of examples.  That way, when you need to create one of these
-complex data structures, you can just pinch, pilfer, or purloin
-a drop-in example from here.
+This document is meant to be a detailed but understandable treatment of the
+many different sorts of data structures you might want to develop.  It
+should also serve as a cookbook of examples.  That way, when you need to
+create one of these complex data structures, you can just pinch, pilfer, or
+purloin a drop-in example from here.
 
 Let's look at each of these possible constructs in detail.  There are separate
-documents on each of the following:
+sections on each of the following:
 
 =over 5
 
@@ -62,10 +62,6 @@ documents on each of the following:
 
 =item * more elaborate constructs
 
-=item * recursive and self-referential data structures
-
-=item * objects
-
 =back
 
 But for now, let's look at some of the general issues common to all
@@ -76,15 +72,15 @@ of these types of data structures.
 The most important thing to understand about all data structures in Perl
 -- including multidimensional arrays--is that even though they might
 appear otherwise, Perl C<@ARRAY>s and C<%HASH>es are all internally
-one-dimensional.  They can only hold scalar values (meaning a string,
+one-dimensional.  They can hold only scalar values (meaning a string,
 number, or a reference).  They cannot directly contain other arrays or
 hashes, but instead contain I<references> to other arrays or hashes.
 
-You can't use a reference to a array or hash in quite the same way that
-you would a real array or hash.  For C or C++ programmers unused to distinguishing
-between arrays and pointers to the same, this can be confusing.  If so,
-just think of it as the difference between a structure and a pointer to a
-structure.
+You can't use a reference to a array or hash in quite the same way that you
+would a real array or hash.  For C or C++ programmers unused to
+distinguishing between arrays and pointers to the same, this can be
+confusing.  If so, just think of it as the difference between a structure
+and a pointer to a structure.
 
 You can (and should) read more about references in the perlref(1) man
 page.  Briefly, references are rather like pointers that know what they
@@ -102,7 +98,7 @@ multidimensional arrays work as well.
     $hash{string}[7]                   # hash of arrays
     $hash{string}{'another string'}    # hash of hashes
 
-Now, because the top level only contains references, if you try to print
+Now, because the top level contains only references, if you try to print
 out your array in with a simple print() function, you'll get something
 that doesn't look very nice, like this:
 
@@ -138,7 +134,7 @@ might do well to consider being a tad more explicit about it, like this:
 
     for $i (1..10) {
        @list = somefunc($i);
-       $counts[$i] = scalar @list;     
+       $counts[$i] = scalar @list;
     }
 
 Here's the case of taking a reference to the same memory location
@@ -149,7 +145,7 @@ again and again:
        $LoL[$i] = \@list;      # WRONG!
     }
 
-So, just what's the big problem with that?  It looks right, doesn't it?
+So, what's the big problem with that?  It looks right, doesn't it?
 After all, I just told you that you need an array of references, so by
 golly, you've made me one!
 
@@ -218,7 +214,7 @@ something is "interesting", that rather than meaning "intriguing",
 they're disturbingly more apt to mean that it's "annoying",
 "difficult", or both?  :-)
 
-So just remember to always use the array or hash constructors with C<[]>
+So just remember always to use the array or hash constructors with C<[]>
 or C<{}>, and you'll be fine, although it's not always optimally
 efficient.
 
@@ -290,21 +286,21 @@ this:
     my $listref = [
        [ "fred", "barney", "pebbles", "bambam", "dino", ],
        [ "homer", "bart", "marge", "maggie", ],
-       [ "george", "jane", "alroy", "judy", ],
+       [ "george", "jane", "elroy", "judy", ],
     ];
 
     print $listref[2][2];
 
 The compiler would immediately flag that as an error I<at compile time>,
 because you were accidentally accessing C<@listref>, an undeclared
-variable, and it would thereby remind you to instead write:
+variable, and it would thereby remind you to write instead:
 
     print $listref->[2][2]
 
 =head1 DEBUGGING
 
-Before 5.002, the standard Perl debugger didn't do a very nice job of
-printing out complex data structures.  With version 5.002 or above, the
+Before version 5.002, the standard Perl debugger didn't do a very nice job of
+printing out complex data structures.  With 5.002 or above, the
 debugger includes several new features, including command line editing as
 well as the C<x> command to dump out complex data structures.  For
 example, given the assignment to $LoL above, here's the debugger output:
@@ -325,14 +321,14 @@ example, given the assignment to $LoL above, here's the debugger output:
        2  ARRAY(0x13b540)
          0  'george'
          1  'jane'
-         2  'alroy'
+         2  'elroy'
          3  'judy'
 
-There's also a lower-case B<x> command which is nearly the same.
+There's also a lowercase B<x> command which is nearly the same.
 
 =head1 CODE EXAMPLES
 
-Presented with little comment (these will get their own man pages someday)
+Presented with little comment (these will get their own manpages someday)
 here are short code examples illustrating access of various
 types of data structures.
 
@@ -387,7 +383,7 @@ types of data structures.
 
  # print the whole thing one at a time
  for $i ( 0 .. $#LoL ) {
-     for $j ( 0 .. $#{$LoL[$i]} ) {
+     for $j ( 0 .. $#{ $LoL[$i] } ) {
          print "elt $i $j is $LoL[$i][$j]\n";
      }
  }
@@ -397,9 +393,9 @@ types of data structures.
 =head2 Declaration of a HASH OF LISTS
 
  %HoL = (
-        "flintstones"        => [ "fred", "barney" ],
-        "jetsons"            => [ "george", "jane", "elroy" ],
-        "simpsons"           => [ "homer", "marge", "bart" ],
+        flintstones        => [ "fred", "barney" ],
+        jetsons            => [ "george", "jane", "elroy" ],
+        simpsons           => [ "homer", "marge", "bart" ],
       );
 
 =head2 Generation of a HASH OF LISTS
@@ -449,19 +445,24 @@ types of data structures.
  # print the whole thing with indices
  foreach $family ( keys %HoL ) {
      print "family: ";
-     foreach $i ( 0 .. $#{ $HoL{$family} ) {
+     foreach $i ( 0 .. $#{ $HoL{$family} } ) {
          print " $i = $HoL{$family}[$i]";
      }
      print "\n";
  }
 
  # print the whole thing sorted by number of members
- foreach $family ( sort { @{$HoL{$b}} <=> @{$HoL{$b}} } keys %HoL ) {
+ foreach $family ( sort { @{$HoL{$b}} <=> @{$HoL{$a}} } keys %HoL ) {
      print "$family: @{ $HoL{$family} }\n"
  }
 
  # print the whole thing sorted by number of members and name
- foreach $family ( sort { @{$HoL{$b}} <=> @{$HoL{$a}} } keys %HoL ) {
+ foreach $family ( sort {
+                           @{$HoL{$b}} <=> @{$HoL{$a}}
+                                       ||
+                                   $a cmp $b
+           } keys %HoL )
+ {
      print "$family: ", join(", ", sort @{ $HoL{$family}), "\n";
  }
 
@@ -509,7 +510,7 @@ types of data structures.
 
  # calling a function  that returns a key,value list, like
  # "lead","fred","daughter","pebbles"
- while ( %fields = getnextpairset() )
+ while ( %fields = getnextpairset() ) {
      push @LoH, { %fields };
  }
 
@@ -560,19 +561,19 @@ types of data structures.
 =head2 Declaration of a HASH OF HASHES
 
  %HoH = (
-        "flintstones" => {
-            "lead"    => "fred",
-            "pal"     => "barney",
+        flintstones => {
+               lead      => "fred",
+               pal       => "barney",
         },
-        "jetsons"     => {
-            "lead"    => "george",
-            "wife"    => "jane",
-            "his boy" => "elroy",
+        jetsons     => {
+               lead      => "george",
+               wife      => "jane",
+               "his boy" => "elroy",
         },
-        "simpsons"    => {
-             "lead"   => "homer",
-             "wife"   => "marge",
-             "kid"    => "bart",
+        simpsons    => {
+               lead      => "homer",
+               wife      => "marge",
+               kid       => "bart",
        },
  );
 
@@ -601,12 +602,6 @@ types of data structures.
      }
  }
 
- # calling a function  that returns a key,value list, like
- # "lead","fred","daughter","pebbles"
- while ( %fields = getnextpairset() )
-     push @a, { %fields };
- }
-
  # calling a function  that returns a key,value hash
  for $group ( "simpsons", "jetsons", "flintstones" ) {
      $HoH{$group} = { get_family($group) };
@@ -620,8 +615,8 @@ types of data structures.
 
  # append new members to an existing family
  %new_folks = (
-     "wife" => "wilma",
-     "pet"  => "dino";
+     wife => "wilma",
+     pet  => "dino";
  );
 
  for $what (keys %new_folks) {
@@ -638,7 +633,7 @@ types of data structures.
 
  # print the whole thing
  foreach $family ( keys %HoH ) {
-     print "$family: ";
+     print "$family: { ";
      for $role ( keys %{ $HoH{$family} } ) {
          print "$role=$HoH{$family}{$role} ";
      }
@@ -647,7 +642,7 @@ types of data structures.
 
  # print the whole thing  somewhat sorted
  foreach $family ( sort keys %HoH ) {
-     print "$family: ";
+     print "$family: { ";
      for $role ( sort keys %{ $HoH{$family} } ) {
          print "$role=$HoH{$family}{$role} ";
      }
@@ -656,8 +651,8 @@ types of data structures.
 
 
  # print the whole thing sorted by number of members
- foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$b}} } keys %HoH ) {
-     print "$family: ";
+ foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$a}} } keys %HoH ) {
+     print "$family: { ";
      for $role ( sort keys %{ $HoH{$family} } ) {
          print "$role=$HoH{$family}{$role} ";
      }
@@ -669,10 +664,10 @@ types of data structures.
  for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }
 
  # now print the whole thing sorted by number of members
- foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$b}} } keys %HoH ) {
-     print "$family: ";
+ foreach $family ( sort { keys %{ $HoH{$b} } <=> keys %{ $HoH{$a} } } keys %HoH ) {
+     print "$family: { ";
      # and print these according to rank order
-     for $role ( sort { $rank{$a} <=> $rank{$b} keys %{ $HoH{$family} } ) {
+     for $role ( sort { $rank{$a} <=> $rank{$b} }  keys %{ $HoH{$family} } ) {
          print "$role=$HoH{$family}{$role} ";
      }
      print "}\n";
@@ -703,8 +698,8 @@ many different sorts:
      print $rec->{LOOKUP}{"key"};
      ($first_k, $first_v) = each %{ $rec->{LOOKUP} };
 
-     $answer = &{ $rec->{THATCODE} }($arg);
-     $answer = &{ $rec->{THISCODE} }($arg1, $arg2);
+     $answer = $rec->{THATCODE}->($arg);
+     $answer = $rec->{THISCODE}->($arg1, $arg2);
 
      # careful of extra block braces on fh ref
      print { $rec->{HANDLE} } "a string\n";
@@ -716,7 +711,7 @@ many different sorts:
 =head2 Declaration of a HASH OF COMPLEX RECORDS
 
      %TV = (
-        "flintstones" => {
+        flintstones => {
             series   => "flintstones",
             nights   => [ qw(monday thursday friday) ],
             members  => [
@@ -726,7 +721,7 @@ many different sorts:
             ],
         },
 
-        "jetsons"     => {
+        jetsons     => {
             series   => "jetsons",
             nights   => [ qw(wednesday saturday) ],
             members  => [
@@ -736,7 +731,7 @@ many different sorts:
             ],
          },
 
-        "simpsons"    => {
+        simpsons    => {
             series   => "simpsons",
             nights   => [ qw(monday) ],
             members  => [
@@ -752,7 +747,7 @@ many different sorts:
      # reading from file
      # this is most easily done by having the file itself be
      # in the raw data format as shown above.  perl is happy
-     # to parse complex datastructures if declared as data, so
+     # to parse complex data structures if declared as data, so
      # sometimes it's easiest to do that
 
      # here's a piece by piece build up
@@ -762,7 +757,7 @@ many different sorts:
 
      @members = ();
      # assume this file in field=value syntax
-     while () {
+     while (<>) {
          %fields = split /[\s=]+/;
          push @members, { %fields };
      }
@@ -782,7 +777,7 @@ many different sorts:
      foreach $family (keys %TV) {
          $rec = $TV{$family}; # temp pointer
          @kids = ();
-         for $person ( @{$rec->{members}} ) {
+         for $person ( @{ $rec->{members} } ) {
              if ($person->{role} =~ /kid|son|daughter/) {
                  push @kids, $person;
              }
@@ -811,7 +806,7 @@ many different sorts:
          for $who ( @{ $TV{$family}{members} } ) {
              print " $who->{name} ($who->{role}), age $who->{age}\n";
          }
-         print "it turns out that $TV{$family}{'lead'} has ";
+         print "it turns out that $TV{$family}{lead} has ";
          print scalar ( @{ $TV{$family}{kids} } ), " kids named ";
          print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } );
          print "\n";
@@ -823,17 +818,17 @@ You cannot easily tie a multilevel data structure (such as a hash of
 hashes) to a dbm file.  The first problem is that all but GDBM and
 Berkeley DB have size limitations, but beyond that, you also have problems
 with how references are to be represented on disk.  One experimental
-module that does attempt to partially address this need is the MLDBM
-module.  Check your nearest CPAN site as described in L<perlmod> for
+module that does partially attempt to address this need is the MLDBM
+module.  Check your nearest CPAN site as described in L<perlmodlib> for
 source code to MLDBM.
 
 =head1 SEE ALSO
 
-L<perlref>, L<perllol>, L<perldata>, L<perlobj>
+perlref(1), perllol(1), perldata(1), perlobj(1)
 
 =head1 AUTHOR
 
-Tom Christiansen E<lt>F<tchrist@perl.com>E<gt>
+Tom Christiansen <F<tchrist@perl.com>>
 
 Last update:
-Mon Jul  8 05:22:49 MDT 1996
+Wed Oct 23 04:57:50 MET DST 1996