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 find the perlfaq
14       The perlfaq is an evolving document.  Read the latest version at
15       <http://learn.perl.org/faq/>.  It is also included in the standard Perl
16       distribution.
17
18   How to use the perlfaq
19       The "perldoc" command line tool is part of the standard Perl
20       distribution. To read the perlfaq:
21
22           $ perldoc perlfaq
23
24       To search the perlfaq question headings:
25
26           $ perldoc -q open
27
28   How to contribute to the perlfaq
29       Review https://github.com/perl-doc-cats/perlfaq/wiki
30       <https://github.com/perl-doc-cats/perlfaq/wiki>.  If you don't find
31       your suggestion create an issue or pull request against
32       https://github.com/perl-doc-cats/perlfaq <https://github.com/perl-doc-
33       cats/perlfaq>.
34
35       Once approved, changes are merged into
36       <https://github.com/tpf/perlfaq>, the repository which drives
37       <http://learn.perl.org/faq/>, and they are distributed with the next
38       Perl 5 release.
39
40   What if my question isn't answered in the FAQ?
41       Try the resources in perlfaq2.
42

TABLE OF CONTENTS

44       perlfaq1 - General Questions About Perl
45       perlfaq2 - Obtaining and Learning about Perl
46       perlfaq3 - Programming Tools
47       perlfaq4 - Data Manipulation
48       perlfaq5 - Files and Formats
49       perlfaq6 - Regular Expressions
50       perlfaq7 - General Perl Language Issues
51       perlfaq8 - System Interaction
52       perlfaq9 - Web, Email and Networking
53

THE QUESTIONS

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

CREDITS

747       Tom Christiansen wrote the original perlfaq then expanded it with the
748       help of Nat Torkington. brian d foy substantialy edited and expanded
749       the perlfaq. perlfaq-workers and others have also supplied feedback,
750       patches and corrections over the years.
751
753       Tom Christiansen wrote the original version of this document.  brian d
754       foy "<bdfoy@cpan.org>" wrote this version. See the individual perlfaq
755       documents for additional copyright information.
756
757       This document is available under the same terms as Perl itself. Code
758       examples in all the perlfaq documents are in the public domain. Use
759       them as you see fit (and at your own risk with no warranty from
760       anyone).
761
762
763
764perl v5.16.3                      2013-03-04                        PERLFAQ(1)
Impressum