[PATCH] perlcommunity.pod: add information about OSDC.fr
[p5sagit/p5-mst-13.2.git] / pod / perlfaq4.pod
index e9c4ab3..8d93d3f 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq4 - Data Manipulation ($Revision: 6816 $)
+perlfaq4 - Data Manipulation
 
 =head1 DESCRIPTION
 
@@ -17,7 +17,7 @@ exactly.  Some real numbers lose precision in the process.  This is a
 problem with how computers store numbers and affects all computer
 languages, not just Perl.
 
-L<perlnumber> show the gory details of number representations and
+L<perlnumber> shows the gory details of number representations and
 conversions.
 
 To limit the number of decimal places in your numbers, you can use the
@@ -48,35 +48,45 @@ numbers.  What you think in the above as 'three' is really more like
 
 =head2 Why isn't my octal data interpreted correctly?
 
-Perl only understands octal and hex numbers as such when they occur as
-literals in your program.  Octal literals in perl must start with a
-leading C<0> and hexadecimal literals must start with a leading C<0x>.
-If they are read in from somewhere and assigned, no automatic
-conversion takes place.  You must explicitly use C<oct()> or C<hex()> if you
-want the values converted to decimal.  C<oct()> interprets hexadecimal (C<0x350>),
-octal (C<0350> or even without the leading C<0>, like C<377>) and binary
-(C<0b1010>) numbers, while C<hex()> only converts hexadecimal ones, with
-or without a leading C<0x>, such as C<0x255>, C<3A>, C<ff>, or C<deadbeef>.
-The inverse mapping from decimal to octal can be done with either the
-<%o> or C<%O> C<sprintf()> formats.
+(contributed by brian d foy)
+
+You're probably trying to convert a string to a number, which Perl only
+converts as a decimal number. When Perl converts a string to a number, it
+ignores leading spaces and zeroes, then assumes the rest of the digits
+are in base 10:
+
+       my $string = '0644';
+
+       print $string + 0;  # prints 644
+
+       print $string + 44; # prints 688, certainly not octal!
 
-This problem shows up most often when people try using C<chmod()>,
-C<mkdir()>, C<umask()>, or C<sysopen()>, which by widespread tradition
-typically take permissions in octal.
+This problem usually involves one of the Perl built-ins that has the
+same name a unix command that uses octal numbers as arguments on the
+command line. In this example, C<chmod> on the command line knows that
+its first argument is octal because that's what it does:
 
-       chmod(644,  $file);   # WRONG
-       chmod(0644, $file);   # right
+       %prompt> chmod 644 file
 
-Note the mistake in the first line was specifying the decimal literal
-C<644>, rather than the intended octal literal C<0644>.  The problem can
-be seen with:
+If you want to use the same literal digits (644) in Perl, you have to tell
+Perl to treat them as octal numbers either by prefixing the digits with
+a C<0> or using C<oct>:
 
-       printf("%#o",644);   # prints 01204
+       chmod(     0644, $file);   # right, has leading zero
+       chmod( oct(644), $file );  # also correct
 
-Surely you had not intended C<chmod(01204, $file);> - did you?  If you
-want to use numeric literals as arguments to chmod() et al. then please
-try to express them as octal constants, that is with a leading zero and
-with the following digits restricted to the set C<0..7>.
+The problem comes in when you take your numbers from something that Perl
+thinks is a string, such as a command line argument in C<@ARGV>:
+
+       chmod( $ARGV[0],      $file);   # wrong, even if "0644"
+
+       chmod( oct($ARGV[0]), $file );  # correct, treat string as octal
+
+You can always check the value you're using by printing it in octal
+notation to ensure it matches what you think it should be. Print it
+in octal  and decimal format:
+
+       printf "0%o %d", $number, $number;
 
 =head2 Does Perl have a round() function?  What about ceil() and floor()?  Trig functions?
 
@@ -362,19 +372,18 @@ pseudorandom generator than comes with your operating system, look at
 
 =head2 How do I get a random number between X and Y?
 
-To get a random number between two values, you can use the
-C<rand()> builtin to get a random number between 0 and
+To get a random number between two values, you can use the C<rand()>
+built-in to get a random number between 0 and 1. From there, you shift
+that into the range that you want.
 
-C<rand($x)> returns a number such that
-C<< 0 <= rand($x) < $x >>. Thus what you want to have perl
-figure out is a random number in the range from 0 to the
-difference between your I<X> and I<Y>.
+C<rand($x)> returns a number such that C<< 0 <= rand($x) < $x >>. Thus
+what you want to have perl figure out is a random number in the range
+from 0 to the difference between your I<X> and I<Y>.
 
-That is, to get a number between 10 and 15, inclusive, you
-want a random number between 0 and 5 that you can then add
-to 10.
+That is, to get a number between 10 and 15, inclusive, you want a
+random number between 0 and 5 that you can then add to 10.
 
-       my $number = 10 + int rand( 15-10+1 );
+       my $number = 10 + int rand( 15-10+1 ); # ( 10,11,12,13,14, or 15 )
 
 Hence you derive the following simple function to abstract
 that. It selects a random integer between the two given
@@ -479,6 +488,9 @@ Julian day)
        31
 
 =head2 How do I find yesterday's date?
+X<date> X<yesterday> X<DateTime> X<Date::Calc> X<Time::Local>
+X<daylight saving time> X<day> X<Today_and_Now> X<localtime>
+X<timelocal>
 
 (contributed by brian d foy)
 
@@ -491,20 +503,36 @@ give you the same time of day, only the day before.
 
        print "Yesterday was $yesterday\n";
 
-You can also use the C<Date::Calc> module using its Today_and_Now
+You can also use the C<Date::Calc> module using its C<Today_and_Now>
 function.
 
        use Date::Calc qw( Today_and_Now Add_Delta_DHMS );
 
        my @date_time = Add_Delta_DHMS( Today_and_Now(), -1, 0, 0, 0 );
 
-       print "@date\n";
+       print "@date_time\n";
 
 Most people try to use the time rather than the calendar to figure out
 dates, but that assumes that days are twenty-four hours each.  For
 most people, there are two days a year when they aren't: the switch to
 and from summer time throws this off. Let the modules do the work.
 
+If you absolutely must do it yourself (or can't use one of the
+modules), here's a solution using C<Time::Local>, which comes with
+Perl:
+
+       # contributed by Gunnar Hjalmarsson
+        use Time::Local;
+        my $today = timelocal 0, 0, 12, ( localtime )[3..5];
+        my ($d, $m, $y) = ( localtime $today-86400 )[3..5];
+        printf "Yesterday: %d-%02d-%02d\n", $y+1900, $m+1, $d;
+
+In this case, you measure the day starting at noon, and subtract 24
+hours. Even if the length of the calendar day is 23 or 25 hours,
+you'll still end up on the previous calendar day, although not at
+noon. Since you don't care about the time, the one hour difference
+doesn't matter and you end up with the previous date.
+
 =head2 Does Perl have a Year 2000 problem? Is Perl Y2K compliant?
 
 Short answer: No, Perl does not have a Year 2000 problem.  Yes, Perl is
@@ -600,7 +628,9 @@ anonymous array. In this case, we call the function in list context.
 If we want to call the function in scalar context, we have to do a bit
 more work. We can really have any code we like inside the braces, so
 we simply have to end with the scalar reference, although how you do
-that is up to you, and you can use code inside the braces.
+that is up to you, and you can use code inside the braces. Note that
+the use of parens creates a list context, so we need C<scalar> to
+force the scalar context on the function:
 
        print "The time is ${\(scalar localtime)}.\n"
 
@@ -780,14 +810,33 @@ result to a scalar, producing a count of the number of matches.
        $count = () = $string =~ /-\d+/g;
 
 =head2 How do I capitalize all the words on one line?
+X<Text::Autoformat> X<capitalize> X<case, title> X<case, sentence>
 
-To make the first letter of each word upper case:
+(contributed by brian d foy)
 
-       $line =~ s/\b(\w)/\U$1/g;
+Damian Conway's L<Text::Autoformat> handles all of the thinking
+for you.
 
-This has the strange effect of turning "C<don't do it>" into "C<Don'T
-Do It>".  Sometimes you might want this.  Other times you might need a
-more thorough solution (Suggested by brian d foy):
+       use Text::Autoformat;
+       my $x = "Dr. Strangelove or: How I Learned to Stop ".
+         "Worrying and Love the Bomb";
+
+       print $x, "\n";
+       for my $style (qw( sentence title highlight )) {
+               print autoformat($x, { case => $style }), "\n";
+               }
+
+How do you want to capitalize those words?
+
+       FRED AND BARNEY'S LODGE        # all uppercase
+       Fred And Barney's Lodge        # title case
+       Fred and Barney's Lodge        # highlight case
+
+It's not as easy a problem as it looks. How many words do you think
+are in there? Wait for it... wait for it.... If you answered 5
+you're right. Perl words are groups of C<\w+>, but that's not what
+you want to capitalize. How is Perl supposed to know not to capitalize
+that C<s> after the apostrophe? You could try a regular expression:
 
        $string =~ s/ (
                                 (^\w)    #at the beginning of the line
@@ -798,34 +847,8 @@ more thorough solution (Suggested by brian d foy):
 
        $string =~ s/([\w']+)/\u\L$1/g;
 
-To make the whole line upper case:
-
-       $line = uc($line);
-
-To force each word to be lower case, with the first letter upper case:
-
-       $line =~ s/(\w+)/\u\L$1/g;
-
-You can (and probably should) enable locale awareness of those
-characters by placing a C<use locale> pragma in your program.
-See L<perllocale> for endless details on locales.
-
-This is sometimes referred to as putting something into "title
-case", but that's not quite accurate.  Consider the proper
-capitalization of the movie I<Dr. Strangelove or: How I Learned to
-Stop Worrying and Love the Bomb>, for example.
-
-Damian Conway's L<Text::Autoformat> module provides some smart
-case transformations:
-
-       use Text::Autoformat;
-       my $x = "Dr. Strangelove or: How I Learned to Stop ".
-         "Worrying and Love the Bomb";
-
-       print $x, "\n";
-       for my $style (qw( sentence title highlight )) {
-               print autoformat($x, { case => $style }), "\n";
-               }
+Now, what if you don't want to capitalize that "and"? Just use
+L<Text::Autoformat> and get on with the next problem. :)
 
 =head2 How can I split a [character] delimited string except when inside [character]?
 
@@ -958,25 +981,39 @@ Left and right padding with any character, modifying C<$text> directly:
 
 =head2 How do I extract selected columns from a string?
 
-Use C<substr()> or C<unpack()>, both documented in L<perlfunc>.
-If you prefer thinking in terms of columns instead of widths,
-you can use this kind of thing:
-
-       # determine the unpack format needed to split Linux ps output
-       # arguments are cut columns
-       my $fmt = cut2fmt(8, 14, 20, 26, 30, 34, 41, 47, 59, 63, 67, 72);
-
-       sub cut2fmt {
-               my(@positions) = @_;
-               my $template  = '';
-               my $lastpos   = 1;
-               for my $place (@positions) {
-                       $template .= "A" . ($place - $lastpos) . " ";
-                       $lastpos   = $place;
-                       }
-               $template .= "A*";
-               return $template;
-               }
+(contributed by brian d foy)
+
+If you know where the columns that contain the data, you can
+use C<substr> to extract a single column.
+
+       my $column = substr( $line, $start_column, $length );
+
+You can use C<split> if the columns are separated by whitespace or
+some other delimiter, as long as whitespace or the delimiter cannot
+appear as part of the data.
+
+       my $line    = ' fred barney   betty   ';
+       my @columns = split /\s+/, $line;
+               # ( '', 'fred', 'barney', 'betty' );
+
+       my $line    = 'fred||barney||betty';
+       my @columns = split /\|/, $line;
+               # ( 'fred', '', 'barney', '', 'betty' );
+
+If you want to work with comma-separated values, don't do this since
+that format is a bit more complicated. Use one of the modules that
+handle that format, such as C<Text::CSV>, C<Text::CSV_XS>, or
+C<Text::CSV_PP>.
+
+If you want to break apart an entire line of fixed columns, you can use
+C<unpack> with the A (ASCII) format. by using a number after the format
+specifier, you can denote the column width. See the C<pack> and C<unpack>
+entries in L<perlfunc> for more details.
+
+       my @fields = unpack( $line, "A8 A8 A8 A16 A4" );
+
+Note that spaces in the format argument to C<unpack> do not denote literal
+spaces. If you have space separated data, you may want C<split> instead.
 
 =head2 How do I find the soundex value of a string?
 
@@ -988,37 +1025,64 @@ C<Text::Metaphone>, and C<Text::DoubleMetaphone> modules.
 
 =head2 How can I expand variables in text strings?
 
-Let's assume that you have a string that contains placeholder
-variables.
+(contributed by brian d foy)
+
+If you can avoid it, don't, or if you can use a templating system,
+such as C<Text::Template> or C<Template> Toolkit, do that instead. You
+might even be able to get the job done with C<sprintf> or C<printf>:
+
+       my $string = sprintf 'Say hello to %s and %s', $foo, $bar;
 
-       $text = 'this has a $foo in it and a $bar';
+However, for the one-off simple case where I don't want to pull out a
+full templating system, I'll use a string that has two Perl scalar
+variables in it. In this example, I want to expand C<$foo> and C<$bar>
+to their variable's values:
 
-You can use a substitution with a double evaluation.  The
-first /e turns C<$1> into C<$foo>, and the second /e turns
-C<$foo> into its value.  You may want to wrap this in an
-C<eval>: if you try to get the value of an undeclared variable
-while running under C<use strict>, you get a fatal error.
+       my $foo = 'Fred';
+       my $bar = 'Barney';
+       $string = 'Say hello to $foo and $bar';
 
-       eval { $text =~ s/(\$\w+)/$1/eeg };
-       die if $@;
+One way I can do this involves the substitution operator and a double
+C</e> flag.  The first C</e> evaluates C<$1> on the replacement side and
+turns it into C<$foo>. The second /e starts with C<$foo> and replaces
+it with its value. C<$foo>, then, turns into 'Fred', and that's finally
+what's left in the string:
 
-It's probably better in the general case to treat those
-variables as entries in some special hash.  For example:
+       $string =~ s/(\$\w+)/$1/eeg; # 'Say hello to Fred and Barney'
 
-       %user_defs = (
-               foo  => 23,
-               bar  => 19,
+The C</e> will also silently ignore violations of strict, replacing
+undefined variable names with the empty string. Since I'm using the
+C</e> flag (twice even!), I have all of the same security problems I
+have with C<eval> in its string form. If there's something odd in
+C<$foo>, perhaps something like C<@{[ system "rm -rf /" ]}>, then
+I could get myself in trouble.
+
+To get around the security problem, I could also pull the values from
+a hash instead of evaluating variable names. Using a single C</e>, I
+can check the hash to ensure the value exists, and if it doesn't, I
+can replace the missing value with a marker, in this case C<???> to
+signal that I missed something:
+
+       my $string = 'This has $foo and $bar';
+
+       my %Replacements = (
+               foo  => 'Fred',
                );
-       $text =~ s/\$(\w+)/$user_defs{$1}/g;
+
+       # $string =~ s/\$(\w+)/$Replacements{$1}/g;
+       $string =~ s/\$(\w+)/
+               exists $Replacements{$1} ? $Replacements{$1} : '???'
+               /eg;
+
+       print $string;
 
 =head2 What's wrong with always quoting "$vars"?
 
 The problem is that those double-quotes force
-stringification--coercing numbers and references into
-strings--even when you don't want them to be strings.  Think
-of it this way: double-quote expansion is used to produce
-new strings.  If you already have a string, why do you need
-more?
+stringification--coercing numbers and references into strings--even
+when you don't want them to be strings.  Think of it this way:
+double-quote expansion is used to produce new strings.  If you already
+have a string, why do you need more?
 
 If you get used to writing odd things like these:
 
@@ -1231,16 +1295,32 @@ same thing.
 
 =head2 How can I tell whether a certain element is contained in a list or array?
 
-(portions of this answer contributed by Anno Siegel)
+(portions of this answer contributed by Anno Siegel and brian d foy)
 
 Hearing the word "in" is an I<in>dication that you probably should have
 used a hash, not a list or array, to store your data.  Hashes are
 designed to answer this question quickly and efficiently.  Arrays aren't.
 
-That being said, there are several ways to approach this.  If you
+That being said, there are several ways to approach this.  In Perl 5.10
+and later, you can use the smart match operator to check that an item is
+contained in an array or a hash:
+
+       use 5.010;
+
+       if( $item ~~ @array )
+               {
+               say "The array contains $item"
+               }
+
+       if( $item ~~ %hash )
+               {
+               say "The hash contains $item"
+               }
+
+With earlier versions of Perl, you have to do a bit more work. If you
 are going to make this query many times over arbitrary string values,
 the fastest way is probably to invert the original array and maintain a
-hash whose keys are the first array's values.
+hash whose keys are the first array's values:
 
        @blues = qw/azure cerulean teal turquoise lapis-lazuli/;
        %is_blue = ();
@@ -1274,7 +1354,7 @@ multiple values against the same array.
 
 If you are testing only once, the standard module C<List::Util> exports
 the function C<first> for this purpose.  It works by stopping once it
-finds the element. It's written in C for speed, and its Perl equivalant
+finds the element. It's written in C for speed, and its Perl equivalent
 looks like this subroutine:
 
        sub first (&@) {
@@ -1315,6 +1395,21 @@ in either A or in B but not in both.  Think of it as an xor operation.
 
 =head2 How do I test whether two arrays or hashes are equal?
 
+With Perl 5.10 and later, the smart match operator can give you the answer
+with the least amount of work:
+
+       use 5.010;
+
+       if( @array1 ~~ @array2 )
+               {
+               say "The arrays are the same";
+               }
+
+       if( %hash1 ~~ %hash2 ) # doesn't check values!
+               {
+               say "The hash keys are the same";
+               }
+
 The following code works for single-level arrays.  It uses a
 stringwise comparison, and does not distinguish defined versus
 undefined empty strings.  Modify if you have other needs.
@@ -1399,7 +1494,7 @@ that satisfies the condition.
 In general, you usually don't need a linked list in Perl, since with
 regular arrays, you can push and pop or shift and unshift at either
 end, or you can use splice to add and/or remove arbitrary number of
-elements at arbitrary points.  Both pop and shift are both O(1)
+elements at arbitrary points.  Both pop and shift are O(1)
 operations on Perl's dynamic arrays.  In the absence of shifts and
 pops, push in general needs to reallocate on the order every log(N)
 times, and unshift will need to copy pointers each time.
@@ -1445,14 +1540,24 @@ You could add to the list this way:
 But again, Perl's built-in are virtually always good enough.
 
 =head2 How do I handle circular lists?
+X<circular> X<array> X<Tie::Cycle> X<Array::Iterator::Circular>
+X<cycle> X<modulus>
 
-Circular lists could be handled in the traditional fashion with linked
-lists, or you could just do something like this with an array:
+(contributed by brian d foy)
 
-       unshift(@array, pop(@array));  # the last shall be first
-       push(@array, shift(@array));   # and vice versa
+If you want to cycle through an array endlessy, you can increment the
+index modulo the number of elements in the array:
 
-You can also use C<Tie::Cycle>:
+       my @array = qw( a b c );
+       my $i = 0;
+
+       while( 1 ) {
+               print $array[ $i++ % @array ], "\n";
+               last if $i > 20;
+               }
+
+You can also use C<Tie::Cycle> to use a scalar that always has the
+next element of the circular array:
 
        use Tie::Cycle;
 
@@ -1462,6 +1567,19 @@ You can also use C<Tie::Cycle>:
        print $cycle; # 000000
        print $cycle; # FFFF00
 
+The C<Array::Iterator::Circular> creates an iterator object for
+circular arrays:
+
+       use Array::Iterator::Circular;
+
+       my $color_iterator = Array::Iterator::Circular->new(
+               qw(red green blue orange)
+               );
+
+       foreach ( 1 .. 20 ) {
+               print $color_iterator->next, "\n";
+               }
+
 =head2 How do I shuffle an array randomly?
 
 If you either have Perl 5.8.0 or later installed, or if you have
@@ -1475,6 +1593,8 @@ If not, you can use a Fisher-Yates shuffle.
 
        sub fisher_yates_shuffle {
                my $deck = shift;  # $deck is a reference to an array
+               return unless @$deck; # must not be empty!
+
                my $i = @$deck;
                while (--$i) {
                        my $j = int rand ($i+1);
@@ -1554,14 +1674,18 @@ Or, simply:
        my $element = $array[ rand @array ];
 
 =head2 How do I permute N elements of a list?
+X<List::Permuter> X<permute> X<Algorithm::Loops> X<Knuth>
+X<The Art of Computer Programming> X<Fischer-Krause>
 
-Use the C<List::Permutor> module on CPAN.  If the list is actually an
+Use the C<List::Permutor> module on CPAN. If the list is actually an
 array, try the C<Algorithm::Permute> module (also on CPAN). It's
-written in XS code and is very efficient.
+written in XS code and is very efficient:
 
        use Algorithm::Permute;
+
        my @array = 'a'..'d';
        my $p_iterator = Algorithm::Permute->new ( \@array );
+
        while (my @perm = $p_iterator->next) {
           print "next permutation: (@perm)\n";
                }
@@ -1569,19 +1693,20 @@ written in XS code and is very efficient.
 For even faster execution, you could do:
 
        use Algorithm::Permute;
+
        my @array = 'a'..'d';
+
        Algorithm::Permute::permute {
                print "next permutation: (@array)\n";
                } @array;
 
-Here's a little program that generates all permutations of
-all the words on each line of input. The algorithm embodied
-in the C<permute()> function is discussed in Volume 4 (still
-unpublished) of Knuth's I<The Art of Computer Programming>
-and will work on any list:
+Here's a little program that generates all permutations of all the
+words on each line of input. The algorithm embodied in the
+C<permute()> function is discussed in Volume 4 (still unpublished) of
+Knuth's I<The Art of Computer Programming> and will work on any list:
 
        #!/usr/bin/perl -n
-       # Fischer-Kause ordered permutation generator
+       # Fischer-Krause ordered permutation generator
 
        sub permute (&@) {
                my $code = shift;
@@ -1596,7 +1721,22 @@ and will work on any list:
                }
        }
 
-       permute {print"@_\n"} split;
+       permute { print "@_\n" } split;
+
+The C<Algorithm::Loops> module also provides the C<NextPermute> and
+C<NextPermuteNum> functions which efficiently find all unique permutations
+of an array, even if it contains duplicate values, modifying it in-place:
+if its elements are in reverse-sorted order then the array is reversed,
+making it sorted, and it returns false; otherwise the next
+permutation is returned.
+
+C<NextPermute> uses string order and C<NextPermuteNum> numeric order, so
+you can enumerate all the permutations of C<0..9> like this:
+
+       use Algorithm::Loops qw(NextPermuteNum);
+
+    my @list= 0..9;
+    do { print "@list\n" } while NextPermuteNum @list;
 
 =head2 How do I sort an array by (anything)?
 
@@ -1651,14 +1791,23 @@ See also the question later in L<perlfaq4> on sorting hashes.
 Use C<pack()> and C<unpack()>, or else C<vec()> and the bitwise
 operations.
 
-For example, this sets C<$vec> to have bit N set if C<$ints[N]> was
-set:
+For example, you don't have to store individual bits in an array
+(which would mean that you're wasting a lot of space). To convert an
+array of bits to a string, use C<vec()> to set the right bits. This
+sets C<$vec> to have bit N set only if C<$ints[N]> was set:
 
+       @ints = (...); # array of bits, e.g. ( 1, 0, 0, 1, 1, 0 ... )
        $vec = '';
-       foreach(@ints) { vec($vec,$_,1) = 1 }
+       foreach( 0 .. $#ints ) {
+               vec($vec,$_,1) = 1 if $ints[$_];
+               }
+
+The string C<$vec> only takes up as many bits as it needs. For
+instance, if you had 16 entries in C<@ints>, C<$vec> only needs two
+bytes to store them (not counting the scalar variable overhead).
 
-Here's how, given a vector in C<$vec>, you can get those bits into your
-C<@ints> array:
+Here's how, given a vector in C<$vec>, you can get those bits into
+your C<@ints> array:
 
        sub bitvec_to_list {
                my $vec = shift;
@@ -1764,15 +1913,113 @@ in the 5.004 release or later of Perl for more detail.
 
 =head2 How do I process an entire hash?
 
-Use the each() function (see L<perlfunc/each>) if you don't care
-whether it's sorted:
+(contributed by brian d foy)
+
+There are a couple of ways that you can process an entire hash. You
+can get a list of keys, then go through each key, or grab a one
+key-value pair at a time.
+
+To go through all of the keys, use the C<keys> function. This extracts
+all of the keys of the hash and gives them back to you as a list. You
+can then get the value through the particular key you're processing:
+
+       foreach my $key ( keys %hash ) {
+               my $value = $hash{$key}
+               ...
+               }
+
+Once you have the list of keys, you can process that list before you
+process the hash elements. For instance, you can sort the keys so you
+can process them in lexical order:
+
+       foreach my $key ( sort keys %hash ) {
+               my $value = $hash{$key}
+               ...
+               }
+
+Or, you might want to only process some of the items. If you only want
+to deal with the keys that start with C<text:>, you can select just
+those using C<grep>:
+
+       foreach my $key ( grep /^text:/, keys %hash ) {
+               my $value = $hash{$key}
+               ...
+               }
+
+If the hash is very large, you might not want to create a long list of
+keys. To save some memory, you can grab one key-value pair at a time using
+C<each()>, which returns a pair you haven't seen yet:
+
+       while( my( $key, $value ) = each( %hash ) ) {
+               ...
+               }
+
+The C<each> operator returns the pairs in apparently random order, so if
+ordering matters to you, you'll have to stick with the C<keys> method.
+
+The C<each()> operator can be a bit tricky though. You can't add or
+delete keys of the hash while you're using it without possibly
+skipping or re-processing some pairs after Perl internally rehashes
+all of the elements. Additionally, a hash has only one iterator, so if
+you use C<keys>, C<values>, or C<each> on the same hash, you can reset
+the iterator and mess up your processing. See the C<each> entry in
+L<perlfunc> for more details.
+
+=head2 How do I merge two hashes?
+X<hash> X<merge> X<slice, hash>
+
+(contributed by brian d foy)
+
+Before you decide to merge two hashes, you have to decide what to do
+if both hashes contain keys that are the same and if you want to leave
+the original hashes as they were.
+
+If you want to preserve the original hashes, copy one hash (C<%hash1>)
+to a new hash (C<%new_hash>), then add the keys from the other hash
+(C<%hash2> to the new hash. Checking that the key already exists in
+C<%new_hash> gives you a chance to decide what to do with the
+duplicates:
+
+       my %new_hash = %hash1; # make a copy; leave %hash1 alone
 
-       while ( ($key, $value) = each %hash) {
-               print "$key = $value\n";
+       foreach my $key2 ( keys %hash2 )
+               {
+               if( exists $new_hash{$key2} )
+                       {
+                       warn "Key [$key2] is in both hashes!";
+                       # handle the duplicate (perhaps only warning)
+                       ...
+                       next;
+                       }
+               else
+                       {
+                       $new_hash{$key2} = $hash2{$key2};
+                       }
                }
 
-If you want it sorted, you'll have to use foreach() on the result of
-sorting the keys as shown in an earlier question.
+If you don't want to create a new hash, you can still use this looping
+technique; just change the C<%new_hash> to C<%hash1>.
+
+       foreach my $key2 ( keys %hash2 )
+               {
+               if( exists $hash1{$key2} )
+                       {
+                       warn "Key [$key2] is in both hashes!";
+                       # handle the duplicate (perhaps only warning)
+                       ...
+                       next;
+                       }
+               else
+                       {
+                       $hash1{$key2} = $hash2{$key2};
+                       }
+               }
+
+If you don't care that one hash overwrites keys and values from the other, you
+could just use a hash slice to add one hash to another. In this case, values
+from C<%hash2> replace values from C<%hash1> when they have keys in common:
+
+       @hash1{ keys %hash2 } = values %hash2;
 
 =head2 What happens if I add or remove keys from a hash while iterating over it?
 
@@ -1810,14 +2057,35 @@ worry you, you can always reverse the hash into a hash of arrays instead:
 
 =head2 How can I know how many entries are in a hash?
 
-If you mean how many keys, then all you have to do is
-use the keys() function in a scalar context:
+(contributed by brian d foy)
+
+This is very similar to "How do I process an entire hash?", also in
+L<perlfaq4>, but a bit simpler in the common cases.
+
+You can use the C<keys()> built-in function in scalar context to find out
+have many entries you have in a hash:
+
+       my $key_count = keys %hash; # must be scalar context!
+       
+If you want to find out how many entries have a defined value, that's
+a bit different. You have to check each value. A C<grep> is handy: 
+
+       my $defined_value_count = grep { defined } values %hash;
+
+You can use that same structure to count the entries any way that
+you like. If you want the count of the keys with vowels in them,
+you just test for that instead:
 
-    $num_keys = keys %hash;
+       my $vowel_count = grep { /[aeiou]/ } keys %hash;
+       
+The C<grep> in scalar context returns the count. If you want the list
+of matching items, just use it in list context instead:
 
-The keys() function also resets the iterator, which means that you may
+       my @defined_values = grep { defined } values %hash;
+
+The C<keys()> function also resets the iterator, which means that you may
 see strange results if you use this between uses of other hash operators
-such as each().
+such as C<each()>.
 
 =head2 How do I sort a hash (optionally by value instead of key)?
 
@@ -1833,7 +2101,7 @@ create a report which lists the keys in ASCIIbetical order.
 
        foreach my $key ( @keys )
                {
-               printf "%-20s %6d\n", $key, $hash{$value};
+               printf "%-20s %6d\n", $key, $hash{$key};
                }
 
 We could get more fancy in the C<sort()> block though. Instead of
@@ -1904,7 +2172,7 @@ And these conditions hold
        $hash{'d'}                       is false
        defined $hash{'d'}               is true
        defined $hash{'a'}               is true
-       exists $hash{'a'}                is true (Perl5 only)
+       exists $hash{'a'}                is true (Perl 5 only)
        grep ($_ eq 'a', keys %hash)     is true
 
 If you now say
@@ -1928,7 +2196,7 @@ and these conditions now hold; changes in caps:
        $hash{'d'}                       is false
        defined $hash{'d'}               is true
        defined $hash{'a'}               is FALSE
-       exists $hash{'a'}                is true (Perl5 only)
+       exists $hash{'a'}                is true (Perl 5 only)
        grep ($_ eq 'a', keys %hash)     is true
 
 Notice the last two: you have an undef value, but a defined key!
@@ -1952,7 +2220,7 @@ and these conditions now hold; changes in caps:
        $hash{'d'}                       is false
        defined $hash{'d'}               is true
        defined $hash{'a'}               is false
-       exists $hash{'a'}                is FALSE (Perl5 only)
+       exists $hash{'a'}                is FALSE (Perl 5 only)
        grep ($_ eq 'a', keys %hash)     is FALSE
 
 See, the whole entry is gone!
@@ -1967,10 +2235,16 @@ end up doing is not what they do with ordinary hashes.
 
 =head2 How do I reset an each() operation part-way through?
 
-Using C<keys %hash> in scalar context returns the number of keys in
-the hash I<and> resets the iterator associated with the hash.  You may
-need to do this if you use C<last> to exit a loop early so that when
-you re-enter it, the hash iterator has been reset.
+(contributed by brian d foy)
+
+You can use the C<keys> or C<values> functions to reset C<each>. To
+simply reset the iterator used by C<each> without doing anything else,
+use one of them in void context:
+
+       keys %hash; # resets iterator, nothing else.
+       values %hash; # resets iterator, nothing else.
+
+See the documentation for C<each> in L<perlfunc>.
 
 =head2 How can I get the unique keys from two hashes?
 
@@ -2021,20 +2295,44 @@ Use the C<Tie::IxHash> from CPAN.
 
 =head2 Why does passing a subroutine an undefined element in a hash create it?
 
-If you say something like:
+(contributed by brian d foy)
+
+Are you using a really old version of Perl?
+
+Normally, accessing a hash key's value for a nonexistent key will
+I<not> create the key.
 
-       somefunc($hash{"nonesuch key here"});
+       my %hash  = ();
+       my $value = $hash{ 'foo' };
+       print "This won't print\n" if exists $hash{ 'foo' };
 
-Then that element "autovivifies"; that is, it springs into existence
-whether you store something there or not.  That's because functions
-get scalars passed in by reference.  If somefunc() modifies C<$_[0]>,
-it has to be ready to write it back into the caller's version.
+Passing C<$hash{ 'foo' }> to a subroutine used to be a special case, though.
+Since you could assign directly to C<$_[0]>, Perl had to be ready to
+make that assignment so it created the hash key ahead of time:
 
-This has been fixed as of Perl5.004.
+    my_sub( $hash{ 'foo' } );
+       print "This will print before 5.004\n" if exists $hash{ 'foo' };
+
+       sub my_sub {
+               # $_[0] = 'bar'; # create hash key in case you do this
+               1;
+               }
 
-Normally, merely accessing a key's value for a nonexistent key does
-I<not> cause that key to be forever there.  This is different than
-awk's behavior.
+Since Perl 5.004, however, this situation is a special case and Perl
+creates the hash key only when you make the assignment:
+
+    my_sub( $hash{ 'foo' } );
+       print "This will print, even after 5.004\n" if exists $hash{ 'foo' };
+
+       sub my_sub {
+               $_[0] = 'bar';
+               }
+
+However, if you want the old behavior (and think carefully about that
+because it's a weird side effect), you can pass a hash slice instead.
+Perl 5.004 didn't make this a special case:
+
+       my_sub( @hash{ qw/foo/ } );
 
 =head2 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?
 
@@ -2056,25 +2354,38 @@ in L<perltoot>.
 
 =head2 How can I use a reference as a hash key?
 
-(contributed by brian d foy)
+(contributed by brian d foy and Ben Morrow)
 
 Hash keys are strings, so you can't really use a reference as the key.
 When you try to do that, perl turns the reference into its stringified
 form (for instance, C<HASH(0xDEADBEEF)>). From there you can't get
 back the reference from the stringified form, at least without doing
-some extra work on your own. Also remember that hash keys must be
-unique, but two different variables can store the same reference (and
-those variables can change later).
-
-The C<Tie::RefHash> module, which is distributed with perl, might be
-what you want. It handles that extra work.
+some extra work on your own.
+
+Remember that the entry in the hash will still be there even if
+the referenced variable  goes out of scope, and that it is entirely
+possible for Perl to subsequently allocate a different variable at
+the same address. This will mean a new variable might accidentally
+be associated with the value for an old.
+
+If you have Perl 5.10 or later, and you just want to store a value
+against the reference for lookup later, you can use the core
+Hash::Util::Fieldhash module. This will also handle renaming the
+keys if you use multiple threads (which causes all variables to be
+reallocated at new addresses, changing their stringification), and
+garbage-collecting the entries when the referenced variable goes out
+of scope.
+
+If you actually need to be able to get a real reference back from
+each hash entry, you can use the Tie::RefHash module, which does the
+required work for you.
 
 =head1 Data: Misc
 
 =head2 How do I handle binary data correctly?
 
 Perl is binary clean, so it can handle binary data just fine.
-On Windows or DOS, however, you have to use C<binmode> for binary 
+On Windows or DOS, however, you have to use C<binmode> for binary
 files to avoid conversions for line endings. In general, you should
 use C<binmode> any time you want to work with binary data.
 
@@ -2170,7 +2481,14 @@ you wanted to copy.
 
 =head2 How do I define methods for every class/object?
 
-Use the C<UNIVERSAL> class (see L<UNIVERSAL>).
+(contributed by Ben Morrow)
+
+You can use the C<UNIVERSAL> class (see L<UNIVERSAL>). However, please
+be very careful to consider the consequences of doing this: adding
+methods to every object is very likely to have unintended
+consequences. If possible, it would be better to have all your object
+inherit from some common base class, or to use an object system like
+Moose that supports roles.
 
 =head2 How do I verify a credit card checksum?
 
@@ -2178,21 +2496,23 @@ Get the C<Business::CreditCard> module from CPAN.
 
 =head2 How do I pack arrays of doubles or floats for XS code?
 
-The kgbpack.c code in the C<PGPLOT> module on CPAN does just this.
+The arrays.h/arrays.c code in the C<PGPLOT> module on CPAN does just this.
 If you're doing a lot of float or double processing, consider using
 the C<PDL> module from CPAN instead--it makes number-crunching easy.
 
+See L<http://search.cpan.org/dist/PGPLOT> for the code.
+
 =head1 REVISION
 
-Revision: $Revision: 6816 $
+Revision: $Revision$
 
-Date: $Date: 2006-08-20 21:20:03 +0200 (dim, 20 aoĆ» 2006) $
+Date: $Date$
 
 See L<perlfaq> for source control details and availability.
 
 =head1 AUTHOR AND COPYRIGHT
 
-Copyright (c) 1997-2006 Tom Christiansen, Nathan Torkington, and
+Copyright (c) 1997-2009 Tom Christiansen, Nathan Torkington, and
 other authors as noted. All rights reserved.
 
 This documentation is free; you can redistribute it and/or modify it