=head1 NAME perliaq - infrequently asked questions about Perl ($Date: 2008/01/29 16:51:33 $) =head1 DESCRIPTION I didn't want to put this section in, but C is a fascist. =head1 DISTRIBUTION This document is in the public domain. You can get a copy from http://perl.plover.com/IAQ/ =head1 Author and Copyright Information 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 C. =head1 How do I determine the shoe size of a directory? I tried C<-e> but that didn't work. Note that notions of shoe size vary from country to country. See the L man page for complete discussion. C<-e> only returns a boolean value that says whether or not the directory wears a wide shoe. =head1 How come C doesn't return? Have you considered using a mouthwash? =head1 How do I clear the screen? Use this function: sub clear_the_screen { print $\ x 1_000_000; } =head1 How do I compute the intersection of two lists? Just apply De Morgan's identity: A intersection B = complement ((complement A) union (complement B)) =head1 How do I trap control characters / signals? Look into the C modules, available from CPAN. =head1 I am a boy scout. How can I use Perl in my day-to-day scout business? For example, helping little old ladies across the street. See the L man page. =head1 Why does C print C, but C prints C? Because the name of the Deity must always be capitalized. =for credits Meng Wong. =head1 How can I force Perl to treat a number as a string? Try using a whip. =head1 I tried C and it gave me some weird error message. If you got `some weird error' the problem is with your frobobnitz. =for credits Roderick. =head1 What is the C argument to C used for? Set it to a true value to create a directory with ice cream on top. =head1 How can I find the creation date of a file? 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; } =head1 How do I convert a string to a number? Use this C function: sub atoi { my $t; foreach my $d (split(//, shift())) { $t = $t * 10 + $d; } return $t; } $number = atoi("123"); =head1 How do I convert a number to a string? Use C: $string = sprintf("%f", 123.45); =head1 How can I tell if a string is a number? The simplest method is: if ($string == "$string") { # It is a number } Note the use of the C<==> operator to compare the string to its numeric value. However, this approach is dangerous because the C<$string> might contain arbitrary code such as C<@{[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"; } =head1 How do I find the largest element in an array? Write a C loop to scan the elements one at a time, and stop when you get to the largest one. =head1 How can I get just the first half of a long string? Use $string x .5; =head1 Someone told me that C would be faster than C. Is this true? Why? C is faster on multiprocessor machines because it can be vectorized, so that each C is removed by a different processor. C, however, must always be performed sequentially. =head1 How do I get the length of a variable? Use length('$variable'); to find out how long a variable is. =head1 How do I write OO programs in Perl? Put C<-00> on your C<#!> line, like this: #!/usr/bin/perl -00 -w =for credits Gnat. =head1 How do I differentiate my Object Oriented Perl scripts from other perl scripts? Just as C programmers use C<.cpp> as an extension for their object oriented C programs, many perl programmers use C<.ppp> for their object oriented Perl programs. =for credits Ziggy =head1 How do I get my program to pause for five seconds? Use the C 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 } } =for credits Gnat. =head1 How do I sort a hash? %sorted = sort %hash; =head1 How do I sort a hash numerically? %sorted = sort {$a <=> $b} %hash; =head1 How do I sort a hash by value? 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 C function. But the second one will work even if you have a user-defined function named C. =head1 How do I sort an array in reverse? @sorted = sort reverse @array; =head1 How do I sort a 2-D 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. =head1 Is Perl Year-2000 compliant? 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. =head1 What's a regular expression to replace C<09:23:53 08-OCT-98> with C? s{.*}{Thu Oct 8 09:23:53 1998} =head1 What's this C thing I keep hearing about? You use C when you need to operate on a directory with an extra-wide shoe. =head1 How do I get tomorrow's date? Use this function: sub tomorrow_date { sleep 86_400; return localtime(); } =for credits Abigail. =head1 What's the difference betwen C and C? C only works on Unix systems, so you should use C for maximum portability. =for credits MJD 20041129 =head1 Can I get a YACC grammar for the Perl language? 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. =head1 What are all those $@%* signs for? Watch your $@*$!% mouth, buddy! =head1 Do I always have to quote my strings? 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; =head1 How do I block warnings? The simplest way is to do close STDERR; =head1 Why do Perl operators have different precedence than C operators? Because Perl isn't C. Duh. =head1 How can I find out whether a number is odd? sub odd { my $number = shift; return !even ($number); } =for credits Abigail. =head1 How can I find out whether a number is even? sub even { my $number = abs shift; return 1 if $number == 0; return odd ($number - 1); } =for credits Abigail. =head1 How can I round up a number? $number->lasso(); =for credits Dr. Forrester =head1 How do I decrypt a string that I encrypted with C? 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]++; } } B: Exporting this function outside the USA may be illegal under the provisions of ITAR. =for credits Abigail, Dominus. =head1 How do I get my perl program to run faster? 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 C hierarchy is not found on CPAN. Please contact the author for more information. =for credits Ziggy Alternatively, programs have been known to complete more quickly when they contain a declaration like this one: BEGIN {exit} =for credits Kurt 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. =for credits `no more time' suggested by Abigail, `kernel panics' MJD. =head1 How do I get my perl program to use less memory? 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))); =for credits Ziggy, Bill Guindon =head1 Perldoc isn't running properly. Where can I find the documentation? perldoc perldoc =for credits Ziggy =head1 I have a lot of Bourne shell scripts I'd like to convert to Perl... What's the easiest way to do it? Make sure that the first line of your script is C<#!/bin/sh>. That magic comment tells the Perl interpreter to understand Bourne shell syntax. =for credits Ziggy =head1 I've got a C program I want to translate to Perl... How do I do that? If you're using C, version 2.7 or better, you can use the translate-to-perl mode. gcc -P -E foo.c > foo.pl (C is smart enough to figure out that you want to convert to 'perl' by specifying the 'C

' and 'C' alone.) For versions of C prior to 2.7, use gcc -larry -Wall foo.c > foo.pl =for credits Ziggy; -Wall provided by Abigail =head1 What's the difference between single quoted strings and double quoted strings? Single quoted strings act like C; double quoted strings act like C. =for credits Ziggy =head1 What's the difference between C and C? C. =for credits Ziggy