=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
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
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
(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<absolutely> need such an old
version (released in April 1999) for stability reasons.
Anything older than 5.004_05 shouldn't be used.
=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
announcements, contests, and much more. I<TPJ> 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<The Perl Review> ( http://www.theperlreview.com ),
=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
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;
=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
=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<random_int_in(50,120)>
+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.
+
+ 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<random_int_in(50,120)>.
sub random_int_in ($$) {
my($min, $max) = @_;
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,
Use the rand() function (see L<perlfunc/rand>):
- # 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<only call srand once per program, if then>.
-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?
=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
=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<pack()|perlfunc/"pack"> and
+L<unpack()|perlfunc/"unpack">. This is faster than using
+L<substr()|perlfunc/"substr"> 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,
# 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 <PS>;
- while (<PS>) {
- ($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<use strict 'refs'>.
-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?