1perlfaq(3)            User Contributed Perl Documentation           perlfaq(3)
2
3
4

NAME

6       perlfaq - Frequently asked questions about Perl
7

VERSION

9       version 5.20201107
10

DESCRIPTION

12       The perlfaq comprises several documents that answer the most commonly
13       asked questions about Perl and Perl programming. It's divided by topic
14       into nine major sections outlined in this document.
15
16   Where to find the perlfaq
17       The perlfaq is an evolving document.  Read the latest version at
18       <https://perldoc.perl.org/perlfaq>.  It is also included in the
19       standard Perl distribution.
20
21   How to use the perlfaq
22       The "perldoc" command line tool is part of the standard Perl
23       distribution. To read the perlfaq:
24
25           $ perldoc perlfaq
26
27       To search the perlfaq question headings:
28
29           $ perldoc -q open
30
31   How to contribute to the perlfaq
32       Review <https://github.com/perl-doc-cats/perlfaq/wiki>.  If you don't
33       find your suggestion create an issue or pull request against
34       <https://github.com/perl-doc-cats/perlfaq>.
35
36       Once approved, changes will be distributed with the next Perl release
37       and subsequently appear at <https://perldoc.perl.org/perlfaq>.
38
39   What if my question isn't answered in the FAQ?
40       Try the resources in perlfaq2.
41

TABLE OF CONTENTS

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

THE QUESTIONS

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

CREDITS

753       Tom Christiansen wrote the original perlfaq then expanded it with the
754       help of Nat Torkington. brian d foy substantially edited and expanded
755       the perlfaq. perlfaq-workers and others have also supplied feedback,
756       patches and corrections over the years.
757
759       Tom Christiansen wrote the original version of this document.  brian d
760       foy "<bdfoy@cpan.org>" wrote this version. See the individual perlfaq
761       documents for additional copyright information.
762
763       This document is available under the same terms as Perl itself. Code
764       examples in all the perlfaq documents are in the public domain. Use
765       them as you see fit (and at your own risk with no warranty from
766       anyone).
767
768
769
770perl v5.32.0                      2020-11-09                        perlfaq(3)
Impressum