1perlfaq(3) User Contributed Perl Documentation perlfaq(3)
2
3
4
6 perlfaq - Frequently asked questions about Perl
7
9 version 5.20210520
10
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
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
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 • Does Perl have anything like Ruby's #{} or Python's f string?
266
267 • What's wrong with always quoting "$vars"?
268
269 • Why don't my <<HERE documents work?
270
271 • What is the difference between a list and an array?
272
273 • What is the difference between $array[1] and @array[1]?
274
275 • How can I remove duplicate elements from a list or array?
276
277 • How can I tell whether a certain element is contained in a list or
278 array?
279
280 • How do I compute the difference of two arrays? How do I compute the
281 intersection of two arrays?
282
283 • How do I test whether two arrays or hashes are equal?
284
285 • How do I find the first array element for which a condition is
286 true?
287
288 • How do I handle linked lists?
289
290 • How do I handle circular lists?
291
292 • How do I shuffle an array randomly?
293
294 • How do I process/modify each element of an array?
295
296 • How do I select a random element from an array?
297
298 • How do I permute N elements of a list?
299
300 • How do I sort an array by (anything)?
301
302 • How do I manipulate arrays of bits?
303
304 • Why does defined() return true on empty arrays and hashes?
305
306 • How do I process an entire hash?
307
308 • How do I merge two hashes?
309
310 • What happens if I add or remove keys from a hash while iterating
311 over it?
312
313 • How do I look up a hash element by value?
314
315 • How can I know how many entries are in a hash?
316
317 • How do I sort a hash (optionally by value instead of key)?
318
319 • How can I always keep my hash sorted?
320
321 • What's the difference between "delete" and "undef" with hashes?
322
323 • Why don't my tied hashes make the defined/exists distinction?
324
325 • How do I reset an each() operation part-way through?
326
327 • How can I get the unique keys from two hashes?
328
329 • How can I store a multidimensional array in a DBM file?
330
331 • How can I make my hash remember the order I put elements into it?
332
333 • Why does passing a subroutine an undefined element in a hash create
334 it?
335
336 • How can I make the Perl equivalent of a C structure/C++ class/hash
337 or array of hashes or arrays?
338
339 • How can I use a reference as a hash key?
340
341 • How can I check if a key exists in a multilevel hash?
342
343 • How can I prevent addition of unwanted keys into a hash?
344
345 • How do I handle binary data correctly?
346
347 • How do I determine whether a scalar is a
348 number/whole/integer/float?
349
350 • How do I keep persistent data across program calls?
351
352 • How do I print out or copy a recursive data structure?
353
354 • How do I define methods for every class/object?
355
356 • How do I verify a credit card checksum?
357
358 • How do I pack arrays of doubles or floats for XS code?
359
360 perlfaq5: Files and Formats
361 This section deals with I/O and the "f" issues: filehandles, flushing,
362 formats, and footers.
363
364 • How do I flush/unbuffer an output filehandle? Why must I do this?
365
366 • How do I change, delete, or insert a line in a file, or append to
367 the beginning of a file?
368
369 • How do I count the number of lines in a file?
370
371 • How do I delete the last N lines from a file?
372
373 • How can I use Perl's "-i" option from within a program?
374
375 • How can I copy a file?
376
377 • How do I make a temporary file name?
378
379 • How can I manipulate fixed-record-length files?
380
381 • How can I make a filehandle local to a subroutine? How do I pass
382 filehandles between subroutines? How do I make an array of
383 filehandles?
384
385 • How can I use a filehandle indirectly?
386
387 • How can I open a filehandle to a string?
388
389 • How can I set up a footer format to be used with write()?
390
391 • How can I write() into a string?
392
393 • How can I output my numbers with commas added?
394
395 • How can I translate tildes (~) in a filename?
396
397 • How come when I open a file read-write it wipes it out?
398
399 • Why do I sometimes get an "Argument list too long" when I use <*>?
400
401 • How can I open a file named with a leading ">" or trailing blanks?
402
403 • How can I reliably rename a file?
404
405 • How can I lock a file?
406
407 • Why can't I just open(FH, ">file.lock")?
408
409 • I still don't get locking. I just want to increment the number in
410 the file. How can I do this?
411
412 • All I want to do is append a small amount of text to the end of a
413 file. Do I still have to use locking?
414
415 • How do I randomly update a binary file?
416
417 • How do I get a file's timestamp in perl?
418
419 • How do I set a file's timestamp in perl?
420
421 • How do I print to more than one file at once?
422
423 • How can I read in an entire file all at once?
424
425 • How can I read in a file by paragraphs?
426
427 • How can I read a single character from a file? From the keyboard?
428
429 • How can I tell whether there's a character waiting on a filehandle?
430
431 • How do I do a "tail -f" in perl?
432
433 • How do I dup() a filehandle in Perl?
434
435 • How do I close a file descriptor by number?
436
437 • Why can't I use "C:\temp\foo" in DOS paths? Why doesn't
438 `C:\temp\foo.exe` work?
439
440 • Why doesn't glob("*.*") get all the files?
441
442 • Why does Perl let me delete read-only files? Why does "-i" clobber
443 protected files? Isn't this a bug in Perl?
444
445 • How do I select a random line from a file?
446
447 • Why do I get weird spaces when I print an array of lines?
448
449 • How do I traverse a directory tree?
450
451 • How do I delete a directory tree?
452
453 • How do I copy an entire directory?
454
455 perlfaq6: Regular Expressions
456 This section is surprisingly small because the rest of the FAQ is
457 littered with answers involving regular expressions. For example,
458 decoding a URL and checking whether something is a number can be
459 handled with regular expressions, but those answers are found elsewhere
460 in this document (in perlfaq9 : "How do I decode or create those
461 %-encodings on the web" and perlfaq4 : "How do I determine whether a
462 scalar is a number/whole/integer/float", to be precise).
463
464 • How can I hope to use regular expressions without creating
465 illegible and unmaintainable code?
466
467 • I'm having trouble matching over more than one line. What's wrong?
468
469 • How can I pull out lines between two patterns that are themselves
470 on different lines?
471
472 • How do I match XML, HTML, or other nasty, ugly things with a regex?
473
474 • I put a regular expression into $/ but it didn't work. What's
475 wrong?
476
477 • How do I substitute case-insensitively on the LHS while preserving
478 case on the RHS?
479
480 • How can I make "\w" match national character sets?
481
482 • How can I match a locale-smart version of "/[a-zA-Z]/" ?
483
484 • How can I quote a variable to use in a regex?
485
486 • What is "/o" really for?
487
488 • How do I use a regular expression to strip C-style comments from a
489 file?
490
491 • Can I use Perl regular expressions to match balanced text?
492
493 • What does it mean that regexes are greedy? How can I get around it?
494
495 • How do I process each word on each line?
496
497 • How can I print out a word-frequency or line-frequency summary?
498
499 • How can I do approximate matching?
500
501 • How do I efficiently match many regular expressions at once?
502
503 • Why don't word-boundary searches with "\b" work for me?
504
505 • Why does using $&, $`, or $' slow my program down?
506
507 • What good is "\G" in a regular expression?
508
509 • Are Perl regexes DFAs or NFAs? Are they POSIX compliant?
510
511 • What's wrong with using grep in a void context?
512
513 • How can I match strings with multibyte characters?
514
515 • How do I match a regular expression that's in a variable?
516
517 perlfaq7: General Perl Language Issues
518 This section deals with general Perl language issues that don't clearly
519 fit into any of the other sections.
520
521 • Can I get a BNF/yacc/RE for the Perl language?
522
523 • What are all these $@%&* punctuation signs, and how do I know when
524 to use them?
525
526 • Do I always/never have to quote my strings or use semicolons and
527 commas?
528
529 • How do I skip some return values?
530
531 • How do I temporarily block warnings?
532
533 • What's an extension?
534
535 • Why do Perl operators have different precedence than C operators?
536
537 • How do I declare/create a structure?
538
539 • How do I create a module?
540
541 • How do I adopt or take over a module already on CPAN?
542
543 • How do I create a class?
544
545 • How can I tell if a variable is tainted?
546
547 • What's a closure?
548
549 • What is variable suicide and how can I prevent it?
550
551 • How can I pass/return a {Function, FileHandle, Array, Hash, Method,
552 Regex}?
553
554 • How do I create a static variable?
555
556 • What's the difference between dynamic and lexical (static) scoping?
557 Between local() and my()?
558
559 • How can I access a dynamic variable while a similarly named lexical
560 is in scope?
561
562 • What's the difference between deep and shallow binding?
563
564 • Why doesn't "my($foo) = <$fh>;" work right?
565
566 • How do I redefine a builtin function, operator, or method?
567
568 • What's the difference between calling a function as &foo and foo()?
569
570 • How do I create a switch or case statement?
571
572 • How can I catch accesses to undefined variables, functions, or
573 methods?
574
575 • Why can't a method included in this same file be found?
576
577 • How can I find out my current or calling package?
578
579 • How can I comment out a large block of Perl code?
580
581 • How do I clear a package?
582
583 • How can I use a variable as a variable name?
584
585 • What does "bad interpreter" mean?
586
587 • Do I need to recompile XS modules when there is a change in the C
588 library?
589
590 perlfaq8: System Interaction
591 This section of the Perl FAQ covers questions involving operating
592 system interaction. Topics include interprocess communication (IPC),
593 control over the user-interface (keyboard, screen and pointing
594 devices), and most anything else not related to data manipulation.
595
596 • How do I find out which operating system I'm running under?
597
598 • How come exec() doesn't return?
599
600 • How do I do fancy stuff with the keyboard/screen/mouse?
601
602 • How do I print something out in color?
603
604 • How do I read just one key without waiting for a return key?
605
606 • How do I check whether input is ready on the keyboard?
607
608 • How do I clear the screen?
609
610 • How do I get the screen size?
611
612 • How do I ask the user for a password?
613
614 • How do I read and write the serial port?
615
616 • How do I decode encrypted password files?
617
618 • How do I start a process in the background?
619
620 • How do I trap control characters/signals?
621
622 • How do I modify the shadow password file on a Unix system?
623
624 • How do I set the time and date?
625
626 • How can I sleep() or alarm() for under a second?
627
628 • How can I measure time under a second?
629
630 • How can I do an atexit() or setjmp()/longjmp()? (Exception
631 handling)
632
633 • Why doesn't my sockets program work under System V (Solaris)? What
634 does the error message "Protocol not supported" mean?
635
636 • How can I call my system's unique C functions from Perl?
637
638 • Where do I get the include files to do ioctl() or syscall()?
639
640 • Why do setuid perl scripts complain about kernel problems?
641
642 • How can I open a pipe both to and from a command?
643
644 • Why can't I get the output of a command with system()?
645
646 • How can I capture STDERR from an external command?
647
648 • Why doesn't open() return an error when a pipe open fails?
649
650 • What's wrong with using backticks in a void context?
651
652 • How can I call backticks without shell processing?
653
654 • Why can't my script read from STDIN after I gave it EOF (^D on
655 Unix, ^Z on MS-DOS)?
656
657 • How can I convert my shell script to perl?
658
659 • Can I use perl to run a telnet or ftp session?
660
661 • How can I write expect in Perl?
662
663 • Is there a way to hide perl's command line from programs such as
664 "ps"?
665
666 • I {changed directory, modified my environment} in a perl script.
667 How come the change disappeared when I exited the script? How do I
668 get my changes to be visible?
669
670 • How do I close a process's filehandle without waiting for it to
671 complete?
672
673 • How do I fork a daemon process?
674
675 • How do I find out if I'm running interactively or not?
676
677 • How do I timeout a slow event?
678
679 • How do I set CPU limits?
680
681 • How do I avoid zombies on a Unix system?
682
683 • How do I use an SQL database?
684
685 • How do I make a system() exit on control-C?
686
687 • How do I open a file without blocking?
688
689 • How do I tell the difference between errors from the shell and
690 perl?
691
692 • How do I install a module from CPAN?
693
694 • What's the difference between require and use?
695
696 • How do I keep my own module/library directory?
697
698 • How do I add the directory my program lives in to the
699 module/library search path?
700
701 • How do I add a directory to my include path (@INC) at runtime?
702
703 • Where are modules installed?
704
705 • What is socket.ph and where do I get it?
706
707 perlfaq9: Web, Email and Networking
708 This section deals with questions related to running web sites, sending
709 and receiving email as well as general networking.
710
711 • Should I use a web framework?
712
713 • Which web framework should I use?
714
715 • What is Plack and PSGI?
716
717 • How do I remove HTML from a string?
718
719 • How do I extract URLs?
720
721 • How do I fetch an HTML file?
722
723 • How do I automate an HTML form submission?
724
725 • How do I decode or create those %-encodings on the web?
726
727 • How do I redirect to another page?
728
729 • How do I put a password on my web pages?
730
731 • How do I make sure users can't enter values into a form that causes
732 my CGI script to do bad things?
733
734 • How do I parse a mail header?
735
736 • How do I check a valid mail address?
737
738 • How do I decode a MIME/BASE64 string?
739
740 • How do I find the user's mail address?
741
742 • How do I send email?
743
744 • How do I use MIME to make an attachment to a mail message?
745
746 • How do I read email?
747
748 • How do I find out my hostname, domainname, or IP address?
749
750 • How do I fetch/put an (S)FTP file?
751
752 • How can I do RPC in Perl?
753
755 Tom Christiansen wrote the original perlfaq then expanded it with the
756 help of Nat Torkington. brian d foy substantially edited and expanded
757 the perlfaq. perlfaq-workers and others have also supplied feedback,
758 patches and corrections over the years.
759
761 Tom Christiansen wrote the original version of this document. brian d
762 foy "<bdfoy@cpan.org>" wrote this version. See the individual perlfaq
763 documents for additional copyright information.
764
765 This document is available under the same terms as Perl itself. Code
766 examples in all the perlfaq documents are in the public domain. Use
767 them as you see fit (and at your own risk with no warranty from
768 anyone).
769
770
771
772perl v5.36.0 2022-07-22 perlfaq(3)