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
15       The perlfaq comes with the standard Perl distribution, so if you have
16       Perl you should have the perlfaq. You should also have the "perldoc"
17       tool that let's you read the perlfaq:
18
19               $ perldoc perlfaq
20
21       Besides your local system, you can find the perlfaq on the web, includ‐
22       ing at http://perldoc.perl.org/ .
23
24       The perlfaq is an evolving document and you can read the latest version
25       at http://faq.perl.org/ . The perlfaq-workers periodically post
26       extracts of the latest perlfaq to comp.lang.perl.misc.
27
28       You can view the source tree at http://cvs.perl.org/viewcvs/cvs-pub
29       lic/perlfaq/ (which is outside of the main Perl source tree).  The CVS
30       repository notes all changes to the FAQ and holds the latest version of
31       the working documents and may vary significantly from the version dis‐
32       tributed with the latest version of Perl. Check the repository before
33       sending your corrections.
34
35       How to contribute to the perlfaq
36
37       You can mail corrections, additions, and suggestions to "<perlfaq-work‐
38       ers AT perl DOT org>". The perlfaq volunteers use this address to coor‐
39       dinate their efforts and track the perlfaq development.  They appreci‐
40       ate your contributions to the FAQ but do not have time to provide indi‐
41       vidual help, so don't use this address to ask FAQs.
42
43       The perlfaq server posts extracts of the perlfaq to that newsgroup
44       every 6 hours (or so), and the community of volunteers reviews and
45       updates the answers. If you'd like to help review and update the
46       answers, check out comp.lang.perl.misc.
47
48       What will happen if you mail your Perl programming problems to the
49       authors?
50
51       The perlfaq-workers like to keep all traffic on the perlfaq-workers
52       list so that everyone can see the work being done (and the work that
53       needs to be done). The mailing list serves as an official record. If
54       you email the authors or maintainers directly, you'll probably get a
55       reply asking you to post to the mailing list. If you don't get a reply,
56       it probably means that the person never saw the message or didn't have
57       time to deal with it. Posting to the list allows the volunteers with
58       time to deal with it when others are busy.
59
60       If you have a question that isn't in the FAQ and you would like help
61       with it, try the resources in perlfaq2.
62

CREDITS

64       Tom Christiansen wrote the original perlfaq then expanded it with the
65       help of Nat Torkington.  The perlfaq-workers maintain current document
66       and the dezinens of comp.lang.perl.misc regularly review and update the
67       FAQ. Several people have contributed answers, corrections, and com‐
68       ments, and the perlfaq notes those contributions wherever appropriate.
69
71       Tom Christainsen wrote the original version of this document.  brian d
72       foy "<bdfoy@cpan.org>" wrote this version. See the individual perlfaq
73       documents for additional copyright information.
74
75       This document is available under the same terms as Perl itself. Code
76       examples in all the perlfaq documents are in the public domain. Use
77       them as you see fit (and at your own risk with no warranty from any‐
78       one).
79

Table of Contents

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

The Questions

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