1 Archive-name: perl-faq/part0
2 Version: $Id: faq,v 1.1 92/11/30 05:12:22 tchrist Exp Locker: tchrist $
4 This article contains the table of contents to some of the most
5 frequently asked questions in comp.lang.perl, a newsgroup devoted to
6 the Perl programming language. There are two pieces following
7 this, the general information questions in part1 and the largely
8 technical opnes in part2.
10 They're all good questions, but they come up often enough that
11 substantial net bandwidth can be saved by looking here first before
12 asking. Before posting a question, you really should consult the Perl
13 man page; there's a lot of information packed in there.
15 Some questions in this group aren't really about Perl, but rather
16 about system-specific issues. You might also consult the Most
17 Frequently Asked Questions list in comp.unix.questions for answers
18 to this type of question.
20 The current version of perl is 4.035 (version 4, patchlevel 35).
21 There haven't actually been 35 updates to perl4; rather, the context
22 diffs posted to the net have been broken up into 35 news-digestable
25 This list is maintained by Tom Christiansen, and is archived on
26 convex.com [130.168.1.1] in the file pub/perl/info/faq. If you
27 have any suggested additions or corrections to this article, please
28 send them to Tom at either <tchrist@convex.com> or <convex!tchrist>.
29 Special thanks to Larry Wall for initially reviewing this list for
30 accuracy and especially for writing and releasing Perl in the first place.
34 1.2) Is Perl hard to learn?
35 1.3) Should I program everything in Perl?
36 1.4) Where can I get Perl over the Internet?
37 1.5) Where can I get Perl via Email?
38 1.6) How can I get Perl via UUCP?
39 1.7) Where can I get more information on Perl?
40 1.8) Can people who aren't on USENET receive comp.lang.perl as a digest?
41 1.9) Are archives of comp.lang.perl available?
42 1.10) How do I get Perl to run on machine FOO?
43 1.11) Where can I get (info|inter|ora|sql|syb)perl?
44 1.12) There's an a2p and an s2p; why isn't there a p2c (perl-to-C)?
45 1.13) Where can I get undump for my machine?
46 1.14) Where can I get a perl-mode for emacs?
47 1.15) How can I use Perl interactively?
48 1.16) Is there a Perl shell?
49 1.17) Is there a Perl profiler?
50 1.18) Is there a yacc for Perl?
51 1.19) How can I use curses with perl?
52 1.20) How can I use X with Perl?
53 1.21) What is perl4? What is perl5?
54 1.22) How does Perl compare with languages like REXX or TCL?
55 1.23) Is it a Perl program or a Perl script?
56 1.24) What's the difference between "Perl" and "perl"?
57 1.25) What companies use or ship Perl?
58 1.26) Is there commercial, 3rd-party support for Perl?
59 1.27) Where can I get a list of the JAPH signature quotes?
60 1.28) Where can I get a list of Larry Wall witticisms?
62 2.1) What are all these $@*%<> signs and how do I know when to use them?
63 2.2) Why don't backticks work as they do in shells?
64 2.3) How come Perl operators have different precedence than C operators?
65 2.4) How come my converted awk/sed/sh script runs more slowly in Perl?
66 2.5) How can I call my system's unique C functions from Perl?
67 2.6) Where do I get the include files to do ioctl() or syscall()?
68 2.7) Why doesn't "local($foo) = <FILE>;" work right?
69 2.8) How can I detect keyboard input without reading it?
70 2.9) How can I make an array of arrays or other recursive data types?
71 2.10) How can I quote a variable to use in a regexp?
72 2.11) Why do setuid Perl scripts complain about kernel problems?
73 2.12) How do I open a pipe both to and from a command?
74 2.13) How can I change the first N letters of a string?
75 2.14) How can I manipulate fixed-record-length files?
76 2.15) How can I make a file handle local to a subroutine?
77 2.16) How can I extract just the unique elements of an array?
78 2.17) How can I call alarm() or usleep() from Perl?
79 2.18) How can I test whether an array contains a certain element?
80 2.19) How can I do an atexit() or setjmp()/longjmp() in Perl?
81 2.20) Why doesn't Perl interpret my octal data octally?
82 2.21) How do I sort an associative array by value instead of by key?
83 2.22) How can I capture STDERR from an external command?
84 2.23) Why doesn't open return an error when a pipe open fails?
85 2.24) How can I compare two date strings?
86 2.25) What's the fastest way to code up a given task in perl?
87 2.26) How can I know how many entries are in an associative array?
88 2.27) Why can't my perl program read from STDIN after I gave it ^D (EOF) ?
89 2.28) Do I always/never have to quote my strings or use semicolons?
90 2.29) How can I translate tildes in a filename?
91 2.30) How can I convert my shell script to Perl?
92 2.31) What is variable suicide and how can I prevent it?
93 2.32) Can I use Perl regular expressions to match balanced text?
94 2.33) Can I use Perl to run a telnet or ftp session?
95 2.34) What does "Malformed command links" mean?
101 A programming language, by Larry Wall <lwall@netlabs.com>.
103 Here's the beginning of the description from the man page:
105 Perl is an interpreted language optimized for scanning arbitrary text
106 files, extracting information from those text files, and printing reports
107 based on that information. It's also a good language for many system
108 management tasks. The language is intended to be practical (easy to use,
109 efficient, complete) rather than beautiful (tiny, elegant, minimal). It
110 combines (in the author's opinion, anyway) some of the best features of C,
111 sed, awk, and sh, so people familiar with those languages should have
112 little difficulty with it. (Language historians will also note some
113 vestiges of csh, Pascal, and even BASIC-PLUS.) Expression syntax
114 corresponds quite closely to C expression syntax. Unlike most Unix
115 utilities, Perl does not arbitrarily limit the size of your data--if
116 you've got the memory, Perl can slurp in your whole file as a single
117 string. Recursion is of unlimited depth. And the hash tables used by
118 associative arrays grow as necessary to prevent degraded performance.
119 Perl uses sophisticated pattern matching techniques to scan large amounts
120 of data very quickly. Although optimized for scanning text, Perl can also
121 deal with binary data, and can make dbm files look like associative arrays
122 (where dbm is available). Setuid Perl scripts are safer than C programs
123 through a dataflow tracing mechanism which prevents many stupid security
124 holes. If you have a problem that would ordinarily use sed or awk or sh,
125 but it exceeds their capabilities or must run a little faster, and you
126 don't want to write the silly thing in C, then Perl may be for you. There
127 are also translators to turn your sed and awk scripts into Perl scripts.
130 1.2) Is Perl hard to learn?
132 No, Perl is easy to learn for two reasons.
134 The first reason is that most of Perl is derived from existing tools
135 and languages, ones that many people who turn to Perl already have
136 some familiarity with. These include the C programming language, the
137 UNIX C library, the UNIX shell, sed, and awk. If you already know
138 these somewhat, Perl should be very easy for you.
140 The second reason that Perl is easy to learn is that you don't have to
141 know every thing there is to know about it in order to get good use
142 out of it. In fact, just a very small subset, mostly borrowed from C,
143 the shell, and sed, will be enough for most tasks. As you feel the
144 need or desire to use more sophisticated features (such as C
145 structures or networking), you can learn these as you go. The
146 learning curve for Perl is not a steep one, especially if you have
147 the headstart of having a background in UNIX. Rather, its learning
148 curve is gentle and gradual, but it *is* admittedly rather long.
150 If you don't know C or UNIX at all, it'll be a steeper learning curve,
151 but what you then learn from Perl will carry over into other areas,
152 like using the C library, UNIX system call, regular expressions, and
153 associative arrays, just to name a few. To know Perl is to know
154 UNIX, and vice versa.
157 1.3) Should I program everything in Perl?
159 Of course not. You should choose the appropriate tool for the task at
160 hand. While it's true that the answer to the question "Can I do (some
161 arbitrary task) in Perl?" is almost always "yes", that doesn't mean
162 this is necessarily a good thing to do. For many people, Perl serves
163 as a great replacement for shell programming. For a few people, it
164 also serves as a replacement for most of what they'd do in C. But
165 for some things, Perl just isn't the optimal choice, such as tasks
166 requiring very complex data structures.
169 1.4) Where can I get Perl over the Internet?
171 From any comp.sources.misc archive. Initial sources were posted to
172 Volume 18, Issues 19-54 at patchlevel 3. The Patches 4-10 were posted
173 to Volume 20, Issues 56-62. You can use the archie server
174 (see the alt.sources FAQ in news.answers) for ways to find these.
176 These machines, at the very least, definitely have it available for
179 ftp.uu.net 137.39.1.2
180 archive.cis.ohio-state.edu 128.146.8.52
181 jpl-devvax.jpl.nasa.gov 128.149.1.143
182 ftp.netlabs.com 192.94.48.152
183 prep.ai.mit.edu 18.71.0.38
184 archive.cs.ruu.nl 131.211.80.5 (Europe)
189 1.5) Where can I get Perl via Email?
191 If you are in Europe, you might using the following site. (I'm still
192 looking for a domestic site.) This information thanks to "Henk P.
193 Penning" <henkp@cs.ruu.nl>: One automated fashion is as follows:
195 Email: Send a message to 'mail-server@cs.ruu.nl' containing:
197 path your_email_address
201 The path-line may be omitted if your message contains a normal From:-line.
202 You will receive a help-file and an index of the directory that contains
205 If all else fails, mail to Larry usually suffices.
208 1.6) How can I get Perl via UUCP?
210 You can get it from the site osu-cis; here is the appropriate info,
211 thanks to J Greely <jgreely@cis.ohio-state.edu> or <osu-cis!jgreely>.
215 Get these two files first:
216 osu-cis!~/GNU.how-to-get.
218 Current Perl distribution:
219 osu-cis!~/perl/4.0/kits@10/perl.kitXX.Z (XX=01-37)
220 How to reach osu-cis via uucp(L.sys/Systems file lines):
224 osu-cis Any ACU 19200 1-614-292-5112 in:--in:--in: Uanon
226 # Direct V.32 (MNP 4)
227 # dead, dead, dead...sigh.
229 #osu-cis Any ACU 9600 1-614-292-1153 in:--in:--in: Uanon
231 # Micom port selector, at 1200, 2400, or 9600 bps.
232 # Replace ##'s below with 12, 24, or 96 (both speed and phone number).
234 osu-cis Any ACU ##00 1-614-292-31## "" \r\c Name? osu-cis nected \c GO \d\r\d\r\d\r in:--in:--in:
237 Modify as appropriate for your site, of course, to deal with your
238 local telephone system. There are no limitations concerning the hours
239 of the day you may call.
241 Another possibility is to use UUNET, although they charge you
242 for it. You have been duly warned. Here's the advert:
244 Anonymous Access to UUNET's Source Archives
248 UUNET now provides access to its extensive collection of UNIX
249 related sources to non- subscribers. By calling 1-900-468-7727
250 and using the login "uucp" with no password, anyone may uucp any
251 of UUNET's on line source collection. Callers will be charged 40
252 cents per minute. The charges will appear on their next tele-
255 The file uunet!/info/help contains instructions. The file
256 uunet!/index//ls-lR.Z contains a complete list of the files available
257 and is updated daily. Files ending in Z need to be uncompressed
258 before being used. The file uunet!~/compress.tar is a tar
259 archive containing the C sources for the uncompress program.
261 This service provides a cost effective way of obtaining
262 current releases of sources without having to maintain accounts
263 with UUNET or some other service. All modems connected to the
264 900 number are Telebit T2500 modems. These modems support all
265 standard modem speeds including PEP, V.32 (9600), V.22bis (2400),
266 Bell 212a (1200), and Bell 103 (300). Using PEP or V.32, a 1.5
267 megabyte file such as the GNU C compiler would cost $10 in con-
268 nect charges. The entire 55 megabyte X Window system V11 R4
269 would cost only $370 in connect time. These costs are less than
270 the official tape distribution fees and they are available now
273 UUNET Communications Services
274 3110 Fairview Park Drive, Suite 570
275 Falls Church, VA 22042
276 +1 703 876 5050 (voice)
277 +1 703 876 5059 (fax)
282 1.7) Where can I get more information on Perl?
284 We'll cover five areas here: USENET (where you're probably reading
285 this), publications, the reference guide, examples on the Internet,
286 and Perl instructional courses.
290 You should definitely read the USENET comp.lang.perl newsgrouor
291 mailing list for all sorts of discussions regarding the language,
292 bugs, features, history, humor, and trivia. In this respect, it
293 functions both as a comp.lang.* style newsgroup and also as a user
294 group for the language; in fact, there's a mailing list called
295 ``perl-users'' that is bidirectionally gatewayed to the newsgroup; see
296 question #38 for details. Larry Wall is a very frequent poster here,
297 as well as many (if not most) of the other seasoned Perl programmers.
298 It's the best place for the very latest information on Perl.
302 If you've been dismayed by the ~80-page troffed Perl man page (or is
303 that man treatise?) you should look to ``the Camel Book'', written by
304 Larry and Randal L. Schwartz <merlyn@ora.com>, published as a Nutshell
305 Handbook by O'Reilly & Associates and entitled _Programming Perl_.
306 Besides serving as a reference guide for Perl, it also contains
307 tutorial material and is a great source of examples and cookbook
308 procedures, as well as wit and wisdom, tricks and traps, pranks and
309 pitfalls. The code examples contained therein are available via
310 anonymous FTP from ftp.uu.net in
311 /published/oreilly/nutshell/perl/perl.tar.Z for your retrieval.
312 Corrections and additions to the book can be found in the Perl man
313 page right before the BUGS section under the heading ERRATA AND
316 If you can't find the book in your local technical bookstore, the book
317 may be ordered directly from O'Reilly by calling 1-800-998-9938 if in
318 North America and 1-707-829-0515. Autographed copies are available
319 from TECHbooks by calling 1-503-646-8257 or mailing info@techbook.com.
320 Cost is ~30$US for the regular version, 40$US for the autographed one.
321 The book's ISBN is 0-937175-64-1.
323 Reasonably substantiated rumor has it that there will be another Perl
324 book out pretty soon, this one aimed more at beginners. Look for it
325 from ORA towards the beginning of 93.
327 Larry Wall has published a 3-part article on perl in Unix World
328 (August through October of 1991), and Rob Kolstad also had a 3-parter
329 in Unix Review (May through July of 1990). Tom Christiansen also has
330 a brief overview article in the trade newsletter Unix Technology
331 Advisor from November of 1989. You might also investigate "The Wisdom
332 of Perl" by Gordon Galligher from SunExpert magazine; April 1991
335 The USENIX LISA (Large Installations Systems Adminstration) Conference
336 have for several years now included many papers of tools written in
337 Perl. Old proceedings of these conferences are available; look in
338 your current issue of ";login:" or send mail to office@usenix.org
339 for futher information.
343 For other examples of Perl scripts, look in the Perl source directory in
344 the eg subdirectory. You can also find a good deal of them on
345 tut.cis.ohio-state.edu in the pub/perl/scripts/ subdirectory.
347 Another source for examples, currently only for anonymous FTP, is on
348 convex.com [130.168.1.1]. This contains, amongst other things,
349 a copy of the newsgroup up through Aug 91, a text retrieval database
350 for the newsgroup, a rather old and short troff version of Tom Christiansen's
351 perl tutorial (this was the version presented at Washington DC USENIX),
352 and quite a few of Tom's scripts. You can look at the INDEX file
353 in /pub/perl/INDEX for a list of what's in that directory.
355 The Convex and Ohio State archives are mirrored on uunet
356 in /languages/perl/scripts-{convex,osu}.
360 A nice reference guide by Johan Vromans <jv@mh.nl> is also available;
361 It is distributed in LaTeX (source) and PostScript (ready to
362 print) forms. Obsolete versions may still be available in TeX and troff
363 forms, although these don't print as nicely. The official kit
364 includes both LaTeX and PostScript forms, and can be FTP'd from
365 archive.cs.ruu.nl [131.211.80.5], file /pub/DOC/perlref-4.035.tar.Z.
366 The reference guide comes with the O'Reilly book in a nice, glossy
371 Various technical conferences, including USENIX, SUG, WCSAS, AUUG,
372 FedUnix, and Europen have been sponsoring tutorials of varying lengths
373 on Perl at their system administration and general conferences. You
374 might consider attending one of these. These classes are typically
375 taught by Tom Christiansen <tchrist@usenix.com>, although both Rob
376 Kolstad <kolstad@usenix.org> and Randal Schwartz <merlyn@ora.com> also
377 teach Perl on occasion. Special appearances by Tom, Rob, and/or
378 Randal may also be negotiated. Classes can run from one day up to a
379 week ranging over a wide range of subject matter (most are two or
380 three days), and can include lab time if you want; having lab time
381 with exercises is generally of great benefit. Send us mail if your
382 organization is interested in having a Perl class taught at your site.
385 1.8) Can people who aren't on USENET receive comp.lang.perl as a digest?
387 "Perl-Users" is the mailing list version of the comp.lang.perl
388 newsgroup. If you're not lucky enough to be on USENET you can post to
389 comp.lang.perl by sending to one of the following addresses. Which one
390 will work best for you depends on which nets your site is hooked into.
391 Ask your local network guru if you're not certain.
393 Internet: PERL-USERS@VIRGINIA.EDU
394 Perl-Users@UVAARPA.VIRGINIA.EDU
396 BitNet: Perl@Virginia
398 uucp: ...!uunet!virginia!perl-users
400 The Perl-Users list is bidirectionally gatewayed with the USENET
401 newsgroup comp.lang.perl. This means that VIRGINIA functions as a
402 reflector. All traffic coming in from the non-USENET side is
403 immediately posted to the newsgroup. Postings from the USENET side are
404 periodically digested and mailed out to the Perl-Users mailing list. A
405 digest is created and distributed at least once per day, more often if
408 All requests to be added to or deleted from this list, problems,
409 questions, etc., should be sent to:
411 Internet: Perl-Users-Request@Virginia.EDU
412 Perl-Users-Request@uvaarpa.Virginia.EDU
414 BitNet: Perl-Req@Virginia
416 uucp: ...!uunet!virginia!perl-users-request
418 Coordinator: Marc Rouleau <mer6g@VIRGINIA.EDU>
420 1.9) Are archives of comp.lang.perl available?
422 Yes, although they're poorly organized. You can get them from
423 the host betwixt.cs.caltech.edu (131.215.128.4) in the directory
424 /pub/comp.lang.perl. They are also to uunet in
425 /languages/perl/comp.lang.perl . It contains these things:
427 comp.lang.perl.tar.Z -- the 5M tarchive in MH/news format
428 archives/ -- the unpacked 5M tarchive
429 unviewed/ -- new comp.lang.perl messages
431 These are currently stored in news- or MH-style format; there are
432 subdirectories named things like "arrays", "programs", "taint", and
433 "emacs". Unfortunately, only the first ~1600 or so messages have been
434 so categorized, and we're now up to almost 15000. Furthermore, even
435 this categorization was haphazardly done and contains errors.
437 A more sophisticated query and retrieval mechanism is desirable.
438 Preferably one that allows you to retrieve article using a fast-access
439 indices, keyed on at least author, date, subject, thread (as in "trn")
440 and probably keywords. Right now, the MH pick command works for this,
441 but it is very slow to select on 15000 articles.
443 If you're serious about this, your best bet is probably to retrieve
444 the compressed tarchive and play with what you get. Any suggestions
445 how to better sort this all out are extremely welcome.
447 Currently the comp.lang.perl archives on convex.com are nearly a year
448 behind. That's because I no longer have room to store them there. I
449 do have them all on-line still, but they are not publicly accessible.
450 If you have a special request for a query on the old newsgroup
451 postings, and make nice noises in my direction, I can run the query
452 and send them to you. Algebraic queries are like "find me anything
453 about this and that and the other thing but not this or whozits". I
454 hope to put this in the form of a mailserver. Donated software would
457 The fast text-retrieval query system for this I'm currently using is
458 Liam Quin's excellent lqtext system, available from ftp.toronto.edu
461 Rumor has it that there are WAIS servers out there for comp.lang.perl
462 these days, but I haven't used them.
465 1.10) How do I get Perl to run on machine FOO?
467 Perl comes with an elaborate auto-configuration script that allows Perl
468 to be painlessly ported to a wide variety of platforms, including many
469 non-UNIX ones. Amiga and MS-DOS binaries are available on
470 jpl-devvax.jpl.nasa.gov [128.149.1.143] for anonymous FTP. Try to bring
471 Perl up on your machine, and if you have problems, examine the README
472 file carefully, and if all else fails, post to comp.lang.perl;
473 probably someone out there has run into your problem and will be able
476 In particular, since they're so often asked about, here's some information
477 for the MacIntosh from Matthias Ulrich Neeracher <neeri@iis.ethz.ch>:
479 A port of Perl to the Apple Macintosh is available by anonymous
480 ftp to rascal.ics.utexas.edu from the file
481 ~ftp/mac/programming/Perl_402_MPW_CPT_bin .
483 The file is 1.1M and must be transferred in BINARY mode. Please
484 be considerate of RASCAL's users during CDT working hours.
485 (And, no, there is no way to get it by email).
487 For European users, the file should soon appear on lth.se.
489 To make optimal use of all the features of this port, you
490 should have MPW, ToolServer, and 5M of memory. There is also a
491 standalone version included, but it's currently of very limited
494 This package contains all of the sources for compilation with
497 And here's some VMS information from Rao V. Akella
498 <rao@moose.cccs.umn.edu>: (this appears to be an old port)
500 You can pick up Perl for VMS (version 3.0.1.1 patchlevel 4) via
501 anonymous ftp from ftp.pitt.edu [130.49.253.1] in the
502 software/vms/perl subdirectory (there are two files there:
503 perl-pl18.bck and perl-pl4.bck).
505 There is also a v3.018 on info.rz.uni-ulm.de [134.60.1.125] or
506 vms.huji.ac.il [128.139.4.3] in /pub/VMS/misc (information courtesy
507 of Anders Rolff <rolff@scotty.eurokom.ie>).
509 And here is a recent version for MS-DOS from Budi Rahard
510 <rahard@ee.UManitoba.CA>, who says:
512 I am collecting MS-DOS Perl(s) in ftp.ee.umanitoba.ca directory
513 /pub/msdos/perl. Currently I received three versions of Perl v4.019
514 and one of 4.010. (Tommy Thorn <tthorn@daimi.aau.dk> and Len Reed
515 <holos0!lbr@gatech.edu>)
517 There is now a 4.035 for 386 [DOS], Hitoshi Doi <doi@jrd.december.com>
518 port, is available ftp.ee.umanitoba.ca as /pub/msdos/perl/perl386.zoo .
520 Please contact the porters directly in case of questions about
524 1.11) Where can I get (info|inter|ora|sql|syb)perl?
526 Numerous database-oriented extensions to Perl have been written.
527 These amount to using the usub mechanism (see the usub/ subdirectory
528 in the distribution tree) to link in a database library, allowing
529 embedded calls to Informix, Interbase, Oracle, Ingres, and Sybase.
530 There is currently a project underway, organized by Buzz Moschetti
531 <buzz@toxicavenger.bear.com>, to create a higher level interface
532 (DBperl) that will allow you to write your queries in a
533 database-independent fashion. Meanwhile, here are the authors of the
537 -------- ----------- ----------------------------------------
538 Infoperl Informix Kurt Andersen (kurt@hpsdid.sdd.hp.com)
539 Interperl Interbase Buzz Moschetti (buzz@fsrg.bear.com)
540 Oraperl Oracle Kevin Stock (kstock@encore.com)
541 Sqlperl Ingres Ted Lemon (mellon@ncd.com)
542 Sybperl Sybase Michael Peppler (mpeppler@itf.ch)
545 1.12) There's an a2p and an s2p; why isn't there a p2c (perl-to-C)?
547 Because the Pascal people would be upset that we stole their name. :-)
549 The dynamic nature of Perl's do and eval operators (and remember that
550 constructs like s/$mac_donald/$mac_gregor/eieio count as an eval) would
551 make this very difficult. To fully support them, you would have to put
552 the whole Perl interpreter into each compiled version for those scripts
553 using them. This is what undump does right now, if your machine has it.
554 If what you're doing will be faster in C than in Perl, maybe it should
555 have been written in C in the first place. For things that ought to be
556 written in Perl, the interpreter will be just about as fast, because the
557 pattern matching routines won't work any faster linked into a C program.
558 Even in the case of simple Perl programs that don't do any fancy evals, the
559 major gain would be in compiling the control flow tests, with the rest
560 still being a maze of twisty, turny subroutine calls. Since these are not
561 usually the major bottleneck in the program, there's not as much to be
562 gained via compilation as one might think.
565 1.13) Where can I get undump for my machine?
567 The undump program comes from the TeX distribution. If you have TeX, then
568 you may have a working undump. If you don't, and you can't get one,
569 *AND* you have a GNU emacs working on your machine that can clone itself,
570 then you might try taking its unexec() function and compiling Perl with
571 -DUNEXEC, which will make Perl call unexec() instead of abort(). You'll
572 have to add unexec.o to the objects line in the Makefile. If you succeed,
573 post to comp.lang.perl about your experience so others can benefit from it.
576 1.14) Where can I get a perl-mode for emacs?
578 In the perl4.0 source directory, you'll find a directory called
579 "emacs", which contains several files that should help you.
582 1.15) How can I use Perl interactively?
584 The easiest way to do this is to run Perl under its debugger.
585 If you have no program to debug, you can invoke the debugger
586 on an `empty' program like this:
590 (The more positive amongst us prefer "perl -de 1". :-)
592 Now you can type in any legal Perl code, and it will be immediately
593 evaluated. You can also examine the symbol table, get stack
594 backtraces, check variable Values, and if you want to, set
595 breakpoints and do the other things you can do in a symbolic debugger.
598 1.16) Is there a Perl shell?
600 Not really. Perl is a programming language, not a command
601 interpreter. There is a very simple one called "perlsh"
602 included in the Perl source distribution. It just does this:
604 $/ = ''; # set paragraph mode
606 while ($SHcmd = <>) {
608 eval $SHcmd; print $@ || "\n";
609 $SHlinesep = $/; $/ = '';
612 Not very interesting, eh?
614 Daniel Smith <dansmith@autodesk.com> is working on an interactive Perl
615 shell called SoftList. It's currently at version 3.0beta. SoftList
616 3.0 has tcsh-like command line editing, can let you define a file of
617 aliases so that you can run chunks of perl or UNIX commands, and so
618 on. You can send mail to him for further information and availability.
621 1.17) Is there a Perl profiler?
623 While there isn't one included with the perl source distribution,
624 various folks have written packages that allow you to do at least some
625 sort of profiling. The strategy usually includes modifying the perl
626 debugger to handle profiling. Authors of these packages include
628 Wayne Thompson <me@anywhere.EBay.Sun.COM>
629 Ray Lischner <lisch@sysserver1.mentor.com>
630 Kresten Krab Thorup <krab@iesd.auc.dk>
632 The original articles by these folks containing their
633 profilers are available on convex.com in
634 /pub/perl/information/profiling.shar via anon ftp.
637 1.18) Is there a yacc for Perl?
639 Yes!! It's a version of Berkeley yacc that outputs Perl code instead
640 of C code! You can get this from ftp.sterling.com [192.124.9.1] in
641 /local/perl-byacc1.8.1.tar.Z, or send the author mail for details.
644 1.19) How can I use curses with perl?
646 One way is to build a curseperl binary by linking in your C curses
647 library as described in the usub subdirectory of the perl sources.
648 This requires a modicum of work, but it will be reasonably fast
649 since it's all in C (assuming you consider curses reasonably fast. :-)
650 Programs written using this method require the modified curseperl,
651 not vanilla perl, to run. While this is something of a disadvantage,
652 experience indicates that it's better to use curseperl than to
653 try to roll your own using termcap directly.
655 Another possibility is to use Henk Penning's cterm package, a curses
656 emulation library written in perl. cterm is actually a separate
657 program with which you communicate via a pipe. It is available from
658 archive.cs.ruu.nl [131.211.80.5] via anonymous ftp in the directory
659 pub/PERL. You may also acquire the package via email in compressed,
660 uuencoded form by sending a message to mail-server@cs.ruu.nl
661 containing these lines:
664 send PERL/cterm.shar.Z
667 See the question on retrieving perl via mail for more information on
668 how to get retrieve other items of interest from the mail server
672 1.20) How can I use X with Perl?
674 Right now, you have several choices. You can wait for perl5, use
675 the WAFE or STDWIN packages, or try to make your own usub bindings.
677 Perl5 is anticipated to be released with bindings for X, called
678 guiperl. An exciting prototype for this, written by Jon Biggar
679 <jon@netlabs.com>, Larry's *other* brother-in-law and officemate,
680 is already up and running inside of Netlabs. This program addresses
681 the same dynamic gui-building problem space as does tcl/tk.
683 If you can't wait or don't think that guiperl will do what you want,
684 a stab at Motif bindings was begun by Theodore C. Law
685 <TEDLAW@TOROLAB6.VNET.IBM.COM> area. His article about this is
686 on convex.com in /pub/perl/info/motif for anon ftp.
688 STDWIN is a library written by Guido van Rossum <guido@cwi.nl>
689 (author of the Python programming language) that is portable
690 between Mac, Dos and X11. One could write a Perl agent to
691 speak to this STDIN server.
693 WAFE is a package that implements a symbolic interface to the Athena
694 widgets (X11R5). A typical Wafe application consists in our framework
695 of two parts: the front-end (we call it Wafe for Widget[Athena]front
696 end) and an application program running typically as separate process.
697 The application program can be implemented in an arbitrary programming
698 language and talks to the front-end via stdio. Since Wafe (the
699 front-end) was developed using the extensible TCL shell (cite John
700 Ousterhout), an application program can dynamically submit requests to
701 the front-end to build up the graphical user interface; the
702 application can even down-load application specific procedures into
703 the front-end. The distribution contains sample application programs
704 in Perl, GAWK, Prolog, TCL, and C talking to the same Wafe binary.
705 Many of the demo applications are implemented in Perl. Wafe 0.9 can
706 be obtained via anonymous ftp from
707 ftp.wu-wien.ac.at:pub/src/X11/wafe-0.9.tar.Z
708 (for people without name server: the ip address is 137.208.3.5)
711 1.21) What is perl4? What is perl5?
713 The answer to what is perl4 is nearly anything you might otherwise
714 program in shell or C. The answer to what is perl5 is basically
715 Perl: the Next Generation. In fact, it's essentially a complete
716 rewrite of perl from the bottom up, and back again.
718 Larry gave a talk on perl5 at a Bay LISA meeting as well as at the
719 most recent USENIX LISA conference in Long Beach in which he timorously
720 admitted that perl5 might possibly be beta released in early 1993.
721 He enumerated some of the following features. Note that not only have
722 not all these been implemented yet, the ones further down the list
723 might well not get done at all.
725 a faster, tighter, more flexible interpreter
726 very easy GUI Perl applications using X bindings ("guiperl")
727 embeddable Perl code in C code: cc prog.c -lperl
728 multiple coresident perl interpreters:
729 perhaps threading and/or coroutines
730 named argument passing:
731 some_func( OC => $red, TOF => "\f");
733 [a, b, [c, d], e] has 4 elts, the 3rd being itself a list
734 typed pointers and generalized indirection:
735 like @{$aptr} or &{$fptr} or &{ $table[$index] . "func" }().
736 merging of list operator and function calling syntax:
737 split /pat/, $string;
738 subroutines without &'s: myfunc($arg);
739 generalization of dbm binding for assoc arrays to handle
740 any generic fetch/store/open/close/flush package.
741 (thus allowing both dbm and gdbm at once)
742 object oriented programming:
746 dynamic loading of C libraries for systems that can
747 byte-compiled code for speed and maybe security
749 It's tempting to want this stuff soon, since the sooner it comes
750 out the sooner we can all build really cool applications. But the
751 longer Larry works on it, the more items from this list will actually
752 get done, and the more robust the release will be. So let's not
753 ask him about it too often.
756 1.22) How does Perl compare with languages like REXX or TCL?
758 REXX is an interpreted programming language first seen on IBM systems,
759 and TCL is John Ousterhout's embeddable command language. TCL's most
760 intriguing feature for many people is the tcl/tk toolset that allows
761 for interpreted X-based tools.
763 To avoid any flamage, if you really want to know the answer to this
764 question, probably the best thing to do is try to write equivalent
765 code to do a set of tasks. All three have their own newsgroups in
766 which you can learn about (but hopefully not argue about) these
769 To find out more about these or other languages, you might also check
770 out David Muir Sharnoff <muir@tfs.com>'s posting on "Catalog of
771 compilers, interpreters, and other language tools" which he posts to
772 comp.lang.misc, comp.sources.d, comp.archives.admin, and the
773 news.answers newsgroups. It's a comprehensive treatment of many
774 different languages. (Caveat lector: he considers Perl's syntax
775 "unappealing".) This list is archived on convex.com in
776 /pub/perl/info/lang-survey.shar .
779 1.23) Is it a Perl program or a Perl script?
783 Current UNIX parlance holds that anything interpreted
784 is a script, and anything compiled into native machine
785 code is a program. However, others hold that a program
786 is a program is a program: after all, one seldom discusses
787 scripts written in BASIC or LISP. Larry considers it
788 a program if it's set in stone and you can't change it,
789 whereas if you go in and hack on it, then it's a script.
791 But doesn't really matter. The terms are generally
792 interchangeable today.
795 1.24) What's the difference between "Perl" and "perl"?
797 32 :-) [ ord('p') - ord('P') ]
799 Larry now uses "Perl" to signify the language proper and "perl" the
800 implementation of it, i.e. the current interpreter. Hence my quip
801 that "Nothing but perl can parse Perl."
803 On the other hand, the aesthetic value of casewise parallelism
804 in "awk", "sed", and "perl" as much require the lower-case
805 version as "C", "Pascal", and "Perl" require the
806 upper-case version. It's also easier to type "Perl" in
807 typeset print than to be constantly switching in Courier. :-)
809 In other words, it doesn't matter much, especially if all
810 you're doing is hearing someone talk about the language;
811 case is hard to distingish aurally.
814 1.25) What companies use or ship Perl?
816 At this time, the known list includes at least the following: Convex,
817 Netlabs, BSDI, Integraph, Dell, and Kubota Pacific, although the
818 latter is in /usr/contrib only. Many other companies use Perl
819 internally for purposes of tools development, systems administration,
820 installation scripts, and test suites. Rumor has it that the large
821 workstation vendors (the TLA set) are seriously looking into shipping
822 Perl with their standard systems "soon".
824 People with support contracts with their vendors are actively
825 encouraged to submit enhancement requests that Perl be shipped
826 as part of their standard system. It would, at the very least,
827 reduce the FTP load on the Internet. :-)
829 1.26) Is there commercial, 3rd-party support for Perl?
831 No. Although perl is included in the GNU distribution, at last check,
832 Cygnus does not offer support for it. However, it's unclear whether
833 they've ever been offered sufficient financial incentive to do so.
835 On the other hand, you do have comp.lang.perl as a totally gratis
836 support mechanism. As long as you ask "interesting" questions,
837 you'll probably get plenty of help. :-)
839 1.27) Where can I get a list of the JAPH signature quotes?
841 These are the "just another perl hacker" signatures that
842 some people sign their postings with. About 100 of the
843 of the earlier ones are on convex.com in /pib/perl/info/japh.
845 1.28) Where can I get a list of Larry Wall witticisms?
847 Over a hundred quips by Larry, from postings of his or source code,
848 can be found on convex.com in /pub/perl/info/lwall-quotes.
853 2.1) What are all these $@*%<> signs and how do I know when to use them?
855 Those are type specifiers: $ for scalar values, @ for indexed arrays,
856 and % for hashed arrays. The * means all types of that symbol name
857 and are sometimes used like pointers; the <> are used for inputting
858 a record from a filehandle. See the question on arrays of arrays
859 for more about Perl pointers.
861 Always make sure to use a $ for single values and @ for multiple ones.
862 Thus element 2 of the @foo array is accessed as $foo[2], not @foo[2],
863 which is a list of length one (not a scalar), and is a fairly common
864 novice mistake. Sometimes you can get by with @foo[2], but it's
865 not really doing what you think it's doing for the reason you think
866 it's doing it, which means one of these days, you'll shoot yourself
867 in the foot; ponder for a moment what these will really do:
868 @foo[0] = `cmd args`;
870 Just always say $foo[2] and you'll be happier.
872 This may seem confusing, but try to think of it this way: you use the
873 character of the type which you *want back*. You could use @foo[1..3] for
874 a slice of three elements of @foo, or even @foo{A,B,C} for a slice of
875 of %foo. This is the same as using ($foo[1], $foo[2], $foo[3]) and
876 ($foo{A}, $foo{B}, $foo{C}) respectively. In fact, you can even use
877 lists to subscript arrays and pull out more lists, like @foo[@bar] or
878 @foo{@bar}, where @bar is in both cases presumably a list of subscripts.
880 While there are a few places where you don't actually need these type
881 specifiers, except for files, you should always use them. Note that
882 <FILE> is NOT the type specifier for files; it's the equivalent of awk's
883 getline function, that is, it reads a line from the handle FILE. When
884 doing open, close, and other operations besides the getline function on
885 files, do NOT use the brackets.
889 Which wil be interpreted as
893 If you always quote your strings, you'll avoid this trap.
895 Normally, files are manipulated something like this (with appropriate
896 error checking added if it were production code):
898 open (FILE, ">/tmp/foo.$$");
899 print FILE "string\n";
902 If instead of a filehandle, you use a normal scalar variable with file
903 manipulation functions, this is considered an indirect reference to a
904 filehandle. For example,
909 After the open, these two while loops are equivalent:
914 as are these two statements:
921 while (<$TEST01>) {} # error
923 ^ note spurious dollar sign
925 This is another common novice mistake; often it's assumed that
927 open($foo, "output.$$");
929 will fill in the value of $foo, which was previously undefined.
930 This just isn't so -- you must set $foo to be the name of a valid
931 filehandle before you attempt to open it.
934 2.2) Why don't backticks work as they do in shells?
936 Several reason. One is because backticks do not interpolate within
937 double quotes in Perl as they do in shells.
939 Let's look at two common mistakes:
941 $foo = "$bar is `wc $file`"; # WRONG
943 This should have been:
945 $foo = "$bar is " . `wc $file`;
947 But you'll have an extra newline you might not expect. This
948 does not work as expected:
950 $back = `pwd`; chdir($somewhere); chdir($back); # WRONG
952 Because backticks do not automatically eat trailing or embedded
953 newlines. The chop() function will remove the last character from
954 a string. This should have been:
956 chop($back = `pwd`); chdir($somewhere); chdir($back);
958 You should also be aware that while in the shells, embedding
959 single quotes will protect variables, in Perl, you'll need
960 to escape the dollar signs.
962 Shell: foo=`cmd 'safe $dollar'`
963 Perl: $foo=`cmd 'safe \$dollar'`;
966 2.3) How come Perl operators have different precedence than C operators?
968 Actually, they don't; all C operators have the same precedence in Perl as
969 they do in C. The problem is with a class of functions called list
970 operators, e.g. print, chdir, exec, system, and so on. These are somewhat
971 bizarre in that they have different precedence depending on whether you
972 look on the left or right of them. Basically, they gobble up all things
973 on their right. For example,
975 unlink $foo, "bar", @names, "others";
977 will unlink all those file names. A common mistake is to write:
979 unlink "a_file" || die "snafu";
981 The problem is that this gets interpreted as
983 unlink("a_file" || die "snafu");
985 To avoid this problem, you can always make them look like function calls
986 or use an extra level of parentheses:
988 (unlink "a_file") || die "snafu";
989 unlink("a_file") || die "snafu";
991 Sometimes you actually do care about the return value:
993 unless ($io_ok = print("some", "list")) { }
995 Yes, print() return I/O success. That means
997 $io_ok = print(2+4) * 5;
999 reutrns 5 times whether printing (2+4) succeeded, and
1001 returns the same 5*io_success value and tosses it.
1003 See the Perl man page's section on Precedence for more gory details,
1004 and be sure to use the -w flag to catch things like this.
1007 2.4) How come my converted awk/sed/sh script runs more slowly in Perl?
1009 The natural way to program in those languages may not make for the fastest
1010 Perl code. Notably, the awk-to-perl translator produces sub-optimal code;
1011 see the a2p man page for tweaks you can make.
1013 Two of Perl's strongest points are its associative arrays and its regular
1014 expressions. They can dramatically speed up your code when applied
1015 properly. Recasting your code to use them can help alot.
1017 How complex are your regexps? Deeply nested sub-expressions with {n,m} or
1018 * operators can take a very long time to compute. Don't use ()'s unless
1019 you really need them. Anchor your string to the front if you can.
1021 Something like this:
1022 next unless /^.*%.*$/;
1023 runs more slowly than the equivalent:
1032 runs faster than this:
1033 next if /Mon/ || /Tue/ || /Wed/ || /Thu/ || /Fri/;
1034 which in turn runs faster than this:
1035 next if /Mon|Tue|Wed|Thu|Fri/;
1036 which runs *much* faster than:
1037 next if /(Mon|Tue|Wed|Thu|Fri)/;
1039 There's no need to use /^.*foo.*$/ when /foo/ will do.
1041 Remember that a printf costs more than a simple print.
1043 Don't split() every line if you don't have to.
1045 Another thing to look at is your loops. Are you iterating through
1046 indexed arrays rather than just putting everything into a hashed
1049 @list = ('abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stv');
1051 for $i ($[ .. $#list) {
1052 if ($pattern eq $list[$i]) { $found++; }
1055 First of all, it would be faster to use Perl's foreach mechanism
1056 instead of using subscripts:
1058 foreach $elt (@list) {
1059 if ($pattern eq $elt) { $found++; }
1062 Better yet, this could be sped up dramatically by placing the whole
1063 thing in an associative array like this:
1065 %list = ('abc', 1, 'def', 1, 'ghi', 1, 'jkl', 1,
1066 'mno', 1, 'pqr', 1, 'stv', 1 );
1067 $found += $list{$pattern};
1069 (but put the %list assignment outside of your input loop.)
1071 You should also look at variables in regular expressions, which is
1072 expensive. If the variable to be interpolated doesn't change over the
1073 life of the process, use the /o modifier to tell Perl to compile the
1074 regexp only once, like this:
1082 Finally, if you have a bunch of patterns in a list that you'd like to
1083 compare against, instead of doing this:
1085 @pats = ('_get.*', 'bogus', '_read', '.*exit', '_write');
1086 foreach $pat (@pats) {
1087 if ( $name =~ /^$pat$/ ) {
1093 If you build your code and then eval it, it will be much faster.
1096 @pats = ('_get.*', 'bogus', '_read', '.*exit', '_write');
1101 foreach $pat (@pats) {
1110 print $code if $debugging;
1115 2.5) How can I call my system's unique C functions from Perl?
1117 If these are system calls and you have the syscall() function, then
1118 you're probably in luck -- see the next question. For arbitrary
1119 library functions, it's not quite so straight-forward. While you
1120 can't have a C main and link in Perl routines, if you're
1121 determined, you can extend Perl by linking in your own C routines.
1122 See the usub/ subdirectory in the Perl distribution kit for an example
1123 of doing this to build a Perl that understands curses functions. It's
1124 neither particularly easy nor overly-documented, but it is feasible.
1127 2.6) Where do I get the include files to do ioctl() or syscall()?
1129 These are generated from your system's C include files using the h2ph
1130 script (once called makelib) from the Perl source directory. This will
1131 make files containing subroutine definitions, like &SYS_getitimer, which
1132 you can use as arguments to your function.
1134 You might also look at the h2pl subdirectory in the Perl source for how to
1135 convert these to forms like $SYS_getitimer; there are both advantages and
1136 disadvantages to this. Read the notes in that directory for details.
1138 In both cases, you may well have to fiddle with it to make these work; it
1139 depends how funny-looking your system's C include files happen to be.
1141 If you're trying to get at C structures, then you should take a look
1142 at using c2ph, which uses debugger "stab" entries generated by your
1143 BSD or GNU C compiler to produce machine-independent perl definitions
1144 for the data structures. This allows to you avoid hardcoding
1145 structure layouts, types, padding, or sizes, greatly enhancing
1146 portability. c2ph comes with the perl distribution. On an SCO
1147 system, GCC only has COFF debugging support by default, so you'll have
1148 to build GCC 2.1 with DBX_DEBUGGING_INFO defined, and use -gstabs to
1149 get c2ph to work there.
1151 See the file /pub/perl/info/ch2ph on convex.com via anon ftp
1152 for more traps and tips on this process.
1155 2.7) Why doesn't "local($foo) = <FILE>;" work right?
1157 Well, it does. The thing to remember is that local() provides an array
1158 context, an that the <FILE> syntax in an array context will read all the
1159 lines in a file. To work around this, use:
1164 You can use the scalar() operator to cast the expression into a scalar
1167 local($foo) = scalar(<FILE>);
1170 2.8) How can I detect keyboard input without reading it?
1172 You should check out the Frequently Asked Questions list in
1173 comp.unix.* for things like this: the answer is essentially the same.
1174 It's very system dependent. Here's one solution that works on BSD
1179 vec($rin, fileno(STDIN), 1) = 1;
1180 return $nfd = select($rin,undef,undef,0);
1183 A closely related question is how to input a single character from the
1184 keyboard. Again, this is a system dependent operation. The following
1185 code that may or may not help you:
1187 $BSD = -f '/vmunix';
1189 system "stty cbreak </dev/tty >/dev/tty 2>&1";
1192 system "stty", 'cbreak',
1193 system "stty", 'eol', "\001";
1199 system "stty -cbreak </dev/tty >/dev/tty 2>&1";
1202 system "stty", 'icanon';
1203 system "stty", 'eol', '^@'; # ascii null
1207 You could also handle the stty operations yourself for speed if you're
1208 going to be doing a lot of them. This code works to toggle cbreak
1209 and echo modes on a BSD system:
1211 sub set_cbreak { # &set_cbreak(1) or &set_cbreak(0)
1213 local($sgttyb,@ary);
1214 require 'sys/ioctl.ph';
1215 $sgttyb_t = 'C4 S' unless $sgttyb_t; # c2ph: &sgttyb'typedef()
1217 ioctl(STDIN,&TIOCGETP,$sgttyb) || die "Can't ioctl TIOCGETP: $!";
1219 @ary = unpack($sgttyb_t,$sgttyb);
1224 $ary[4] &= ~&CBREAK;
1227 $sgttyb = pack($sgttyb_t,@ary);
1229 ioctl(STDIN,&TIOCSETP,$sgttyb) || die "Can't ioctl TIOCSETP: $!";
1232 Note that this is one of the few times you actually want to use the
1233 getc() function; it's in general way too expensive to call for normal
1234 I/O. Normally, you just use the <FILE> syntax, or perhaps the read()
1235 or sysread() functions.
1237 For perspectives on more portable solutions, use anon ftp to retrieve
1238 the file /pub/perl/info/keypress from convex.com.
1241 2.9) How can I make an array of arrays or other recursive data types?
1243 Remember that Perl isn't about nested data structures (actually,
1244 perl0 .. perl4 weren't, but maybe perl5 will be, at least
1245 somewhat). It's about flat ones, so if you're trying to do this, you
1246 may be going about it the wrong way or using the wrong tools. You
1247 might try parallel arrays with common subscripts.
1249 But if you're bound and determined, you can use the multi-dimensional
1250 array emulation of $a{'x','y','z'}, or you can make an array of names
1251 of arrays and eval it.
1253 For example, if @name contains a list of names of arrays, you can
1254 get at a the j-th element of the i-th array like so:
1257 $val = eval "\$$ary[$j]";
1261 $val = eval "\$$name[$i][\$j]";
1263 You could also use the type-globbing syntax to make an array of *name
1264 values, which will be more efficient than eval. Here @name hold
1265 a list of pointers, which we'll have to dereference through a temporary
1270 { local(*ary) = $name[$i]; $val = $ary[$j]; }
1272 In fact, you can use this method to make arbitrarily nested data
1273 structures. You really have to want to do this kind of thing
1274 badly to go this far, however, as it is notationally cumbersome.
1276 Let's assume you just simply *have* to have an array of arrays of
1277 arrays. What you do is make an array of pointers to arrays of
1278 pointers, where pointers are *name values described above. You
1279 initialize the outermost array normally, and then you build up your
1280 pointers from there. For example:
1282 @w = ( 'ww' .. 'xx' );
1283 @x = ( 'xx' .. 'yy' );
1284 @y = ( 'yy' .. 'zz' );
1285 @z = ( 'zz' .. 'zzz' );
1292 Now make a couple of array of pointers to these:
1294 @A = ( *w, *x, *y, *z );
1295 @B = ( *ww, *xx, *yy, *zz );
1297 And finally make an array of pointers to these arrays:
1301 To access an element, such as AAA[i][j][k], you must do this:
1303 local(*foo) = $AAA[$i];
1304 local(*bar) = $foo[$j];
1307 Similar manipulations on associative arrays are also feasible.
1309 You could take a look at recurse.pl package posted by Felix Lee
1310 <flee@cs.psu.edu>, which lets you simulate vectors and tables (lists and
1311 associative arrays) by using type glob references and some pretty serious
1314 In C, you're used to creating recursive datatypes for operations
1315 like recursive decent parsing or tree traversal. In Perl, these
1316 algorithms are best implemented using associative arrays. Take an
1317 array called %parent, and build up pointers such that $parent{$person}
1318 is the name of that person's parent. Make sure you remember that
1319 $parent{'adam'} is 'adam'. :-) With a little care, this approach can
1320 be used to implement general graph traversal algorithms as well.
1323 2.10) How can I quote a variable to use in a regexp?
1327 $pattern =~ s/(\W)/\\$1/g;
1329 Now you can freely use /$pattern/ without fear of any unexpected
1330 meta-characters in it throwing off the search. If you don't know
1331 whether a pattern is valid or not, enclose it in an eval to avoid
1332 a fatal run-time error.
1335 2.11) Why do setuid Perl scripts complain about kernel problems?
1339 YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!
1340 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!
1342 is triggered because setuid scripts are inherently insecure due to a
1343 kernel bug. If your system has fixed this bug, you can compile Perl
1344 so that it knows this. Otherwise, create a setuid C program that just
1345 execs Perl with the full name of the script.
1348 2.12) How do I open a pipe both to and from a command?
1350 In general, this is a dangerous move because you can find yourself in a
1351 deadlock situation. It's better to put one end of the pipe to a file.
1354 # first write some_cmd's input into a_file, then
1355 open(CMD, "some_cmd its_args < a_file |");
1358 # or else the other way; run the cmd
1359 open(CMD, "| some_cmd its_args > a_file");
1360 while ($condition) {
1361 print CMD "some output\n";
1362 # other code deleted
1364 close CMD || warn "cmd exited $?";
1367 open(FILE,"a_file");
1370 If you have ptys, you could arrange to run the command on a pty and
1371 avoid the deadlock problem. See the chat2.pl package in the
1372 distributed library for ways to do this.
1374 At the risk of deadlock, it is theoretically possible to use a
1375 fork, two pipe calls, and an exec to manually set up the two-way
1376 pipe. (BSD system may use socketpair() in place of the two pipes,
1377 but this is not as portable.) The open2 library function distributed
1378 with the current perl release will do this for you.
1380 It assumes it's going to talk to something like adb, both writing to
1381 it and reading from it. This is presumably safe because you "know"
1382 that commands like adb will read a line at a time and output a line at
1383 a time. Programs like sort that read their entire input stream first,
1384 however, are quite apt to cause deadlock.
1387 2.13) How can I change the first N letters of a string?
1389 Remember that the substr() function produces an lvalue, that is, it may be
1390 assigned to. Therefore, to change the first character to an S, you could
1393 substr($var,0,1) = 'S';
1395 This assumes that $[ is 0; for a library routine where you can't know $[,
1396 you should use this instead:
1398 substr($var,$[,1) = 'S';
1400 While it would be slower, you could in this case use a substitute:
1404 But this won't work if the string is empty or its first character is a
1405 newline, which "." will never match. So you could use this instead:
1407 $var =~ s/^[^\0]?/S/;
1409 To do things like translation of the first part of a string, use substr,
1412 substr($var, $[, 10) =~ tr/a-z/A-Z/;
1414 If you don't know then length of what to translate, something like
1417 /^(\S+)/ && substr($_,$[,length($1)) =~ tr/a-z/A-Z/;
1419 For some things it's convenient to use the /e switch of the
1420 substitute operator:
1422 s/^(\S+)/($tmp = $1) =~ tr#a-z#A-Z#, $tmp/e
1424 although in this case, it runs more slowly than does the previous example.
1427 2.14) How can I manipulate fixed-record-length files?
1429 The most efficient way is using pack and unpack. This is faster than
1430 using substr. Here is a sample chunk of code to break up and put back
1431 together again some fixed-format input lines, in this case, from ps.
1433 # sample input line:
1434 # 15158 p5 T 0:00 perl /mnt/tchrist/scripts/now-what
1435 $ps_t = 'A6 A4 A7 A5 A*';
1439 ($pid, $tt, $stat, $time, $command) = unpack($ps_t, $_);
1440 for $var ('pid', 'tt', 'stat', 'time', 'command' ) {
1441 print "$var: <", eval "\$$var", ">\n";
1443 print 'line=', pack($ps_t, $pid, $tt, $stat, $time, $command), "\n";
1447 2.15) How can I make a file handle local to a subroutine?
1449 You must use the type-globbing *VAR notation. Here is some code to
1450 cat an include file, calling itself recursively on nested local
1451 include files (i.e. those with #include "file", not #include <file>):
1458 warn "<INCLUDING $name>\n";
1459 if (!open (FILE, $name)) {
1460 warn "can't open $name: $!\n";
1464 if (/^#\s*include "([^"]*)"/) {
1474 2.16) How can I extract just the unique elements of an array?
1476 There are several possible ways, depending on whether the
1477 array is ordered and you wish to preserve the ordering.
1479 a) If @in is sorted, and you want @out to be sorted:
1482 @out = grep($_ ne $prev && (($prev) = $_), @in);
1484 This is nice in that it doesn't use much extra memory,
1485 simulating uniq's behavior of removing only adjacent
1488 b) If you don't know whether @in is sorted:
1491 @out = grep(!$saw{$_}++, @in);
1493 c) Like (b), but @in contains only small integers:
1495 @out = grep(!$saw[$_]++, @in);
1497 d) A way to do (b) without any loops or greps:
1501 @out = sort keys %saw; # remove sort if undesired
1503 e) Like (d), but @in contains only small positive integers:
1510 2.17) How can I call alarm() or usleep() from Perl?
1512 It's available as a built-in as of version 3.038. If you want finer
1513 granularity than 1 second (as usleep() provides) and have itimers and
1514 syscall() on your system, you can use the following. You could also
1517 It takes a floating-point number representing how long to delay until
1518 you get the SIGALRM, and returns a floating- point number representing
1519 how much time was left in the old timer, if any. Note that the C
1520 function uses integers, but this one doesn't mind fractional numbers.
1522 # alarm; send me a SIGALRM in this many seconds (fractions ok)
1523 # tom christiansen <tchrist@convex.com>
1525 require 'syscall.ph';
1526 require 'sys/time.ph';
1529 local($in_timer,$out_timer);
1530 local($isecs, $iusecs, $secs, $usecs);
1532 local($itimer_t) = 'L4'; # should be &itimer'typedef()
1534 $secs = int($ticks);
1535 $usecs = ($ticks - $secs) * 1e6;
1537 $out_timer = pack($itimer_t,0,0,0,0);
1538 $in_timer = pack($itimer_t,0,0,$secs,$usecs);
1540 syscall(&SYS_setitimer, &ITIMER_REAL, $in_timer, $out_timer)
1541 && die "alarm: setitimer syscall failed: $!";
1543 ($isecs, $iusecs, $secs, $usecs) = unpack($itimer_t,$out_timer);
1544 return $secs + ($usecs/1e6);
1548 2.18) How can I test whether an array contains a certain element?
1550 There are several ways to approach this. If you are going to make
1551 this query many times and the values are arbitrary strings, the
1552 fastest way is probably to invert the original array and keep an
1553 associative array lying about whose keys are the first array's values.
1555 @blues = ('turquoise', 'teal', 'lapis lazuli');
1557 for (@blues) { $is_blue{$_} = 1; }
1559 Now you can check whether $is_blue{$some_color}. It might have been
1560 a good idea to keep the blues all in an assoc array in the first place.
1562 If the values are all small integers, you could use a simple
1563 indexed array. This kind of an array will take up less space:
1565 @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
1566 undef @is_tiny_prime;
1567 for (@primes) { $is_tiny_prime[$_] = 1; }
1569 Now you check whether $is_tiny_prime[$some_number].
1571 If the values in question are integers, but instead of strings,
1572 you can save quite a lot of space by using bit strings instead:
1574 @articles = ( 1..10, 150..2000, 2017 );
1576 grep (vec($read,$_,1) = 1, @articles);
1578 Now check whether vec($read,$n,1) is true for some $n.
1581 2.19) How can I do an atexit() or setjmp()/longjmp() in Perl?
1583 Perl's exception-handling mechanism is its eval operator. You
1584 can use eval as setjmp and die as longjmp. Here's an example
1585 of Larry's for timed-out input, which in C is often implemented
1586 using setjmp and longjmp:
1588 $SIG{ALRM} = TIMEOUT;
1589 sub TIMEOUT { die "restart input\n" }
1591 do { eval { &realcode } } while $@ =~ /^restart input/;
1599 Here's an example of Tom's for doing atexit() handling:
1601 sub atexit { push(@_exit_subs, @_) }
1603 sub _cleanup { unlink $tmp }
1605 &atexit('_cleanup');
1607 eval <<'End_Of_Eval'; $here = __LINE__;
1608 # as much code here as you want
1611 $oops = $@; # save error message
1613 # now call his stuff
1614 for (@_exit_subs) { &$_() }
1616 $oops && ($oops =~ s/\(eval\) line (\d+)/$0 .
1617 " line " . ($1+$here)/e, die $oops);
1619 You can register your own routines via the &atexit function now. You
1620 might also want to use the &realcode method of Larry's rather than
1621 embedding all your code in the here-is document. Make sure to leave
1622 via die rather than exit, or write your own &exit routine and call
1623 that instead. In general, it's better for nested routines to exit
1624 via die rather than exit for just this reason.
1626 Eval is also quite useful for testing for system dependent features,
1627 like symlinks, or using a user-input regexp that might otherwise
1631 2.20) Why doesn't Perl interpret my octal data octally?
1633 Perl only understands octal and hex numbers as such when they occur
1634 as constants in your program. If they are read in from somewhere
1635 and assigned, then no automatic conversion takes place. You must
1636 explicitly use oct() or hex() if you want this kind of thing to happen.
1637 Actually, oct() knows to interpret both hex and octal numbers, while
1638 hex only converts hexadecimal ones. For example:
1641 print "What mode would you like? ";
1645 print "You can't really want mode 0!\n";
1651 Without the octal conversion, a requested mode of 755 would turn
1652 into 01363, yielding bizarre file permissions of --wxrw--wt.
1654 If you want something that handles decimal, octal and hex input,
1655 you could follow the suggestion in the man page and use:
1657 $val = oct($val) if $val =~ /^0/;
1659 2.21) How do I sort an associative array by value instead of by key?
1661 You have to declare a sort subroutine to do this. Let's assume
1662 you want an ASCII sort on the values of the associative array %ary.
1663 You could do so this way:
1665 foreach $key (sort by_value keys %ary) {
1666 print $key, '=', $ary{$key}, "\n";
1668 sub by_value { $ary{$a} cmp $ary{$b}; }
1670 If you wanted a descending numeric sort, you could do this:
1672 sub by_value { $ary{$b} <=> $ary{$a}; }
1674 You can also inline your sort function, like this:
1676 foreach $key ( sort { $x{$b} <=> $a{$a} } keys %ary ) {
1677 print $key, '=', $ary{$key}, "\n";
1680 If you wanted a function that didn't have the array name hard-wired
1681 into it, you could so this:
1683 foreach $key (&sort_by_value(*ary)) {
1684 print $key, '=', $ary{$key}, "\n";
1688 sub _by_value { $x{$a} cmp $x{$b}; }
1689 sort _by_value keys %x;
1692 If you want neither an alphabetic nor a numeric sort, then you'll
1693 have to code in your own logic instead of relying on the built-in
1694 signed comparison operators "cmp" and "<=>".
1696 Note that if you're sorting on just a part of the value, such as a
1697 piece you might extract via split, unpack, pattern-matching, or
1698 substr, then rather than performing that operation inside your sort
1699 routine on each call to it, it is significantly more efficient to
1700 build a parallel array of just those portions you're sorting on, sort
1701 the indices of this parallel array, and then to subscript your original
1702 array using the newly sorted indices. This method works on both
1703 regular and associative arrays, since both @ary[@idx] and @ary{@idx}
1704 make sense. See page 245 in the Camel Book on "Sorting an Array by a
1705 Computable Field" for a simple example of this.
1708 2.22) How can I capture STDERR from an external command?
1710 There are three basic ways of running external commands:
1714 open (PIPE, "cmd |");
1716 In the first case, both STDOUT and STDERR will go the same place as
1717 the script's versions of these, unless redirected. You can always put
1718 them where you want them and then read them back when the system
1719 returns. In the second and third cases, you are reading the STDOUT
1720 *only* of your command. If you would like to have merged STDOUT and
1721 STDERR, you can use shell file-descriptor redirection to dup STDERR to
1724 $output = `$cmd 2>&1`;
1725 open (PIPE, "cmd 2>&1 |");
1727 Another possibility is to run STDERR into a file and read the file
1730 $output = `$cmd 2>some_file`;
1731 open (PIPE, "cmd 2>some_file |");
1733 Here's a way to read from both of them and know which descriptor
1734 you got each line from. The trick is to pipe only STDERR through
1735 sed, which then marks each of its lines, and then sends that
1736 back into a merged STDOUT/STDERR stream, from which your Perl program
1737 then reads a line at a time:
1740 "3>&1 (cmd args 2>&1 1>&3 3>&- | sed 's/^/STDERR:/' 3>&-) 3>&- |");
1744 print "line from stderr: ", $_;
1746 print "line from stdout: ", $_;
1750 Be apprised that you *must* use Bourne shell redirection syntax
1751 here, not csh! In fact, you can't even do these things with csh.
1752 For details on how lucky you are that perl's system() and backtick
1753 and pipe opens all use Bourne shell, fetch the file from convex.com
1754 called /pub/csh.whynot -- and you'll be glad that perl's shell
1755 interface is the Bourne shell.
1758 2.23) Why doesn't open return an error when a pipe open fails?
1762 open(TOPIPE, "|bogus_command") || die ...
1763 open(FROMPIPE, "bogus_command|") || die ...
1765 will not fail just for lack of the bogus_command. They'll only
1766 fail if the fork to run them fails, which is seldom the problem.
1768 If you're writing to the TOPIPE, you'll get a SIGPIPE if the child
1769 exits prematurely or doesn't run. If you are reading from the
1770 FROMPIPE, you need to check the close() to see what happened.
1772 If you want an answer sooner than pipe buffering might otherwise
1773 afford you, you can do something like this:
1775 $kid = open (PIPE, "bogus_command |"); # XXX: check defined($kid)
1776 (kill 0, $kid) || die "bogus_command failed";
1778 This works fine if bogus_command doesn't have shell metas in it, but
1779 if it does, the shell may well not have exited before the kill 0. You
1780 could always introduce a delay:
1782 $kid = open (PIPE, "bogus_command </dev/null |");
1784 (kill 0, $kid) || die "bogus_command failed";
1786 but this is sometimes undesirable, and in any event does not guarantee
1787 correct behavior. But it seems slightly better than nothing.
1789 Similar tricks can be played with writable pipes if you don't wish to
1793 2.24) How can I compare two date strings?
1795 If the dates are in an easily parsed, predetermined format, then you
1796 can break them up into their component parts and call &timelocal from
1797 the distributed perl library. If the date strings are in arbitrary
1798 formats, however, it's probably easier to use the getdate program
1799 from the Cnews distribution, since it accepts a wide variety of dates.
1800 Note that in either case the return values you will really be
1801 comparing will be the total time in seconds as return by time().
1803 Here's a getdate function for perl that's not very efficient; you
1804 can do better this by sending it many dates at once or modifying
1805 getdate to behave better on a pipe. Beware the hardcoded pathname.
1810 s/-(\d{4})$/+$1/ || s/\+(\d{4})$/-$1/;
1811 # getdate has broken timezone sign reversal!
1813 $_ = `/usr/local/lib/news/newsbin/getdate '$_'`;
1818 Richard Ohnemus <rick@IMD.Sterling.COM> actually has a getdate.y
1819 for use with the Perl yacc. You can get this from ftp.sterling.com
1820 [192.124.9.1] in /local/perl-byacc1.8.1.tar.Z, or send the author
1824 2.25) What's the fastest way to code up a given task in perl?
1826 Because Perl so lends itself to a variety of different approaches
1827 for any given task, a common question is which is the fastest way
1828 to code a given task. Since some approaches can be dramatically
1829 more efficient that others, it's sometimes worth knowing which is
1830 best. Unfortunately, the implementation that first comes to mind,
1831 perhaps as a direct translation from C or the shell, often yields
1832 suboptimal performance. Not all approaches have the same results
1833 across different hardware and software platforms. Furthermore,
1834 legibility must sometimes be sacrificed for speed.
1836 While an experienced perl programmer can sometimes eye-ball the code
1837 and make an educated guess regarding which way would be fastest,
1838 surprises can still occur. So, in the spirit of perl programming
1839 being an empirical science, the best way to find out which of several
1840 different methods runs the fastest is simply to code them all up and
1841 time them. For example:
1843 $COUNT = 10_000; $| = 1;
1848 for ($i = 0; $i < $COUNT; $i++) {
1852 printf "%8.4fu %8.4fs\n", ($nu - $u), ($ns - $s);
1857 for ($i = 0; $i < $COUNT; $i++) {
1861 printf "%8.4fu %8.4fs\n", ($nu - $u), ($ns - $s);
1863 For more specific tips, see the section on Efficiency in the
1864 ``Other Oddments'' chapter at the end of the Camel Book.
1867 2.26) How can I know how many entries are in an associative array?
1869 While the number of elements in a @foobar array is simply @foobar when
1870 used in a scalar, you can't figure out how many elements are in an
1871 associative array in an analagous fashion. That's because %foobar in
1872 a scalar context returns the ratio (as a string) of number of buckets
1873 filled versus the number allocated. For example, scalar(%ENV) might
1874 return "20/32". While perl could in theory keep a count, this would
1875 break down on associative arrays that have been bound to dbm files.
1877 However, while you can't get a count this way, one thing you *can* use
1878 it for is to determine whether there are any elements whatsoever in
1879 the array, since "if (%table)" is guaranteed to be false if nothing
1880 has ever been stored in it.
1882 So you either have to keep your own count around and increments
1883 it every time you store a new key in the array, or else do it
1884 on the fly when you really care, perhaps like this:
1886 $count++ while each %ENV;
1888 This preceding method will be faster than extracting the
1889 keys into a temporary array to count them.
1891 As of a very recent patch, you can say
1897 2.27) Why can't my perl program read from STDIN after I gave it ^D (EOF) ?
1899 Because some stdio's set error and eof flags that need clearing.
1901 Try keeping around the seekpointer and go there, like this:
1903 seek(LOG, $where, 0);
1905 If that doesn't work, try seeking to a different part of the file and
1906 then back. If that doesn't work, try seeking to a different part of
1907 the file, reading something, and then seeking back. If that doesn't
1908 work, give up on your stdio package and use sysread. You can't call
1909 stdio's clearerr() from Perl, so if you get EINTR from a signal
1910 handler, you're out of luck. Best to just use sysread() from the
1914 2.28) Do I always/never have to quote my strings or use semicolons?
1916 You don't have to quote strings that can't mean anything else
1917 in the language, like identifiers with any upper-case letters
1918 in them. Therefore, it's fine to do this:
1920 $SIG{INT} = Timeout_Routine;
1923 @Days = (Sun, Mon, Tue, Wed, Thu, Fri, Sat, Sun);
1925 but you can't get away with this:
1927 $foo{while} = until;
1931 $foo{'while'} = 'until';
1933 The requirements on semicolons have been increasingly relaxed. You no
1934 longer need one at the end of a block, but stylistically, you're
1935 better to use them if you don't put the curly brace on the same line:
1937 for (1..10) { print }
1941 @nlist = sort { $a <=> $b } @olist;
1943 but you probably shouldn't do this:
1945 for ($i = 0; $i < @a; $i++) {
1946 print "i is $i\n" # <-- oops!
1949 because you might want to add lines later, and anyway,
1953 2.29) How can I translate tildes in a filename?
1955 Perl doesn't expand tildes -- the shell (ok, some shells) do.
1956 The classic request is to be able to do something like:
1958 open(FILE, "~/dir1/file1");
1959 open(FILE, "~tchrist/dir1/file1");
1961 which doesn't work. (And you don't know it, because you
1962 did a system call without an "|| die" clause! :-)
1964 If you *know* you're on a system with the csh, and you *know*
1965 that Larry hasn't internalized file globbing, then you could
1968 $filename = <~tchrist/dir1/file1>;
1970 but that's pretty iffy.
1972 A better way is to do the translation yourself, as in:
1974 $filename =~ s#^~(\w+)(/.*)?$#(getpwnam($1))[7].$2#e;
1976 More robust and efficient versions that checked for error conditions,
1977 handed simple ~/blah notation, and cached lookups are all reasonable
1981 2.30) How can I convert my shell script to Perl?
1983 Larry's standard answer for this is to send your script to me (Tom
1984 Christiansen) with appropriate supplications and offerings. :-(
1985 That's because there's no automatic machine translator. Even if you
1986 were, you wouldn't gain a lot, as most of the external programs would
1987 still get called. It's the same problem as blind translation into C:
1988 you're still apt to be bogged down by exec()s. You have to analize
1989 the dataflow and algorithm and rethink it for optimal speedup. It's
1990 not uncommon to see one, two, or even three orders of magnitude of
1991 speed difference between the brute-force and the recoded approaches.
1994 2.31) What is variable suicide and how can I prevent it?
1996 Variable suicide is a nasty sideeffect of dynamic scoping and
1997 the way variables are passed by reference. If you say
2003 local($myvar) = $_[0];
2007 Then you have just clubbered $_[0]! Why this is occurring
2008 is pretty heavy wizardry: the reference to $x stored in
2009 $_[0] was temporarily occluded by the previous local($x)
2010 statement (which, you're recall, occurs at run-time, not
2011 compile-time). The work around is simple, however: declare
2012 your formal parameters first:
2015 local($myvar) = $_[0];
2020 That doesn't help you if you're going to be trying to access
2021 @_ directly after the local()s. In this case, careful use
2022 of the package facility is your only recourse.
2024 Another manifestation of this problem occurs due to the
2025 magical nature of the index variable in a foreach() loop.
2028 print "num begin @num\n";
2029 foreach $m (@num) { &ug }
2030 print "num finish @num\n";
2033 print "m=$m $num[0],$num[1],$num[2],$num[3]\n";
2036 Which prints out the mysterious:
2044 num finish 0 1 2 3 4
2046 What's happening here is that $m is an alias for each
2047 element of @num. Inside &ug, you temporarily change
2048 $m. Well, that means that you've also temporarily
2049 changed whatever $m is an alias to!! The only workaround
2050 is to be careful with global variables, using packages,
2051 and/or just be aware of this potential in foreach() loops.
2054 2.32) Can I use Perl regular expressions to match balanced text?
2056 No, or at least, not by the themselves.
2058 Regexps just aren't powerful enough. Although Perl's patterns aren't
2059 strictly regular because they do backtracking (the \1 notation), you
2060 still can't do it. You need to employ auxiliary logic. A simple
2061 approach would involve keeping a bit of state around, something
2062 vaguely like this (although we don't handle patterns on the same line):
2066 if ($inpat++ > 0) { warn "already saw pat1" }
2070 if (--$inpat < 0) { warn "never saw pat1" }
2075 A rather more elaborate subroutine to pull out balanced and possibly
2076 nested single chars, like ` and ', { and }, or ( and ) can be found
2077 on convex.com in /pub/perl/scripts/pull_quotes.
2080 2.33) Can I use Perl to run a telnet or ftp session?
2082 Sure, you can connect directly to them using sockets, or you can run a
2083 session on a pty. In either case, Randal's chat2 package, which is
2084 distributed with the perl source, will come in handly. It address
2085 much the same problem space as Don Libes's expect package does. Two
2086 examples of using managing an ftp session using chat2 can be found on
2087 convex.com in /pub/perl/scripts/ftp-chat2.shar .
2089 Caveat lector: chat2 is documented only by example, may not run on
2090 System V systems, and is subtly machine dependent both in its ideas
2091 of networking and in pseudottys.
2094 2.34) What does "Malformed command links" mean?
2096 This is a bug in 4.035. While in general it's merely a cosmetic
2097 problem, it often comanifests with a highly undesirable coredumping
2098 problem. Programs known to be affected by the fatal coredump include
2099 plum and pcops. Since perl5 is prety much a total rewrite, we can
2100 count on it being fixed then, but if anyone tracks down the coredump
2101 problem before then, a signifcant portion of the perl world would
2102 rejoice. [Fixed in 4.036--lwall]