cvs commit: perlfaq perlfaq2.pod perlfaq3.pod perlfaq4.pod perlfaq7.pod perlfaq9.pod

[email protected] (brian d foy) 13 Oct 2005 19:43:14 -0000
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
cvsuser     05/10/13 12:43:13

  Modified:    .        perlfaq2.pod perlfaq3.pod perlfaq4.pod perlfaq7.pod
                        perlfaq9.pod
  Log:
  * perlfaq2: URL updates and winnowing out ancient books from the list
  
  * perlfaq3: URL updates and formatting fixes
  
  * perlfaq4:
  	* How can I tell whether a certain element is contained in a list or
  	array?
  	   + Anno updated this answer with pointers to List::Util
  
  	* How can I use a reference as a hash key?
  	   + I updated the answer to explain why you shouldn't.
  	   + If people have other favorite modules to handle this sort
  	   of thing, let me know so I can add them to the answer.
  
  * perlfaq7:
  	* I updated the answer to note that we've fixed this bug. I kept
  	the old answer around although I added example output to clarify
  	the symptoms.
  
  	* This might be a candidate for removal.
  
  *perlfaq9:
  	* I fixed a syntax error involving a missing close quote
  
  	* I changed the double quotes to single quotes since I'm not
  	interpolating anything.
  
  Revision  Changes    Path
  1.35      +16 -46    perlfaq/perlfaq2.pod
  
  Index: perlfaq2.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq2.pod,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- perlfaq2.pod	10 Aug 2005 15:56:54 -0000	1.34
  +++ perlfaq2.pod	13 Oct 2005 19:43:13 -0000	1.35
  @@ -149,9 +149,8 @@
   have a proper man command, or it's been misinstalled.  If that doesn't
   work, try looking in /usr/local/lib/perl5/pod for documentation.
   
  -If all else fails, consult http://perldoc.cpan.org/ or
  -http://www.perldoc.com/ both offer the complete documentation
  -in html format.
  +If all else fails, consult http://perldoc.perl.org/ which has the
  +complete documentation in HTML and PDF format.
   
   Many good books have been written about Perl--see the section below
   for more details.
  @@ -162,11 +161,9 @@
   L<perlreftut> for managing references, L<perlretut> for regular
   expressions, L<perlthrtut> for threads, L<perldebtut> for debugging,
   and L<perlxstut> for linking C and Perl together.  There may be more
  -by the time you read this.  The following URLs might also be of
  -assistance:
  +by the time you read this.  These URLs might also be useful:
   
  -    http://perldoc.cpan.org/
  -    http://www.perldoc.com/
  +    http://perldoc.perl.org/
       http://bookmarks.cpan.org/search.cgi?cat=Training%2FTutorials
   
   =head2 What are the Perl newsgroups on Usenet?  Where do I post questions?
  @@ -221,10 +218,11 @@
   A number of books on Perl and/or CGI programming are available.  A few
   of these are good, some are OK, but many aren't worth your money.
   There is a list of these books, some with extensive reviews, at
  -http://books.perl.org/ .
  +http://books.perl.org/ . If you don't see your book listed here, you
  +can write to [email protected] .
   
   The incontestably definitive reference book on Perl, written by
  -the creator of Perl, is now (Sept 2004) in its third edition:
  +the creator of Perl, is Programming Perl:
   
   	Programming Perl (the "Camel Book"):
   	by Larry Wall, Tom Christiansen, and Jon Orwant
  @@ -242,13 +240,13 @@
   	http://www.oreilly.com/catalog/perlckbk2/
   
   If you're already a seasoned programmer, then the Camel Book might
  -suffice for you to learn Perl from.  If you're not, check out the
  +suffice for you to learn Perl.  If you're not, check out the
   Llama book:
   
  -	Learning Perl (the "Llama Book")
  -	by Randal L. Schwartz and Tom Phoenix
  -	ISBN 0-596-00132-0 [3rd edition July 2001]
  -	http://www.oreilly.com/catalog/lperl3/
  +	Learning Perl
  +	by Randal L. Schwartz, Tom Phoenix, and brian d foy
  +	ISBN 0-596-10105-8 [4th edition July 2005]
  +	http://www.oreilly.com/catalog/learnperl4/
   
   And for more advanced information on writing larger programs,
   presented in the same style as the Llama book, continue your education
  @@ -306,11 +304,6 @@
   	ISBN 0-596-00032-4 [3rd edition May 2000]
   	http://www.oreilly.com/catalog/perlpr3/
   
  -	Perl in a Nutshell
  -	by Ellen Siever, Stephan Spainhour, and Nathan Patwardhan
  -	ISBN 1-56592-286-7 [1st edition December 1998]
  -	http://www.oreilly.com/catalog/perlnut/
  -
   =item Tutorials
   
   	Beginning Perl
  @@ -324,38 +317,15 @@
   	http://www.manning.com/Johnson/
   
   	Learning Perl
  -	by Randal L. Schwartz and Tom Phoenix
  -	ISBN 0-596-00132-0 [3rd edition July 2001]
  -	http://www.oreilly.com/catalog/lperl3/
  +	by Randal L. Schwartz, Tom Phoenix, and brian d foy
  +	ISBN 0-596-10105-8 [4th edition July 2005]
  +	http://www.oreilly.com/catalog/learnperl4/
   
   	Learning Perl Objects, References, and Modules
   	by Randal L. Schwartz, with Tom Phoenix (foreword by Damian Conway)
   	ISBN 0-596-00478-8 [1st edition June 2003]
   	http://www.oreilly.com/catalog/lrnperlorm/
   
  -	Learning Perl on Win32 Systems
  -	by Randal L. Schwartz, Erik Olson, and Tom Christiansen,
  -	    with foreword by Larry Wall
  -	ISBN 1-56592-324-3 [1st edition August 1997]
  -	http://www.oreilly.com/catalog/lperlwin/
  -
  -	Perl: The Programmer's Companion
  -	by Nigel Chapman
  -	ISBN 0-471-97563-X [1997, 3rd printing Spring 1998]
  -	http://www.wiley.com/compbooks/catalog/97563-X.htm
  -	http://www.wiley.com/compbooks/chapman/perl/perltpc.html (errata etc)
  -
  -	Cross-Platform Perl
  -	by Eric Foster-Johnson
  -	ISBN 1-55851-483-X [2nd edition September 2000]
  -	http://www.pconline.com/~erc/perlbook.htm
  -
  -	MacPerl: Power and Ease
  -	by Vicki Brown and Chris Nandor,
  -	    with foreword by Matthias Neeracher
  -	ISBN 1-881957-32-2 [1st edition May 1998]
  -	http://www.macperl.com/ptf_book/
  -
   =item Task-Oriented
   
   	Writing Perl Modules for CPAN
  
  
  
  1.52      +23 -28    perlfaq/perlfaq3.pod
  
  Index: perlfaq3.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq3.pod,v
  retrieving revision 1.51
  retrieving revision 1.52
  diff -u -r1.51 -r1.52
  --- perlfaq3.pod	10 Aug 2005 15:56:39 -0000	1.51
  +++ perlfaq3.pod	13 Oct 2005 19:43:13 -0000	1.52
  @@ -47,7 +47,7 @@
   power of Perl. The goal is a full featured shell that behaves as
   expected for normal shell activity and uses Perl syntax and
   functionality for control-flow statements and other things.
  -You can get psh at http://www.focusresearch.com/gregor/psh/ .
  +You can get psh at http://sourceforge.net/projects/psh/ .
   
   Zoidberg is a similar project and provides a shell written in perl,
   configured in perl and operated in perl. It is intended as a login shell
  @@ -772,30 +772,26 @@
   solutions simply bundle a Perl interpreter in the final product
   (but see L<How can I make my Perl program run faster?>).
   
  -The Perl Archive Toolkit (http://par.perl.org/index.cgi) is
  -Perl's analog to Java's JAR.  It's freely available and on
  -CPAN (http://search.cpan.org/dist/PAR/).
  -
  -The B::* namespace, often called "the Perl compiler", but is really a
  -way for Perl programs to peek at its innards rather than create
  -pre-compiled versions of your program.  However. the B::Bytecode
  -module can turn your script  into a bytecode format that could be
  -loaded later by the ByteLoader module and executed as a regular Perl
  -script.
  -
  -There are also some commercial products that may work for
  -you, although you have to buy a license for them.
  -
  -The Perl Dev Kit
  -(http://www.activestate.com/Products/Perl_Dev_Kit/) from
  -ActiveState can "Turn your Perl programs into ready-to-run
  -executables for HP-UX, Linux, Solaris and Windows."
  +The Perl Archive Toolkit ( http://par.perl.org/index.cgi ) is Perl's
  +analog to Java's JAR.  It's freely available and on CPAN (
  +http://search.cpan.org/dist/PAR/ ).
  +
  +The B::* namespace, often called "the Perl compiler", but is really a way
  +for Perl programs to peek at its innards rather than create pre-compiled
  +versions of your program.  However. the B::Bytecode module can turn your
  +script  into a bytecode format that could be loaded later by the
  +ByteLoader module and executed as a regular Perl script.
  +
  +There are also some commercial products that may work for you, although
  +you have to buy a license for them.
   
  -Perl2Exe (http://www.indigostar.com/perl2exe.htm) is a
  -command line program for converting perl scripts to
  -executable files.  It targets both Windows and unix
  -platforms.
  +The Perl Dev Kit ( http://www.activestate.com/Products/Perl_Dev_Kit/ )
  +from ActiveState can "Turn your Perl programs into ready-to-run
  +executables for HP-UX, Linux, Solaris and Windows."
   
  +Perl2Exe ( http://www.indigostar.com/perl2exe.htm ) is a command line
  +program for converting perl scripts to executable files.  It targets both
  +Windows and unix platforms.
   
   =head2 How can I compile Perl into Java?
   
  @@ -923,12 +919,11 @@
   
   A good place to start is L<perltoot>, and you can use L<perlobj>,
   L<perlboot>, L<perltoot>, L<perltooc>, and L<perlbot> for reference.
  -(If you are using really old Perl, you may not have all of these,
  -try http://www.perldoc.com/ , but consider upgrading your perl.)
   
   A good book on OO on Perl is the "Object-Oriented Perl"
  -by Damian Conway from Manning Publications,
  -http://www.manning.com/Conway/index.html
  +by Damian Conway from Manning Publications, or "Learning Perl
  +References, Objects, & Modules" by Randal Schwartz and Tom
  +Phoenix from O'Reilly Media.
   
   =head2 Where can I learn about linking C with Perl?
   
  
  
  
  1.68      +39 -20    perlfaq/perlfaq4.pod
  
  Index: perlfaq4.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
  retrieving revision 1.67
  retrieving revision 1.68
  diff -u -r1.67 -r1.68
  --- perlfaq4.pod	10 Aug 2005 15:55:49 -0000	1.67
  +++ perlfaq4.pod	13 Oct 2005 19:43:13 -0000	1.68
  @@ -1212,6 +1212,8 @@
   
   =head2 How can I tell whether a certain element is contained in a list or array?
   
  +(portions of this answer contributed by Annoo Siegel)
  +
   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.
  @@ -1247,28 +1249,35 @@
   
   Now check whether C<vec($read,$n,1)> is true for some C<$n>.
   
  -Please do not use
  -
  -    ($is_there) = grep $_ eq $whatever, @array;
  +These methods guarantee fast individual tests but require a re-organization
  +of the original list or array.  They only pay off if you have to test
  +multiple values against the same array.
  +
  +If you are testing only once, the standard module 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
  +looks like this subroutine:
   
  -or worse yet
  +	sub first (&@) {
  +		my $code = shift;
  +		foreach (@_) {
  +			return $_ if &{$code}();
  +		}
  +		undef;
  +	}
   
  -    ($is_there) = grep /$whatever/, @array;
  +If speed is of little concern, the common idiom use grep in scalar context
  +(which returns the number of items that passed its condition) to traverse the
  +entire list. This does have the benefit of telling you how many matches it
  +found, though.
   
  -These are slow (checks every element even if the first matches),
  -inefficient (same reason), and potentially buggy (what if there are
  -regex characters in $whatever?).  If you're only testing once, then
  -use:
  +	my $is_there = grep $_ eq $whatever, @array;
   
  -    $is_there = 0;
  -    foreach $elt (@array) {
  -	if ($elt eq $elt_to_find) {
  -	    $is_there = 1;
  -	    last;
  -	}
  -    }
  -    if ($is_there) { ... }
  +If you want to actually extract the matching elements, simply use grep in
  +list context.
   
  +	my @matches = grep $_ eq $whatever, @array;
  +	
   =head2 How do I compute the difference of two arrays?  How do I compute the intersection of two arrays?
   
   Use a hash.  Here's code to do both and more.  It assumes that
  @@ -1982,8 +1991,18 @@
   
   =head2 How can I use a reference as a hash key?
   
  -You can't do this directly, but you could use the standard Tie::RefHash
  -module distributed with Perl.
  +(contributed by brian d foy)
  +
  +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 Tie::RefHash module, which is distributed with perl, might be what
  +you want. It handles that extra work.
   
   =head1 Data: Misc
   
  
  
  
  1.26      +28 -12    perlfaq/perlfaq7.pod
  
  Index: perlfaq7.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq7.pod,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- perlfaq7.pod	8 Aug 2005 02:38:25 -0000	1.25
  +++ perlfaq7.pod	13 Oct 2005 19:43:13 -0000	1.26
  @@ -271,24 +271,40 @@
   
   =head2 What is variable suicide and how can I prevent it?
   
  -Variable suicide is when you (temporarily or permanently) lose the
  -value of a variable.  It is caused by scoping through my() and local()
  -interacting with either closures or aliased foreach() iterator
  -variables and subroutine arguments.  It used to be easy to
  -inadvertently lose a variable's value this way, but now it's much
  -harder.  Take this code:
  +This problem was fixed in perl 5.004_05, so preventing it means upgrading
  +your version of perl. ;)
   
  -    my $f = "foo";
  +Variable suicide is when you (temporarily or permanently) lose the value
  +of a variable.  It is caused by scoping through my() and local()
  +interacting with either closures or aliased foreach() iterator variables
  +and subroutine arguments.  It used to be easy to inadvertently lose a
  +variable's value this way, but now it's much harder.  Take this code:
  +
  +    my $f = 'foo';
       sub T {
  -      while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }
  +      while ($i++ < 3) { my $f = $f; $f .= $i; print $f, "\n" }
       }
       T;
       print "Finally $f\n";
   
  +If you are experiencing variable suicide, that C<my $f> in the subroutine
  +doesn't pick up a fresh copy of the C<$f> whose value is <foo>. The output
  +shows that inside the subroutine the value of C<$f> leaks through when it
  +shouldn't, as in this output:
  +
  +	foobar
  +	foobarbar
  +	foobarbarbar
  +	Finally foo
  +
   The $f that has "bar" added to it three times should be a new C<$f>
  -(C<my $f> should create a new local variable each time through the loop).
  -It isn't, however.  This was a bug, now fixed in the latest releases
  -(tested against 5.004_05, 5.005_03, and 5.005_56).
  +C<my $f> should create a new lexical variable each time through the loop.
  +The expected output is:
  +
  +	foobar
  +	foobar
  +	foobar
  +	Finally foo
   
   =head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?
   
  
  
  
  1.24      +5 -5      perlfaq/perlfaq9.pod
  
  Index: perlfaq9.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq9.pod,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- perlfaq9.pod	10 Aug 2005 15:54:54 -0000	1.23
  +++ perlfaq9.pod	13 Oct 2005 19:43:13 -0000	1.24
  @@ -366,9 +366,9 @@
   
   	use CGI qw(:standard);
   
  -	my $total = param( "price" ) + param( "shipping" );
  +	my $total = param( 'price' ) + param( 'shipping' );
   
  -	my @items = param( "item ); # multiple values, same field name
  +	my @items = param( 'item' ); # multiple values, same field name
   
   If you want an object-oriented approach, CGI.pm can do that too.
   
  @@ -376,9 +376,9 @@
   
   	my $cgi = CGI->new();
   
  -	my $total = $cgi->param( "price" ) + $cgi->param( "shipping" );
  +	my $total = $cgi->param( 'price' ) + $cgi->param( 'shipping' );
   
  -	my @items = $cgi->param( "item" );
  +	my @items = $cgi->param( 'item' );
   
   You might also try CGI::Minimal which is a lightweight version
   of the same thing.  Other CGI::* modules on CPAN might work better