are displayed in either the format you specify with printf(), or the
current output format for numbers (see L<perlvar/"$#"> if you use
print. C<$#> has a different default value in Perl5 than it did in
-Perl4. Changing C<$#> yourself is deprecated.
+Perl4. Changing C<$#> yourself is deprecated.)
This affects B<all> computer languages that represent decimal
floating-point numbers in binary, not just Perl. Perl provides
printf("%.3f", 3.1415926535); # prints 3.142
-The POSIX module (part of the standard perl distribution) implements
+The POSIX module (part of the standard Perl distribution) implements
ceil(), floor(), and a number of other mathematical and trigonometric
functions.
$floor = floor(3.5); # 3
In 5.000 to 5.003 Perls, trigonometry was done in the Math::Complex
-module. With 5.004, the Math::Trig module (part of the standard perl
+module. With 5.004, the Math::Trig module (part of the standard Perl
distribution) implements the trigonometric functions. Internally it
uses the Math::Complex module and some functions can break out from
the real axis into the complex plane, for example the inverse sine of
To turn a string of 1s and 0s like C<10110110> into a scalar containing
its binary value, use the pack() and unpack() functions (documented in
-L<perlfunc/"pack" L<perlfunc/"unpack">):
+L<perlfunc/"pack"> and L<perlfunc/"unpack">):
$decimal = unpack('c', pack('B8', '10110110'));
This packs the string C<10110110> into an eight bit binary structure.
-This is then unpack as a character, which returns its ordinal value.
+This is then unpacked as a character, which returns its ordinal value.
This does the same thing:
push(@results, some_func($i));
}
+This situation has been fixed in Perl5.005. Use of C<..> in a C<for>
+loop will iterate over the range, without creating the entire range.
+
+ for my $i (5 .. 500_005) {
+ push(@results, some_func($i));
+ }
+
+will not create a list of 500,000 integers.
+
=head2 How can I output Roman numerals?
Get the http://www.perl.com/CPAN/modules/by-module/Roman module.
=head2 How can I find the Julian Day?
-You could use Date::Calc's Delta_Days function and calculate the number
-of days from there. Assuming that's what you really want, that is.
+Use the Time::JulianDay module (part of the Time-modules bundle
+available from CPAN.)
Before you immerse yourself too deeply in this, be sure to verify that it
is the I<Julian> Day you really want. Are they really just interested in
the first meaning that you really want, then check out the Date::Manip
and Date::Calc modules. (Thanks to David Cassell for most of this text.)
-There is also an example of Julian date calculation that should help you in
-http://www.perl.com/CPAN/authors/David_Muir_Sharnoff/modules/Time/JulianDay.pm.gz
-
=head2 How do I find yesterday's date?
The C<time()> function returns the current time in seconds since the
#
# The explicit settings of $ndst and $tdst are necessary because localtime
# only says it returns the system tm struct, and the system tm struct at
- # least on Solaris doesn't guarantee any particuliar positive value (like,
+ # least on Solaris doesn't guarantee any particular positive value (like,
# say, 1) for isdst, just a positive value. And that value can
# potentially be negative, if DST information isn't available (this sub
# just treats those cases like no DST).
# Copyright relinquished 1999 by Russ Allbery <rra@stanford.edu>
# This code is in the public domain
-=head2 Does Perl have a year 2000 problem? Is Perl Y2K compliant?
+=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
Y2K compliant (whatever that means). The programmers you've hired to
Can you use your pencil to write a non-Y2K-compliant memo? Of course
you can. Is that the pencil's fault? Of course it isn't.
-The date and time functions supplied with perl (gmtime and localtime)
+The date and time functions supplied with Perl (gmtime and localtime)
supply adequate information to determine the year well beyond 2000
(2038 is when trouble strikes for 32-bit machines). The year returned
by these functions when used in an array context is the year minus 1900.
parser.
If you are serious about writing a parser, there are a number of
-modules or oddities that will make your life a lot easier. There is
-the CPAN module Parse::RecDescent, the standard module Text::Balanced,
-the byacc program, the CPAN module Parse::Yapp, and Mark-Jason
-Dominus's excellent I<py> tool at http://www.plover.com/~mjd/perl/py/
-.
+modules or oddities that will make your life a lot easier. There are
+the CPAN modules Parse::RecDescent, Parse::Yapp, and Text::Balanced;
+and the byacc program.
One simple destructive, inside-out approach that you might try is to
pull out the smallest nesting parts one at a time:
# $_ contains the string to parse
# BEGIN and END are the opening and closing markers for the
# nested text.
-
+
@( = ('(','');
@) = (')','');
($re=$_)=~s/((BEGIN)|(END)|.)/$)[!$3]\Q$1\E$([!$2]/gs;
1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
-Or you can just use the Text::Tabs module (part of the standard perl
+Or you can just use the Text::Tabs module (part of the standard Perl
distribution).
use Text::Tabs;
=head2 How do I reformat a paragraph?
-Use Text::Wrap (part of the standard perl distribution):
+Use Text::Wrap (part of the standard Perl distribution):
use Text::Wrap;
print wrap("\t", ' ', @paragraphs);
$line =~ s/\b(\w)/\U$1/g;
This has the strange effect of turning "C<don't do it>" into "C<Don'T
-Do It>". Sometimes you might want this, instead (Suggested by Brian
-Foy):
+Do It>". Sometimes you might want this, instead (Suggested by brian d.
+foy):
$string =~ s/ (
(^\w) #at the beginning of the line
C<"like \"this\"">. Unescaping them is a task addressed earlier in
this section.
-Alternatively, the Text::ParseWords module (part of the standard perl
+Alternatively, the Text::ParseWords module (part of the standard Perl
distribution) lets you say:
use Text::ParseWords;
=head2 How do I find the soundex value of a string?
-Use the standard Text::Soundex module distributed with perl.
+Use the standard Text::Soundex module distributed with Perl.
But before you do so, you may want to determine whether `soundex' is in
fact what you think it is. Knuth's soundex algorithm compresses words
into a small space, and so it does not necessarily distinguish between
print "@lines"; # WRONG - extra blanks
print @lines; # right
-=head2 Why don't my E<lt>E<lt>HERE documents work?
+=head2 Why don't my <<HERE documents work?
Check for these three things:
@bad[0] = `same program that outputs several lines`;
-The B<-w> flag will warn you about these matters.
+The C<use warnings> pragma and the B<-w> flag will warn you about these
+matters.
=head2 How can I remove duplicate elements from a list or array?
This is nice in that it doesn't use much extra memory, simulating
uniq(1)'s behavior of removing only adjacent duplicates. It's less
nice in that it won't work with false values like undef, 0, or "";
-"0 but true" is ok, though.
+"0 but true" is OK, though.
=item b) If you don't know whether @in is sorted:
undef @ary;
@ary[@in] = @in;
- @out = @ary;
+ @out = grep {defined} @ary;
=back
sub compare_arrays {
my ($first, $second) = @_;
- local $^W = 0; # silence spurious -w undef complaints
+ no warnings; # silence spurious -w undef complaints
return 0 unless @$first == @$second;
for (my $i = 0; $i < @$first; $i++) {
return 0 if $first->[$i] ne $second->[$i];
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) operations on perl's
+arbitrary points. Both pop and shift are both 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.
@list = sort { $a <=> $b } @list;
The default sort function is cmp, string comparison, which would
-sort C<(1, 2, 10)> into C<(1, 10, 2)>. C<E<lt>=E<gt>>, used above, is
+sort C<(1, 2, 10)> into C<(1, 10, 2)>. C<< <=> >>, used above, is
the numerical comparison operator.
If you have a complicated function needed to pull out the part you
Don't do that. :-)
[lwall] In Perl 4, you were not allowed to modify a hash at all while
-interating over it. In Perl 5 you can delete from it, but you still
+iterating over it. In Perl 5 you can delete from it, but you still
can't add to it, because that might cause a doubling of the hash table,
in which half the entries get copied up to the new top half of the
-table, at which point you've totally bamboozled the interator code.
+table, at which point you've totally bamboozled the iterator code.
Even if the table doesn't double, there's no telling whether your new
entry will be inserted before or after the current iterator position.
$ary{'d'} is false
defined $ary{'d'} is true
defined $ary{'a'} is true
- exists $ary{'a'} is true (perl5 only)
+ exists $ary{'a'} is true (Perl5 only)
grep ($_ eq 'a', keys %ary) is true
If you now say
$ary{'d'} is false
defined $ary{'d'} is true
defined $ary{'a'} is FALSE
- exists $ary{'a'} is true (perl5 only)
+ exists $ary{'a'} is true (Perl5 only)
grep ($_ eq 'a', keys %ary) is true
Notice the last two: you have an undef value, but a defined key!
$ary{'d'} is false
defined $ary{'d'} is true
defined $ary{'a'} is false
- exists $ary{'a'} is FALSE (perl5 only)
+ exists $ary{'a'} is FALSE (Perl5 only)
grep ($_ eq 'a', keys %ary) is FALSE
See, the whole entry is gone!
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.
-This has been fixed as of perl5.004.
+This has been fixed as of Perl5.004.
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
=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.
+module distributed with Perl.
=head1 Data: Misc
}
}
- sub is_numeric { defined &getnum }
+ sub is_numeric { defined getnum($_[0]) }
-Or you could check out
-http://www.perl.com/CPAN/modules/by-module/String/String-Scanf-1.1.tar.gz
-instead. The POSIX module (part of the standard Perl distribution)
-provides the C<strtol> and C<strtod> for converting strings to double
-and longs, respectively.
+Or you could check out the String::Scanf module on CPAN instead. The
+POSIX module (part of the standard Perl distribution) provides the
+C<strtol> and C<strtod> for converting strings to double and longs,
+respectively.
=head2 How do I keep persistent data across program calls?