1PERLFAQ(1)             Perl Programmers Reference Guide             PERLFAQ(1)
2
3
4

NAME

6       perlfaq - frequently asked questions about Perl
7

DESCRIPTION

9       The perlfaq comprises several documents that answer the most commonly
10       asked questions about Perl and Perl programming. It's divided by topic
11       into nine major sections outlined in this document.
12
13   Where to get the perlfaq
14       The perlfaq comes with the standard Perl distribution, so if you have
15       Perl you should have the perlfaq. You should also have the "perldoc"
16       tool that lets you read the perlfaq:
17
18               $ perldoc perlfaq
19
20       Besides your local system, you can find the perlfaq on the web,
21       including at http://perldoc.perl.org/ .
22
23       The perlfaq is an evolving document and you can read the latest version
24       at http://faq.perl.org/ . The perlfaq-workers periodically post
25       extracts of the latest perlfaq to comp.lang.perl.misc.
26
27       You can view the source tree at https://github.com/briandfoy/perlfaq
28       (which is outside of the main Perl source tree).  The git repository
29       notes all changes to the FAQ and holds the latest version of the
30       working documents and may vary significantly from the version
31       distributed with the latest version of Perl. Check the repository
32       before sending your corrections.
33
34   How to contribute to the perlfaq
35       You can mail corrections, additions, and suggestions to
36       "<perlfaq-workers AT perl DOT org>". The perlfaq volunteers use this
37       address to coordinate their efforts and track the perlfaq development.
38       They appreciate your contributions to the FAQ but do not have time to
39       provide individual help, so don't use this address to ask FAQs.
40
41       The perlfaq server posts extracts of the perlfaq to that newsgroup
42       every 6 hours (or so), and the community of volunteers reviews and
43       updates the answers. If you'd like to help review and update the
44       answers, check out comp.lang.perl.misc.
45
46       You can also fork the git repository for the perlfaq and send a pull
47       request so the main repository can pull your changes. The repository is
48       at:
49
50               https://github.com/briandfoy/perlfaq
51
52   What will happen if you mail your Perl programming problems to the authors?
53       The perlfaq-workers like to keep all traffic on the perlfaq-workers
54       list so that everyone can see the work being done (and the work that
55       needs to be done). The mailing list serves as an official record. If
56       you email the authors or maintainers directly, you'll probably get a
57       reply asking you to post to the mailing list. If you don't get a reply,
58       it probably means that the person never saw the message or didn't have
59       time to deal with it. Posting to the list allows the volunteers with
60       time to deal with it when others are busy.
61
62       If you have a question that isn't in the FAQ and you would like help
63       with it, try the resources in perlfaq2.
64

CREDITS

66       Tom Christiansen wrote the original perlfaq then expanded it with the
67       help of Nat Torkington.  The perlfaq-workers maintain current document
68       and the denizens of comp.lang.perl.misc regularly review and update the
69       FAQ. Several people have contributed answers, corrections, and
70       comments, and the perlfaq notes those contributions wherever
71       appropriate.
72
74       Copyright (c) 1997-2009 Tom Christiansen, Nathan Torkington, and other
75       authors as noted. All rights reserved.
76
77       Tom Christainsen wrote the original version of this document.  brian d
78       foy "<bdfoy@cpan.org>" wrote this version. See the individual perlfaq
79       documents for additional copyright information.
80
81       This document is available under the same terms as Perl itself. Code
82       examples in all the perlfaq documents are in the public domain. Use
83       them as you see fit and at your own risk with no warranty from anyone.
84

Table of Contents

86       perlfaq  - this document
87       perlfaq1 - General Questions About Perl
88       perlfaq2 - Obtaining and Learning about Perl
89       perlfaq3 - Programming Tools
90       perlfaq4 - Data Manipulation
91       perlfaq5 - Files and Formats
92       perlfaq6 - Regular Expressions
93       perlfaq7 - General Perl Language Issues
94       perlfaq8 - System Interaction
95       perlfaq9 - Networking
96

The Questions

98   perlfaq1: General Questions About Perl
99       Very general, high-level questions about Perl.
100
101       ·   What is Perl?
102
103       ·   Who supports Perl?  Who develops it?  Why is it free?
104
105       ·   Which version of Perl should I use?
106
107       ·   What are Perl 4, Perl 5, or Perl 6?
108
109       ·   What was Ponie?
110
111       ·   What is Perl 6?
112
113       ·   How stable is Perl?
114
115       ·   Is Perl difficult to learn?
116
117       ·   How does Perl compare with other languages like Java, Python, REXX,
118           Scheme, or Tcl?
119
120       ·   Can I do [task] in Perl?
121
122       ·   When shouldn't I program in Perl?
123
124       ·   What's the difference between "perl" and "Perl"?
125
126       ·   Is it a Perl program or a Perl script?
127
128       ·   What is a JAPH?
129
130       ·   Where can I get a list of Larry Wall witticisms?
131
132       ·   How can I convince others to use Perl?
133
134   perlfaq2: Obtaining and Learning about Perl
135       Where to find source and documentation for Perl, support, and related
136       matters.
137
138       ·   What machines support perl?  Where do I get it?
139
140       ·   How can I get a binary version of perl?
141
142       ·   I don't have a C compiler. How can I build my own Perl interpreter?
143
144       ·   I copied the perl binary from one machine to another, but scripts
145           don't work.
146
147       ·   I grabbed the sources and tried to compile but gdbm/dynamic
148           loading/malloc/linking/... failed.  How do I make it work?
149
150       ·   What modules and extensions are available for Perl?  What is CPAN?
151           What does CPAN/src/... mean?
152
153       ·   Is there an ISO or ANSI certified version of Perl?
154
155       ·   Where can I get information on Perl?
156
157       ·   What are the Perl newsgroups on Usenet?  Where do I post questions?
158
159       ·   Where should I post source code?
160
161       ·   Perl Books
162
163       ·   Which magazines have Perl content?
164
165       ·   What mailing lists are there for Perl?
166
167       ·   Where are the archives for comp.lang.perl.misc?
168
169       ·   Where can I buy a commercial version of perl?
170
171       ·   Where do I send bug reports?
172
173       ·   What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org?
174
175   perlfaq3: Programming Tools
176       Programmer tools and programming support.
177
178       ·   How do I do (anything)?
179
180       ·   How can I use Perl interactively?
181
182       ·   Is there a Perl shell?
183
184       ·   How do I find which modules are installed on my system?
185
186       ·   How do I debug my Perl programs?
187
188       ·   How do I profile my Perl programs?
189
190       ·   How do I cross-reference my Perl programs?
191
192       ·   Is there a pretty-printer (formatter) for Perl?
193
194       ·   Is there a ctags for Perl?
195
196       ·   Is there an IDE or Windows Perl Editor?
197
198       ·   Where can I get Perl macros for vi?
199
200       ·   Where can I get perl-mode for emacs?
201
202       ·   How can I use curses with Perl?
203
204       ·   How can I write a GUI (X, Tk, Gtk, etc.) in Perl?
205
206       ·   How can I make my Perl program run faster?
207
208       ·   How can I make my Perl program take less memory?
209
210       ·   Is it safe to return a reference to local or lexical data?
211
212       ·   How can I free an array or hash so my program shrinks?
213
214       ·   How can I make my CGI script more efficient?
215
216       ·   How can I hide the source for my Perl program?
217
218       ·   How can I compile my Perl program into byte code or C?
219
220       ·   How can I get "#!perl" to work on [MS-DOS,NT,...]?
221
222       ·   Can I write useful Perl programs on the command line?
223
224       ·   Why don't Perl one-liners work on my DOS/Mac/VMS system?
225
226       ·   Where can I learn about CGI or Web programming in Perl?
227
228       ·   Where can I learn about object-oriented Perl programming?
229
230       ·   Where can I learn about linking C with Perl?
231
232       ·   I've read perlembed, perlguts, etc., but I can't embed perl in my C
233           program; what am I doing wrong?
234
235       ·   When I tried to run my script, I got this message. What does it
236           mean?
237
238       ·   What's MakeMaker?
239
240   perlfaq4: Data Manipulation
241       Manipulating numbers, dates, strings, arrays, hashes, and miscellaneous
242       data issues.
243
244       ·   Why am I getting long decimals (eg, 19.9499999999999) instead of
245           the numbers I should be getting (eg, 19.95)?
246
247       ·   Why is int() broken?
248
249       ·   Why isn't my octal data interpreted correctly?
250
251       ·   Does Perl have a round() function?  What about ceil() and floor()?
252           Trig functions?
253
254       ·   How do I convert between numeric representations/bases/radixes?
255
256       ·   Why doesn't & work the way I want it to?
257
258       ·   How do I multiply matrices?
259
260       ·   How do I perform an operation on a series of integers?
261
262       ·   How can I output Roman numerals?
263
264       ·   Why aren't my random numbers random?
265
266       ·   How do I get a random number between X and Y?
267
268       ·   How do I find the day or week of the year?
269
270       ·   How do I find the current century or millennium?
271
272       ·   How can I compare two dates and find the difference?
273
274       ·   How can I take a string and turn it into epoch seconds?
275
276       ·   How can I find the Julian Day?
277
278       ·   How do I find yesterday's date?
279
280       ·   Does Perl have a Year 2000 problem? Is Perl Y2K compliant?
281
282       ·   How do I validate input?
283
284       ·   How do I unescape a string?
285
286       ·   How do I remove consecutive pairs of characters?
287
288       ·   How do I expand function calls in a string?
289
290       ·   How do I find matching/nesting anything?
291
292       ·   How do I reverse a string?
293
294       ·   How do I expand tabs in a string?
295
296       ·   How do I reformat a paragraph?
297
298       ·   How can I access or change N characters of a string?
299
300       ·   How do I change the Nth occurrence of something?
301
302       ·   How can I count the number of occurrences of a substring within a
303           string?
304
305       ·   How do I capitalize all the words on one line?
306
307       ·   How can I split a [character] delimited string except when inside
308           [character]?
309
310       ·   How do I strip blank space from the beginning/end of a string?
311
312       ·   How do I pad a string with blanks or pad a number with zeroes?
313
314       ·   How do I extract selected columns from a string?
315
316       ·   How do I find the soundex value of a string?
317
318       ·   How can I expand variables in text strings?
319
320       ·   What's wrong with always quoting "$vars"?
321
322       ·   Why don't my <<HERE documents work?
323
324       ·   What is the difference between a list and an array?
325
326       ·   What is the difference between $array[1] and @array[1]?
327
328       ·   How can I remove duplicate elements from a list or array?
329
330       ·   How can I tell whether a certain element is contained in a list or
331           array?
332
333       ·   How do I compute the difference of two arrays?  How do I compute
334           the intersection of two arrays?
335
336       ·   How do I test whether two arrays or hashes are equal?
337
338       ·   How do I find the first array element for which a condition is
339           true?
340
341       ·   How do I handle linked lists?
342
343       ·   How do I handle circular lists?
344
345       ·   How do I shuffle an array randomly?
346
347       ·   How do I process/modify each element of an array?
348
349       ·   How do I select a random element from an array?
350
351       ·   How do I permute N elements of a list?
352
353       ·   How do I sort an array by (anything)?
354
355       ·   How do I manipulate arrays of bits?
356
357       ·   Why does defined() return true on empty arrays and hashes?
358
359       ·   How do I process an entire hash?
360
361       ·   How do I merge two hashes?
362
363       ·   What happens if I add or remove keys from a hash while iterating
364           over it?
365
366       ·   How do I look up a hash element by value?
367
368       ·   How can I know how many entries are in a hash?
369
370       ·   How do I sort a hash (optionally by value instead of key)?
371
372       ·   How can I always keep my hash sorted?
373
374       ·   What's the difference between "delete" and "undef" with hashes?
375
376       ·   Why don't my tied hashes make the defined/exists distinction?
377
378       ·   How do I reset an each() operation part-way through?
379
380       ·   How can I get the unique keys from two hashes?
381
382       ·   How can I store a multidimensional array in a DBM file?
383
384       ·   How can I make my hash remember the order I put elements into it?
385
386       ·   Why does passing a subroutine an undefined element in a hash create
387           it?
388
389       ·   How can I make the Perl equivalent of a C structure/C++ class/hash
390           or array of hashes or arrays?
391
392       ·   How can I use a reference as a hash key?
393
394       ·   How do I handle binary data correctly?
395
396       ·   How do I determine whether a scalar is a
397           number/whole/integer/float?
398
399       ·   How do I keep persistent data across program calls?
400
401       ·   How do I print out or copy a recursive data structure?
402
403       ·   How do I define methods for every class/object?
404
405       ·   How do I verify a credit card checksum?
406
407       ·   How do I pack arrays of doubles or floats for XS code?
408
409   perlfaq5: Files and Formats
410       I/O and the "f" issues: filehandles, flushing, formats, and footers.
411
412       ·   How do I flush/unbuffer an output filehandle?  Why must I do this?
413
414       ·   How do I change, delete, or insert a line in a file, or append to
415           the beginning of a file?
416
417       ·   How do I count the number of lines in a file?
418
419       ·   How can I use Perl's "-i" option from within a program?
420
421       ·   How can I copy a file?
422
423       ·   How do I make a temporary file name?
424
425       ·   How can I manipulate fixed-record-length files?
426
427       ·   How can I make a filehandle local to a subroutine?  How do I pass
428           filehandles between subroutines?  How do I make an array of
429           filehandles?
430
431       ·   How can I use a filehandle indirectly?
432
433       ·   How can I set up a footer format to be used with write()?
434
435       ·   How can I write() into a string?
436
437       ·   How can I open a filehandle to a string?
438
439       ·   How can I output my numbers with commas added?
440
441       ·   How can I translate tildes (~) in a filename?
442
443       ·   How come when I open a file read-write it wipes it out?
444
445       ·   Why do I sometimes get an "Argument list too long" when I use <*>?
446
447       ·   Is there a leak/bug in glob()?
448
449       ·   How can I open a file with a leading ">" or trailing blanks?
450
451       ·   How can I reliably rename a file?
452
453       ·   How can I lock a file?
454
455       ·   Why can't I just open(FH, ">file.lock")?
456
457       ·   I still don't get locking.  I just want to increment the number in
458           the file.  How can I do this?
459
460       ·   All I want to do is append a small amount of text to the end of a
461           file.  Do I still have to use locking?
462
463       ·   How do I randomly update a binary file?
464
465       ·   How do I get a file's timestamp in perl?
466
467       ·   How do I set a file's timestamp in perl?
468
469       ·   How do I print to more than one file at once?
470
471       ·   How can I read in an entire file all at once?
472
473       ·   How can I read in a file by paragraphs?
474
475       ·   How can I read a single character from a file?  From the keyboard?
476
477       ·   How can I tell whether there's a character waiting on a filehandle?
478
479       ·   How do I do a "tail -f" in perl?
480
481       ·   How do I dup() a filehandle in Perl?
482
483       ·   How do I close a file descriptor by number?
484
485       ·   Why can't I use "C:\temp\foo" in DOS paths?  Why doesn't
486           `C:\temp\foo.exe` work?
487
488       ·   Why doesn't glob("*.*") get all the files?
489
490       ·   Why does Perl let me delete read-only files?  Why does "-i" clobber
491           protected files?  Isn't this a bug in Perl?
492
493       ·   How do I select a random line from a file?
494
495       ·   Why do I get weird spaces when I print an array of lines?
496
497       ·   How do I traverse a directory tree?
498
499       ·   How do I delete a directory tree?
500
501       ·   How do I copy an entire directory?
502
503   perlfaq6: Regular Expressions
504       This section is surprisingly small because the rest of the FAQ is
505       littered with answers involving regular expressions. For example,
506       decoding a URL and checking whether something is a number are handled
507       with regular expressions, but those answers are found elsewhere in this
508       document (in perlfaq9: "How do I decode or create those %-encodings on
509       the web" and perlfaq4: "How do I determine whether a scalar is a
510       number/whole/integer/float", to be precise).
511
512       ·   How can I hope to use regular expressions without creating
513           illegible and unmaintainable code?
514
515       ·   I'm having trouble matching over more than one line.  What's wrong?
516
517       ·   How can I pull out lines between two patterns that are themselves
518           on different lines?
519
520       ·   How do I match XML, HTML, or other nasty, ugly things with a regex?
521
522       ·   I put a regular expression into $/ but it didn't work. What's
523           wrong?
524
525       ·   How do I substitute case insensitively on the LHS while preserving
526           case on the RHS?
527
528       ·   How can I make "\w" match national character sets?
529
530       ·   How can I match a locale-smart version of "/[a-zA-Z]/"?
531
532       ·   How can I quote a variable to use in a regex?
533
534       ·   What is "/o" really for?
535
536       ·   How do I use a regular expression to strip C style comments from a
537           file?
538
539       ·   Can I use Perl regular expressions to match balanced text?
540
541       ·   What does it mean that regexes are greedy?  How can I get around
542           it?
543
544       ·   How do I process each word on each line?
545
546       ·   How can I print out a word-frequency or line-frequency summary?
547
548       ·   How can I do approximate matching?
549
550       ·   How do I efficiently match many regular expressions at once?
551
552       ·   Why don't word-boundary searches with "\b" work for me?
553
554       ·   Why does using $&, $`, or $' slow my program down?
555
556       ·   What good is "\G" in a regular expression?
557
558       ·   Are Perl regexes DFAs or NFAs?  Are they POSIX compliant?
559
560       ·   What's wrong with using grep in a void context?
561
562       ·   How can I match strings with multibyte characters?
563
564       ·   How do I match a regular expression that's in a variable?
565
566   perlfaq7: General Perl Language Issues
567       General Perl language issues that don't clearly fit into any of the
568       other sections.
569
570       ·   Can I get a BNF/yacc/RE for the Perl language?
571
572       ·   What are all these $@%&* punctuation signs, and how do I know when
573           to use them?
574
575       ·   Do I always/never have to quote my strings or use semicolons and
576           commas?
577
578       ·   How do I skip some return values?
579
580       ·   How do I temporarily block warnings?
581
582       ·   What's an extension?
583
584       ·   Why do Perl operators have different precedence than C operators?
585
586       ·   How do I declare/create a structure?
587
588       ·   How do I create a module?
589
590       ·   How do I adopt or take over a module already on CPAN?
591
592       ·   How do I create a class?
593
594       ·   How can I tell if a variable is tainted?
595
596       ·   What's a closure?
597
598       ·   What is variable suicide and how can I prevent it?
599
600       ·   How can I pass/return a {Function, FileHandle, Array, Hash, Method,
601           Regex}?
602
603       ·   How do I create a static variable?
604
605       ·   What's the difference between dynamic and lexical (static) scoping?
606           Between local() and my()?
607
608       ·   How can I access a dynamic variable while a similarly named lexical
609           is in scope?
610
611       ·   What's the difference between deep and shallow binding?
612
613       ·   Why doesn't "my($foo) = <FILE>;" work right?
614
615       ·   How do I redefine a builtin function, operator, or method?
616
617       ·   What's the difference between calling a function as &foo and foo()?
618
619       ·   How do I create a switch or case statement?
620
621       ·   How can I catch accesses to undefined variables, functions, or
622           methods?
623
624       ·   Why can't a method included in this same file be found?
625
626       ·   How can I find out my current or calling package?
627
628       ·   How can I comment out a large block of perl code?
629
630       ·   How do I clear a package?
631
632       ·   How can I use a variable as a variable name?
633
634       ·   What does "bad interpreter" mean?
635
636   perlfaq8: System Interaction
637       This section of the Perl FAQ covers questions involving operating
638       system interaction. Topics include interprocess communication (IPC),
639       control over the user-interface (keyboard, screen and pointing
640       devices), and most anything else not related to data manipulation. Read
641       the FAQs and documentation specific to the port of perl to your
642       operating system (eg, perlvms, perlplan9, ...). These should contain
643       more detailed information on the vagaries of your perl.
644
645       ·   How do I find out which operating system I'm running under?
646
647       ·   How come exec() doesn't return?
648
649       ·   How do I do fancy stuff with the keyboard/screen/mouse?
650
651       ·   How do I print something out in color?
652
653       ·   How do I read just one key without waiting for a return key?
654
655       ·   How do I check whether input is ready on the keyboard?
656
657       ·   How do I clear the screen?
658
659       ·   How do I get the screen size?
660
661       ·   How do I ask the user for a password?
662
663       ·   How do I read and write the serial port?
664
665       ·   How do I decode encrypted password files?
666
667       ·   How do I start a process in the background?
668
669       ·   How do I trap control characters/signals?
670
671       ·   How do I modify the shadow password file on a Unix system?
672
673       ·   How do I set the time and date?
674
675       ·   How can I sleep() or alarm() for under a second?
676
677       ·   How can I measure time under a second?
678
679       ·   How can I do an atexit() or setjmp()/longjmp()? (Exception
680           handling)
681
682       ·   Why doesn't my sockets program work under System V (Solaris)?  What
683           does the error message "Protocol not supported" mean?
684
685       ·   How can I call my system's unique C functions from Perl?
686
687       ·   Where do I get the include files to do ioctl() or syscall()?
688
689       ·   Why do setuid perl scripts complain about kernel problems?
690
691       ·   How can I open a pipe both to and from a command?
692
693       ·   Why can't I get the output of a command with system()?
694
695       ·   How can I capture STDERR from an external command?
696
697       ·   Why doesn't open() return an error when a pipe open fails?
698
699       ·   What's wrong with using backticks in a void context?
700
701       ·   How can I call backticks without shell processing?
702
703       ·   Why can't my script read from STDIN after I gave it EOF (^D on
704           Unix, ^Z on MS-DOS)?
705
706       ·   How can I convert my shell script to perl?
707
708       ·   Can I use perl to run a telnet or ftp session?
709
710       ·   How can I write expect in Perl?
711
712       ·   Is there a way to hide perl's command line from programs such as
713           "ps"?
714
715       ·   I {changed directory, modified my environment} in a perl script.
716           How come the change disappeared when I exited the script?  How do I
717           get my changes to be visible?
718
719       ·   How do I close a process's filehandle without waiting for it to
720           complete?
721
722       ·   How do I fork a daemon process?
723
724       ·   How do I find out if I'm running interactively or not?
725
726       ·   How do I timeout a slow event?
727
728       ·   How do I set CPU limits?
729
730       ·   How do I avoid zombies on a Unix system?
731
732       ·   How do I use an SQL database?
733
734       ·   How do I make a system() exit on control-C?
735
736       ·   How do I open a file without blocking?
737
738       ·   How do I tell the difference between errors from the shell and
739           perl?
740
741       ·   How do I install a module from CPAN?
742
743       ·   What's the difference between require and use?
744
745       ·   How do I keep my own module/library directory?
746
747       ·   How do I add the directory my program lives in to the
748           module/library search path?
749
750       ·   How do I add a directory to my include path (@INC) at runtime?
751
752       ·   What is socket.ph and where do I get it?
753
754   perlfaq9: Networking
755       Networking, the internet, and a few on the web.
756
757       ·   What is the correct form of response from a CGI script?
758
759       ·   My CGI script runs from the command line but not the browser.  (500
760           Server Error)
761
762       ·   How can I get better error messages from a CGI program?
763
764       ·   How do I remove HTML from a string?
765
766       ·   How do I extract URLs?
767
768       ·   How do I download a file from the user's machine?  How do I open a
769           file on another machine?
770
771       ·   How do I make an HTML pop-up menu with Perl?
772
773       ·   How do I fetch an HTML file?
774
775       ·   How do I automate an HTML form submission?
776
777       ·   How do I decode or create those %-encodings on the web?
778
779       ·   How do I redirect to another page?
780
781       ·   How do I put a password on my web pages?
782
783       ·   How do I edit my .htpasswd and .htgroup files with Perl?
784
785       ·   How do I make sure users can't enter values into a form that cause
786           my CGI script to do bad things?
787
788       ·   How do I parse a mail header?
789
790       ·   How do I decode a CGI form?
791
792       ·   How do I check a valid mail address?
793
794       ·   How do I decode a MIME/BASE64 string?
795
796       ·   How do I return the user's mail address?
797
798       ·   How do I send mail?
799
800       ·   How do I use MIME to make an attachment to a mail message?
801
802       ·   How do I read mail?
803
804       ·   How do I find out my hostname, domainname, or IP address?
805
806       ·   How do I fetch a news article or the active newsgroups?
807
808       ·   How do I fetch/put an FTP file?
809
810       ·   How can I do RPC in Perl?
811
812
813
814perl v5.10.1                      2009-08-15                        PERLFAQ(1)
Impressum