From 793f5136aceac628d0f8aee41ccb49204963e443 Mon Sep 17 00:00:00 2001 From: Rafael Garcia-Suarez Date: Tue, 2 Dec 2003 22:18:05 +0000 Subject: [PATCH] FAQ sync. p4raw-id: //depot/perl@21835 --- pod/perlfaq1.pod | 14 +++++++++----- pod/perlfaq2.pod | 9 ++++----- pod/perlfaq3.pod | 4 ++-- pod/perlfaq4.pod | 36 +++++++++++++++++------------------- pod/perlfaq5.pod | 35 +++++++++++++++++++---------------- 5 files changed, 51 insertions(+), 47 deletions(-) diff --git a/pod/perlfaq1.pod b/pod/perlfaq1.pod index 13f8f42..61fd1d1 100644 --- a/pod/perlfaq1.pod +++ b/pod/perlfaq1.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq1 - General Questions About Perl ($Revision: 1.12 $, $Date: 2003/07/09 15:47:28 $) +perlfaq1 - General Questions About Perl ($Revision: 1.14 $, $Date: 2003/11/23 08:02:29 $) =head1 DESCRIPTION @@ -60,7 +60,7 @@ You should definitely use version 5. Version 4 is old, limited, and no longer maintained; its last patch (4.036) was in 1992, long ago and far away. Sure, it's stable, but so is anything that's dead; in fact, perl4 had been called a dead, flea-bitten camel carcass. The most -recent production release is 5.8.1 (although 5.005_03 and 5.6.1 are +recent production release is 5.8.2 (although 5.005_03 and 5.6.2 are still supported). The most cutting-edge development release is 5.9. Further references to the Perl language in this document refer to the production release unless otherwise specified. There may be one or @@ -102,6 +102,8 @@ will be used for Ponie, and there will be no language level differences between perl5 and ponie. Ponie is not a complete rewrite of perl5. +For more details, see http://www.poniecode.org/ + =head2 What is perl6? At The Second O'Reilly Open Source Software Convention, Larry Wall @@ -307,9 +309,11 @@ for any given task. Also mention that the difference between version (Well, OK, maybe it's not quite that distinct, but you get the idea.) If you want support and a reasonable guarantee that what you're developing will continue to work in the future, then you have to run -the supported version. As of October 2003 that means running either -5.8.1 (released in September 2003), or one of the older releases like -5.6.1 (released in April 2001) or 5.005_03 (released in March 1999), +the supported version. As of December 2003 that means running either +5.8.2 (released in November 2003), or one of the older releases like +5.6.2 (also released in November 2003; a maintenance release to let perl +5.6 compile on newer systems as 5.6.1 was released in April 2001) or +5.005_03 (released in March 1999), although 5.004_05 isn't that bad if you B need such an old version (released in April 1999) for stability reasons. Anything older than 5.004_05 shouldn't be used. diff --git a/pod/perlfaq2.pod b/pod/perlfaq2.pod index 8649ca8..ee097e2 100644 --- a/pod/perlfaq2.pod +++ b/pod/perlfaq2.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq2 - Obtaining and Learning about Perl ($Revision: 1.20 $, $Date: 2003/01/26 17:50:56 $) +perlfaq2 - Obtaining and Learning about Perl ($Revision: 1.25 $, $Date: 2003/10/16 04:57:38 $) =head1 DESCRIPTION @@ -407,10 +407,9 @@ I contains tutorials, demonstrations, case studies, announcements, contests, and much more. I has columns on web development, databases, Win32 Perl, graphical programming, regular expressions, and networking, and sponsors the Obfuscated Perl Contest -and the Perl Poetry Contests. As of mid-2001, the dead tree version -of TPJ will be published as a quarterly supplement of SysAdmin -magazine ( http://www.sysadminmag.com/ ) For more details on TPJ, -see http://www.tpj.com/ +and the Perl Poetry Contests. Beginning in November 2002, TPJ moved to a +reader-supported monthly e-zine format in which subscribers can download +issues as PDF documents. For more details on TPJ, see http://www.tpj.com/ Beyond this, magazines that frequently carry quality articles on Perl are I ( http://www.theperlreview.com ), diff --git a/pod/perlfaq3.pod b/pod/perlfaq3.pod index 8fd484f..2fdc2fc 100644 --- a/pod/perlfaq3.pod +++ b/pod/perlfaq3.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq3 - Programming Tools ($Revision: 1.35 $, $Date: 2003/08/24 05:26:59 $) +perlfaq3 - Programming Tools ($Revision: 1.37 $, $Date: 2003/11/24 19:55:50 $) =head1 DESCRIPTION @@ -65,7 +65,7 @@ You can use the ExtUtils::Installed module to show all installed distributions, although it can take awhile to do its magic. The standard library which comes with Perl just shows up as "Perl" (although you can get those with -Mod::CoreList). +Module::CoreList). use ExtUtils::Installed; diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index 61503b6..6a882c5 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq4 - Data Manipulation ($Revision: 1.52 $, $Date: 2003/10/02 04:44:33 $) +perlfaq4 - Data Manipulation ($Revision: 1.54 $, $Date: 2003/11/30 00:50:08 $) =head1 DESCRIPTION @@ -362,9 +362,20 @@ pseudorandom generator than comes with your operating system, look at =head2 How do I get a random number between X and Y? -Use the following simple function. It selects a random integer between -(and possibly including!) the two given integers, e.g., -C +C 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 and I. + +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 ); + +Hence you derive the following simple function to abstract +that. It selects a random integer between the two given +integers (inclusive), For example: C. sub random_int_in ($$) { my($min, $max) = @_; @@ -415,14 +426,6 @@ Use the following simple functions: return 1+int((((localtime(shift || time))[5] + 1899))/1000); } -You can also use the POSIX strftime() function which may be a bit -slower but is easier to read and maintain. - - use POSIX qw/strftime/; - - my $week_of_the_year = strftime "%W", localtime; - my $day_of_the_year = strftime "%j", localtime; - On some systems, the POSIX module's strftime() function has been extended in a non-standard way to use a C<%C> format, which they sometimes claim is the "century". It isn't, @@ -1489,16 +1492,11 @@ the hash is to be modified. Use the rand() function (see L): - # at the top of the program: - srand; # not needed for 5.004 and later - - # then later on $index = rand @array; $element = $array[$index]; -Make sure you I. -If you are calling it more than once (such as before each -call to rand), you're almost certainly doing something wrong. +Or, simply: + my $element = $array[ rand @array ]; =head2 How do I permute N elements of a list? diff --git a/pod/perlfaq5.pod b/pod/perlfaq5.pod index cad896d..be10390 100644 --- a/pod/perlfaq5.pod +++ b/pod/perlfaq5.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq5 - Files and Formats ($Revision: 1.28 $, $Date: 2003/01/26 17:45:46 $) +perlfaq5 - Files and Formats ($Revision: 1.30 $, $Date: 2003/11/23 08:07:46 $) =head1 DESCRIPTION @@ -153,8 +153,10 @@ temporary files in one process, use a counter: =head2 How can I manipulate fixed-record-length files? -The most efficient way is using pack() and unpack(). This is faster than -using substr() when taking many, many strings. It is slower for just a few. +The most efficient way is using L and +L. This is faster than using +L when taking many, many strings. It is +slower for just a few. Here is a sample chunk of code to break up and put back together again some fixed-format input lines, in this case from the output of a normal, @@ -162,22 +164,23 @@ Berkeley-style ps: # sample input line: # 15158 p5 T 0:00 perl /home/tchrist/scripts/now-what - $PS_T = 'A6 A4 A7 A5 A*'; - open(PS, "ps|"); - print scalar ; - while () { - ($pid, $tt, $stat, $time, $command) = unpack($PS_T, $_); - for $var (qw!pid tt stat time command!) { - print "$var: <$$var>\n"; + my $PS_T = 'A6 A4 A7 A5 A*'; + open my $ps, '-|', 'ps'; + print scalar <$ps>; + my @fields = qw( pid tt stat time command ); + while (<$ps>) { + my %process; + @process{@fields} = unpack($PS_T, $_); + for my $field ( @fields ) { + print "$field: <$process{$field}>\n"; } - print 'line=', pack($PS_T, $pid, $tt, $stat, $time, $command), - "\n"; + print 'line=', pack($PS_T, @process{@fields} ), "\n"; } -We've used C<$$var> in a way that forbidden by C. -That is, we've promoted a string to a scalar variable reference using -symbolic references. This is okay in small programs, but doesn't scale -well. It also only works on global variables, not lexicals. +We've used a hash slice in order to easily handle the fields of each row. +Storing the keys in an array means it's easy to operate on them as a +group or loop over them with for. It also avoids polluting the program +with global variables and using symbolic references. =head2 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? -- 2.7.4