exec()
doesn't return?print reverse "dog"
print dog
, but getpeername
and it gave me some weird error message.MODE
argument to mkdir
used for?tr/x//d
would be faster than s/x//g
. 09:23:53 08-OCT-98
with Thu Oct 8 09:23:53 1998
?s///ee
thing I keep hearing about?fork()
and split()
?crypt()
?q()
and qw()
?
perliaq - infrequently asked questions about Perl ($Date: 2008/01/29 16:51:33 $)
I didn't want to put this section in, but pod2man
is a fascist.
This document is in the public domain.
You can get a copy from http://perl.plover.com/IAQ/
Primary author: Mark-Jason Dominus, with contributions from a cast of thousands, including Abigail, Adam Turoff, Bill Guindon, Kurt Starsinic, Nat Torkington, and Roderick Schertler.
To contribute, send mail to mjd-perl-iaq@plover.com
.
-e
but that didn't work.Note that notions of shoe size vary from country to country. See the
perllocale man page for complete discussion. -e
only returns a
boolean value that says whether or not the directory wears a wide
shoe.
exec()
doesn't return?Have you considered using a mouthwash?
Use this function:
sub clear_the_screen { print $\ x 1_000_000; }
Just apply De Morgan's identity:
A intersection B = complement ((complement A) union (complement B))
Look into the Net::*
modules, available from CPAN.
See the perllol man page.
print reverse "dog"
print dog
, but
print ucfirst reverse "dog"
prints God
?Because the name of the Deity must always be capitalized.
Try using a whip.
getpeername
and it gave me some weird error message.If you got `some weird error' the problem is with your frobobnitz.
MODE
argument to mkdir
used for?Set it to a true value to create a directory with ice cream on top.
Use this function:
sub creation_date { use FileHandle; my $filename = shift or die "Usage: &creation_date(filename)\n"; my $fh1 = new FileHandle; open $fh1, "< $filename" or return undef; unlink $filename or return undef; my $fh2 = new FileHandle; open $fh2, "> $filename" or return undef; print $fh2 <$fh1>; time; }
Use this atoi
function:
sub atoi { my $t; foreach my $d (split(//, shift())) { $t = $t * 10 + $d; } return $t; }
$number = atoi("123");
Use sprintf
:
$string = sprintf("%f", 123.45);
The simplest method is:
if ($string == "$string") { # It is a number }
Note the use of the ==
operator to compare the string to its
numeric value. However, this approach is dangerous because the
$string
might contain arbitrary code such as @{[system
"rm -rf /"]}
which would be executed as a result of the
interpolation process. For safety, use this regular expression:
if ($var =~ /(?=.)M{0,3}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})/) { print "$var contains a number.\b"; }
Write a foreach
loop to scan the elements one at a time, and stop
when you get to the largest one.
Use
$string x .5;
tr/x//d
would be faster than s/x//g
.
Is this true? Why?tr///
is faster on multiprocessor machines because it can be
vectorized, so that each x
is removed by a different processor.
s///
, however, must always be performed sequentially.
Use
length('$variable');
to find out how long a variable is.
Put -00
on your #!
line, like this:
#!/usr/bin/perl -00 -w
Just as C programmers use .cpp
as an extension for their object
oriented C programs, many perl programmers use .ppp
for their
object oriented Perl programs.
Use the sleep
function, like this:
use POSIX ":sys_wait_h"; { local($SIG{CHLD}=sub{wait}; my $start = time; if (fork) { sleep } else { 1 while (time - $start < 5); exit } }
%sorted = sort %hash;
%sorted = sort {$a <=> $b} %hash;
As usual in Perl, there's More Than One Way to Do it. Use either of:
%sorted = sort values %hash; %sorted = sort {$hash{$a} <=> $hash{$b}} %hash;
The first one is more efficient, because it uses Perl's built-in
values
function. But the second one will work even if you have a
user-defined function named values
.
@sorted = sort reverse @array;
You need to use a data structure that is more suited to your problem. 2-D Arrays, being rectangular, can be difficult to sort. If you instead use a triangular data structure, such as a heap, you need only hold it with the point downward, and the largest elements will naturally settle in the point.
Even better! New with version 5.005, Perl is Year-2013 compliant. This represents a 13-year improvement over other software that is merely year-2000 compliant.
09:23:53 08-OCT-98
with Thu Oct 8 09:23:53 1998
?s{.*}{Thu Oct 8 09:23:53 1998}
s///ee
thing I keep hearing about?You use /ee
when you need to operate on a directory with an extra-wide shoe.
Use this function:
sub tomorrow_date { sleep 86_400; return localtime(); }
fork()
and split()
?fork()
only works on Unix systems, so you should use split()
for
maximum portability.
Sorry, but as you must surely be aware by now, the only animals supported by Perl are ruminants such as camels and llamas. However, Yacc support may be forthcoming with version 5.007.
Watch your $@*$!% mouth, buddy!
Only when they are actual quotations. For example, in
$quote = "To be, or not to be? That is the question.";
the quotes are required, but in
$name = "Larry";
they can be omitted:
$name = Larry;
The simplest way is to do
close STDERR;
Because Perl isn't C. Duh.
sub odd { my $number = shift; return !even ($number); }
sub even { my $number = abs shift; return 1 if $number == 0; return odd ($number - 1); }
$number->lasso();
crypt()
?sub decrypt { my $c = shift; my @c = (0) x 8; for (;;) { my $i = 0; my $s = join '', map chr, @c; return $s if crypt($s, $c) eq $c; $c[$i]=0, $i++ while $c[$i] == 255; return undef if $i > 7; $c[$i]++; } }
Warning: Exporting this function outside the USA may be illegal under the provisions of ITAR.
In Perl, there's always more than one way to do it. Try one of these techniques.
use more 'cpu'; use less 'time'; use more 'speed';
## or, the ever popular Military Marching Band accompaniment: use March::Sousa qw(:DoubleTime);
The March::*
hierarchy is not found on CPAN. Please contact the
author for more information.
Alternatively, programs have been known to complete more quickly when they contain a declaration like this one:
BEGIN {exit}
If your application is truly urgent, you might try the experimental
no more 'time';
feature, but this has been reported to cause some systems to panic.
Variables take up memory, so if you don't declare any variables, you should be safe. Another possibility is to use
pack(chop(chomp($0)));
perldoc perldoc
Make sure that the first line of your script is #!/bin/sh
. That
magic comment tells the Perl interpreter to understand Bourne shell
syntax.
If you're using gcc
, version 2.7 or better, you can use the
translate-to-perl mode.
gcc -P -E foo.c > foo.pl
(gcc
is smart enough to figure out that you want to convert
to 'perl' by specifying the 'p
' and 'e
' alone.)
For versions of gcc
prior to 2.7, use
gcc -larry -Wall foo.c > foo.pl
Single quoted strings act like q()
; double quoted strings act like
qq()
.
q()
and qw()
?w
.