home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / internet / faq / englisch / comp.lang.perl < prev    next >
Encoding:
Text File  |  1995-04-11  |  170.2 KB  |  4,382 lines

  1. Archive-name: perl-faq/part0
  2. Version: $Id: perl-intro,v 2.4 1995/01/31 14:44:32 spp Exp spp $
  3. Posting-Frequency: bi-weekly
  4. Change-Log: $Log: perl-intro,v $
  5. # Revision 2.4  1995/01/31  14:44:32  spp
  6. # 12/02/94 -  The recent spread of CDROM and book FAQ collections has made
  7. #             copyright notices necessary.  This document now contains a
  8. #             copyright notice for Tom and myself.
  9. # 01/31/95 -  Added question 5.24, affecting parent environment from script
  10. #
  11.  
  12. Hear ye, hear ye.  This most honourable session on Perl Frequently Asked
  13. Questions is hereby called to order.  Please stand and show your respect
  14. for those that have gone before us.  Let us honour Larry Wall*, the father
  15. of Perl who unselfishly spends vast amounts of time reviewing this document
  16. for veracity and accuracy.  Let us also honour Tom Christiansen* without
  17. whom this document would not exist.  Finally, let us collectively honour
  18. the members of comp.lang.perl who have offered much advice and corrections
  19. to this list.
  20.  
  21. This article details the contents of the most Frequently Asked Questions in
  22. comp.lang.perl, a Usenet newsgroup devoted to the Perl programming language.
  23. There are five pieces following this, the general information questions in
  24. part1, part2 and part3 and the programming ones in part4 and part5.
  25.  
  26. If this is your first exposure to Perl, please read this document and the
  27. perl(1) man page before asking questions in comp.lang.perl.  If you're using
  28. v4 perl, that page contains all you need to know (or at least enough to
  29. get started).  If you're using v5 perl, that page will show you where to
  30. look for specific information.  When we refer to perlmod(1), it means the
  31. "perlmod" man page in section "1" of the manual, just as Foo(3pm), that
  32. means it's the "Foo" man page in section "3pm" (perl modules) of the
  33. library.  The perl install does NOT automatically install the module man
  34. pages for you, however.
  35.  
  36. Hopefully the questions herein are asked enough that considerable net
  37. bandwidth can be saved by looking here before asking.  Also, hopefully
  38. there is enough information contained here that someone who has never
  39. heard of Perl can read this and at least have some sort of idea as to what
  40. Perl is.  If this is your first exposure to Perl, please read this and the
  41. Perl(1) man page before asking questions in comp.lang.perl.
  42.  
  43. Some questions in this group aren't really about Perl, but rather about
  44. system-specific issues.  You might also consult the Most Frequently Asked
  45. Questions list in comp.unix.questions for answers to this type of question.
  46.  
  47. The current version of perl is 5.000, which emerged into the world on 16
  48. October, 1994.  The previous non-beta version was 4.036 (version 4,
  49. patchlevel 36).  Many of these questions were written for perl4, however a
  50. lot of perl5 information has also been added.  Perl5 only features will be
  51. clearly marked as such, so as not to cause confusion for those still using
  52. perl4.  You should upgrade to perl5 as soon as possible though (see below).
  53.  
  54. This list was initially written, and still hacked upon, by Tom
  55. Christiansen*.  However, due to his erratic schedule, it is currently
  56. maintained by Stephen P Potter*.  First person singular pronouns, when not
  57. in quoted postings, generally are Tom talking.
  58.  
  59. This document, and all its parts, are Copyright (c) 1994, Stephen P Potter
  60. and Tom Christiansen, perlfaq@perl.com.  All rights reservered.  Permisson
  61. to distribute this collection, in part or full, via electronic means
  62. (emailed, posted or archived) or printed copy are granted providing that no
  63. charges are involved, reasonable attempt is made to use the most current
  64. version, and all credits and copyright notices are retained.  Requests for
  65. other distribution rights, including incorporation in commercial products,
  66. such as books, magazine articles, or CD-ROMs should be made to
  67. perlfaq@perl.com.
  68.  
  69. This FAQ is archived on ftp.cis.ufl.edu [128.227.100.198] in the file
  70. pub/perl/doc/FAQ, as well as on rtfm.mit.edu [18.181.0.24] in
  71. /pub/usenet/comp.lang.perl.  If you have any suggested additions or
  72. corrections to this article, please send them to <perlfaq@perl.com>.
  73.  
  74.  
  75. Part 1 - General Information and Availability
  76. 1.1) What is Perl?
  77. 1.2) What are perl4 and perl5?
  78. 1.3) What features does perl5 provide over perl4?
  79. 1.4) Where can I get docs on perl5?
  80. 1.5) Will perl5 break my perl4 scripts?
  81. 1.6) When will Perl stabiliize?
  82. 1.7) What's the difference between "perl" and "Perl"?
  83. 1.8) Is it a perl program or a perl script?
  84. 1.9) Is perl difficult to learn?
  85. 1.10) Should I program everything in perl?
  86. 1.11) How does perl compare with other scripting languages, like
  87.       REXX or Tcl?
  88. 1.12) Where can I get perl from over the Internet (FTP)?
  89. 1.13) How can I get perl via Email?
  90. 1.14) How can I get perl via UUCP?
  91. 1.15) Are there other ways of getting perl?
  92. 1.16) Has perl been ported to machine FOO?
  93. 1.17) How do I get perl to compile on Solaris?
  94. 1.18) How do I get perl to compile on NeXTs?
  95. 1.19) What extensions are available from Perl and where can I get them?
  96. 1.20) What is dbperl and where can I get it?
  97. 1.21) Which DBM should I use?
  98. 1.22) Is there an SNMP aware perl?
  99.  
  100.  
  101. Part 2 - Informational Sources
  102. 2.1) Is there a USENET group for perl?
  103. 2.2) Have any books or magazine articles been published about perl?
  104. 2.3) When will the Camel and Llama books be updated?
  105. 2.4) What FTP resources are available?
  106. 2.5) What WWW/gopher resources are available?
  107. 2.6) Can people who don't have access to USENET get comp.lang.perl?
  108. 2.7) Are archives of comp.lang.perl available?
  109. 2.8) Is there a WAIS server for comp.lang.perl?
  110. 2.9) What other sources of information about Perl or training are available?
  111. 2.10) Where can I get training classes on Perl?
  112. 2.11) What companies ship or use perl?
  113. 2.12) Is there commercial, third-party support for perl?
  114. 2.13) What is a JAPH?  What does "Will hack perl for ..." mean?
  115. 2.14) Where can I get a collection of Larry Wall witticisms?
  116. 2.15) What are the known bugs?
  117. 2.16) Where should I post bugs?
  118. 2.17) Where should I post source code?
  119. 2.18) Where can I learn about object-orienting Perl programming?
  120. 2.19) Where can I learn about linking C with Perl? [h2xs]
  121. 2.20) What is perl.com?
  122. 2.21) What do the asterisks (*) throughout the FAQ stand for?
  123.  
  124.  
  125. Part 3 - Programming Aids
  126. 3.1) How do I use perl interactively?
  127. 3.2) Is there a perl profiler?
  128. 3.3) Is there a yacc for perl?
  129. 3.4) Is there a pretty printer for perl?
  130. 3.5) There's an a2p and an s2p; why isn't there a p2c (perl to C)?
  131. 3.6) Where can I get a perl mode for emacs?
  132. 3.7) Is there a perl shell?
  133. 3.8) How can I use curses with perl?
  134. 3.9) How can I use X with perl?
  135. 3.10) Can I dynamically load C user routines?
  136. 3.11) What is undump and where can I get it?
  137. 3.12) How can I get '#!perl' to work under MS-DOS?
  138.  
  139.  
  140. Part 4 - General Programming, Regular Expressions (Regexp) and I/O
  141. 4.1) What are all these $@%*<> signs and how do I know when to use them?
  142. 4.2) Why do perl operators have different precedences than C operators?
  143. 4.3) What's the difference between dynamic and static (lexical) scoping?
  144. 4.4) What's the difference between deep and shallow binding?
  145. 4.5) How can I manipulate fixed-record-length files?
  146. 4.6) How can I make a file handle local to a subroutine?
  147. 4.7) How can I sleep or alarm for under a second?
  148. 4.8) How can I do an atexit() or setjmp()/longjmp() in perl?
  149.       (Exception handling)
  150. 4.9) How can I catch signals in perl?
  151. 4.10) Why doesn't perl interpret my octal data octally?
  152. 4.11) How can I compare two date strings?
  153. 4.12) How can I find the Julian Day?
  154. 4.13) What's the fastest way to code up a given task in perl?
  155. 4.14) Do I always/never have to quote my strings or use semicolons?
  156. 4.15) What is variable suicide and how can I prevent it?
  157. 4.16) What does "Malformed command links" mean?
  158. 4.17) How can I set up a footer format to be used with write()?
  159. 4.18) Why does my perl program keep growing in size?
  160. 4.19) Can I do RPC in perl?
  161. 4.20) Why doesn't my program, which uses sockets, work under Solaris?
  162. 4.21) How can I quote a variable to use in a regexp?
  163. 4.22) How can I change the first N letters of a string?
  164. 4.23) Can I use regular expressions to match balanced text?
  165. 4.24) What does it mean that regexps are greedy?  How do I get around it?
  166. 4.25) How do I use a regular expression to strip C style comments from a
  167.       file?
  168. 4.26) Why doesn't "local($foo) = <FILE>;" work correctly?
  169. 4.27) How can I detect keyboard input without reading it?
  170. 4.28) How can I read a single character from the keyboard under UNIX and
  171.       DOS?
  172. 4.29) How can I get input from the keyboard without it echoing to the
  173.       screen?
  174. 4.30) Is there any easy way to strip blank space from the beginning/end of
  175.       a string?
  176.  
  177.  
  178. Part 5 - Arrays and Shell and External Program Interactions
  179. 5.1) What is the difference between $array[1] and @array[1]?
  180. 5.2) How can I make an array of arrays or other recursive data types?
  181. 5.3) How can I make an array of structures containing various data types?
  182. 5.4) How can I extract just the unique elements of an array?
  183. 5.5) How can I tell whether an array contains a certain element?
  184. 5.6) How can I sort an associative array by value instead of by key?
  185. 5.7) How can I know how many entries are in an associative array?
  186. 5.8) What's the difference between "delete" and "undef" with %arrays?
  187. 5.9) Why don't backticks work as they do in shells?
  188. 5.10) Why does my converted awk/sed/sh script run more slowly in perl?
  189. 5.11) How can I call my system's unique C functions from perl?
  190. 5.12) Where do I get the include files to do ioctl() or syscall()?  [h2ph]
  191. 5.13) Why do setuid perl scripts complain about kernel problems?
  192. 5.14) How can I open a pipe both to and from a command?
  193. 5.15) How can I capture STDERR from an external command?
  194. 5.16) Why doesn't open() return an error when a pipe open fails?
  195. 5.17) Why can't my script read from STDIN after I gave it ^D (EOF)?
  196. 5.18) How can I translate tildes (~) in a filename?
  197. 5.19) How can I convert my shell script to perl?
  198. 5.20) Can I use perl to run a telnet or ftp session?
  199. 5.21) Why do I sometimes get an "Argument list to long" when I use <*>?
  200. 5.22) How do I do a "tail -f" in perl?
  201. 5.23) Is there a way to hide perl's command line from programs such as "ps"?
  202. 5.24) I {changed directory, modified my environment} in a perl script.  How
  203.       come the change disappeared when I exited the script?  How do I get
  204.       my changes to be visible?
  205.  
  206. --
  207. Stephen P Potter        spp@vx.com        Varimetrix Corporation
  208. 2350 Commerce Park Drive, Suite 4                Palm Bay, FL 32905
  209. (407) 676-3222                           CAD/CAM/CAE/Software
  210.  
  211.  
  212.  
  213. Archive-name: perl-faq/part1
  214. Version: $Id: part1,v 2.5 1995/01/31 14:44:47 spp Exp spp $
  215. Posting-Frequency: bi-weekly
  216. Change-Log: $Log: part1,v $
  217. # Revision 2.5  1995/01/31  14:44:47  spp
  218. # 01/23/95 -  Fixed perl5-porters address
  219. # 01/23/95 -  Fixed unknown IP address
  220. # 01/25/95 -  Fixed typos
  221. # 01/30/95 -  Added information on Amiga port
  222. # 01/31/95 -  Reworked section on documentation to include TeXinfo source
  223. #
  224.  
  225.  
  226. This posting contains answers to general information and availability
  227. questions.  The following questions are answered in this posting:
  228.  
  229.  
  230. 1.1) What is Perl?
  231.  
  232.     Perl is a compiled scripting language written by Larry Wall*.
  233.  
  234.     Here's the beginning of the description from the perl(1) man page:
  235.  
  236.     Perl is an interpreted language optimized for scanning arbi-
  237.     trary  text  files,  extracting  information from those text
  238.     files, and printing reports based on that information.  It's
  239.     also  a good language for many system management tasks.  The
  240.     language is intended to be practical  (easy  to  use,  effi-
  241.     cient,  complete)  rather  than  beautiful  (tiny,  elegant,
  242.     minimal).  It combines (in  the  author's  opinion,  anyway)
  243.     some  of the best features of C, sed, awk, and sh, so people
  244.     familiar with those languages should have little  difficulty
  245.     with  it.  (Language historians will also note some vestiges
  246.     of csh, Pascal,  and  even  BASIC-PLUS.)  Expression  syntax
  247.     corresponds  quite  closely  to C expression syntax.  Unlike
  248.         most Unix utilities, perl does  not  arbitrarily  limit  the
  249.     size  of your data--if you've got the memory, perl can slurp
  250.         in your whole file as a  single  string.   Recursion  is  of
  251.     unlimited  depth.   And  the hash tables used by associative
  252.         arrays grow as necessary to  prevent  degraded  performance.
  253.     Perl  uses sophisticated pattern matching techniques to scan
  254.         large amounts of data very quickly.  Although optimized  for
  255.     scanning  text, perl can also deal with binary data, and can
  256.         make dbm files look like associative arrays  (where  dbm  is
  257.     available).   Setuid  perl scripts are safer than C programs
  258.         through a dataflow tracing  mechanism  which  prevents  many
  259.     stupid  security  holes.   If  you have a problem that would
  260.         ordinarily use sed or awk or sh, but it exceeds their  capa-
  261.     bilities  or must run a little faster, and you don't want to
  262.         write the silly thing in C, then perl may be for you.  There
  263.     are  also  translators to turn your sed and awk scripts into
  264.         perl scripts.  OK, enough hype.
  265.  
  266.  
  267. 1.2) What are perl4 and perl5, are there any differences?
  268.  
  269.     Perl4 and perl5 are different versions of the language.  Perl4 was the
  270.     previous release, and perl5 is "Perl: The Next Generation."
  271.     Perl5 is, essentially, a complete rewrite of the perl source code
  272.     from the ground up.  It has been modularized, object oriented,
  273.     tweaked, trimmed, and optimized until it almost doesn't look like
  274.     the old code.  However, the interface is mostly the same, and
  275.     compatibility with previous releases is very high.
  276.  
  277.  
  278. 1.3) What features does perl5 provide over perl4?
  279.  
  280.     If you get the newest source (from any of the main FTP sites), you will
  281.     find a directory full of man pages (possibly to be installed as section
  282.     1p and 3pm) that discuss the differences, new features, old
  283.     incompatibilies and much more.  Here, however, are some highlights as
  284.     to the new feature and old incompatibilites.
  285.  
  286.     * Enhanced Usability:  Perl code can now be written in a much more
  287.     legible style.  Regular expressions have been enhanced to allow
  288.     minimal matches, conditionals, and much more.  Cryptic variable
  289.     names (although still supported) have been aliased to new
  290.         nmemonics, using the "English" module, allowing old scripts to run
  291.         and new scripts to be readable.  Error messages and optional
  292.         warnings are more informative and will catch many common mistakes.
  293.         See the perldiag(1) man page, which contains pithy prose from Larry
  294.         Wall* on each and every possible muttering perl might spout at you.
  295.     * Simplified Grammar:  The new yacc grammar is one half the size of
  296.     the old one.  Many of the arbitrary grammer rules have been
  297.     regularized.  The number of reserved words has been cut by 2/3.
  298.     * Lexical Scoping:  Perl variables may now be declared within a
  299.     lexical scope, similar to C's "auto" variables.  This is a
  300.     great improvement on efficiency and contributes to better
  301.     privacy.  See the my() entry in perlfunc(1).
  302.     * Arbitrarily nested data structures:  Full fledged multidimensional
  303.     arrays.  Any scalar value, including an array element, may now
  304.     contain a reference to any other variable or subroutine.
  305.     Easily created anonymous variables and subroutines.  See
  306.         perlref(1).
  307.     * Modularity and Reusability:  The Perl library is now defined in
  308.     terms of modules which can be easily shared among various
  309.     packages.  Packages can import any or all of a module's
  310.     published interface.  See perlmod(1), perlsub(1), and
  311.         Exporter(3pm).
  312.     * Object-oriented programming:  A package can function as a class.
  313.     Dynamic multiple inheritance and virtual methods are supported
  314.     in a straight-forward manner with little new syntax.  Filehandles
  315.     are now treated as objects.  See perlobj(1), perlmod(1), and
  316.         FileHandle(3pm).
  317.     * Embeddible and Extensible:  Perl can be easily embedded in C/C++
  318.     applications, and can either call or be called by your routines
  319.         through a documented interface.  The XS preprocessor is provided to
  320.         make it easy to glue your C/C++ routines into Perl.  Dynamic
  321.         loading of modules is supported.  See perlapi(1), perlcall(1), and
  322.         DynaLoader(3pm).
  323.     * POSIX compliant:  A major new module is the POSIX module, which
  324.     provides access to all available POSIX routines and definitions.
  325.         Seee POSIX(3pm).
  326.     * Package constructors and destructors:  The new BEGIN and END blocks
  327.         provide means to capture control as a package is being compiled and
  328.         after the program exits.  As a degenerate case, they work just like
  329.         awk's BEGIN and END when you use the -p or -n switches.  See
  330.         perlmod(1).
  331.     * Multiple simultaneous DBM implementations:  A perl program now has
  332.         access to DBM, NDBM, SDBM, GDBM and Berkeley DB files in the same
  333.         script.  The dbmopen interface has been generalized to allow any
  334.         variable to be tied to an object class which defines its access
  335.         methods.  tie/untie now preferable to dbmopen/dbmclose.  See the
  336.         tie() entry in perlfunc(1) and the DB_File(3pm) man pages.
  337.     * Subroutine definitions may now be autoloaded:  The AUTOLOAD mechanism
  338.         allows any arbitrary semantics to undefined subroutine calls.  See
  339.         the section on Autoloading in the perlsub(1) manpage.
  340.     * Regular Expression Enhancements:  Qualifiers may be followed by a "?"
  341.     to signify that they should be non-greedy.  A "?" directly after
  342.     an opening paren indicates non backreference grouping and the next
  343.     character determines the purpose of the match (?:a|b|c) will match
  344.     any of a b or c without producing a backreference, (?=stuff) does
  345.     a non-eating look ahead to assure that the next thing is stuff,
  346.     (?!nonsense) looks ahead to assure that the next thing must not
  347.     be "nonsense".  Embedded whitespace and comments for readability.
  348.     A consistent extensibility mechanism has been added that is
  349.     upwardly compatible with all old regexps.  Variables may now be
  350.     interpolated literally into a pattern with \Q or the quotemeta
  351.     fuction, which works like \U but backwhacks non-alphanumerics.
  352.     New m and s "flags" for pattern matching force multi- or
  353.     single-line matching.  The "s" makes "." match "\n".  \A and
  354.     \Z anchor matches to the beginning and end of a string and ignore
  355.     multiline semantics.  \G matches where the previous m//g or s///g
  356.     left off.
  357.     * The -w (warnings) switch is much more informative.
  358.     * References and Objects (see t/op/ref.t) for examples.
  359.     * => is a synonym for comma and helps group paired arguments, such
  360.     as initializers for associative arrays and named arguments to
  361.     subroutines.
  362.     * All functions, even predeclared subroutines, are treated as list
  363.         operators or unary operators.  Parens are optional.
  364.     * Flattened interpreter:  Compare perl4's eval.c with perl5's pp.c.
  365.     Compare perl4's 900 line interpreter look with perl5's one line.
  366.     * eval is now treated like a subroutine call, meaning (among other
  367.     things) you can return from it.
  368.     * format value lists may be spread over multiple lines with a do {}
  369.     block.
  370.     * flags on the #! line are interpreted even if the script wasn't
  371.     invoked directly.
  372.     * ?: is now an lvalue.
  373.     * list context now propogates to the right side of && and ||, and
  374.     as the 2nd and 3rd arguments of ?:
  375.     * preferred package delimeter now :: rather than '.
  376.     * new "and" and "or" operators, like && and || but with a lower
  377.     precedence than comma, so they work better with list operators.
  378.     * New functions abs(), chr(), uc(), ucfirst(), lc(), and lcfirst()
  379.     * require(number) checks to see that the version is at least that
  380.     version
  381.     * qw//, which is equivalent to split(' ', q//)
  382.     * assignment of a reference to a glob replaces the single element
  383.     of the glob corresponding to the reference type:
  384.         *foo = \$bar, * foo = \&bletch;
  385.     * filehandle methods are supported:
  386.     output_autoflush STDOUT 1;
  387.     * Autoload stubs can now call the replacement subroutine with
  388.     goto &realsub.
  389.     * Subroutines can be defined lazily in any package by declaring
  390.     an AUTOLOAD routine, which will be called if a non-existant
  391.     subroutine is called in that package.
  392.     * "use" and "no" subsume many feautres.  "use Module LIST" is
  393.     short for "BEGIN { require Module; import Module LIST }"
  394.     "no" is identical, except that it calls "unimport" instead.
  395.     "use integer" and variations of "use strict [vars,refs,subs]"
  396.     were implemented through new modules.
  397.  
  398. (Thanks to Tom Christiansen* for this section)
  399.  
  400.  
  401. 1.4) Where can I get docs on perl5?
  402.  
  403.     The complete perl documentation is available with the Perl
  404.     distribution, or can be accessed from the following sites.
  405.     Note that the PerlDoc ps file is 240 pages long!!
  406.  
  407.     Marked Up (HTML) format:
  408.         http://www.metronet.com/0/perlinfo/perl5/manual/perl.html
  409.         http://web.nexor.co.uk/perl/perl.html                    (Europe)
  410.  
  411.     PostScript:
  412.         ftp://ftp.uu.net/languages/perl/PerlDoc.ps.gz
  413.         ftp://prep.ai.mit.edu/pub/gnu/PerlDoc.ps.gz
  414.         ftp://ftp.cbi.tamucc.edu/pub/duff/Perl/PerlDoc.ps.gz
  415.         ftp://www.metronet.com/pub/perlinfo/perl5/manual/PerlDoc.ps.gz
  416.         ftp://ftp.zrz.tu-berlin.de/pub/unix/perl/PerlDoc.ps.gz  (Europe)
  417.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/PerlDoc.ps.gz      (Europe)
  418.         ftp://sungear.mame.mu.oz.au/pub/perl/doc/PerlDoc.ps.gz  (Oz)
  419.  
  420.     TeXinfo (Emacs) Format:
  421.         ftp://www.metronet.com/pub/perl/perl5/manual/perl5-info.tar.gz
  422.  
  423.  
  424. 1.5) Will perl5 break my perl4 scripts?
  425.  
  426.     In general, no.  However, certain bad old practices have become highly
  427.     frowned upon.  The following are the most important of the known
  428.     incompatibilies between perl4 and perl5.  See perltrap(1) for more
  429.     details.
  430.  
  431.     * "@" ***ALWAYS*** interpolate in double quoted strings.  Non-array
  432.     "@"s must be escaped:
  433.         Mail("foo@bar.com") needs to be
  434.         Mail("foo\@bar.com");
  435.     The compiler catches this.
  436.     * "open FILE || die" needs to be "open(FILE) || die".  The compiler
  437.       forgives you for this, but won't stop complaining until you fix it.
  438.     * Barewords are no longer (necessarily) strings: they will actually
  439.       call the function (if it existed when that code was compiled)
  440.       instead of returning a string of that value.  Check your
  441.       signal handlers.  The 'use strict subs' pragma (see strict(3pm))
  442.       will help you with this.
  443.     * "shift @x + 20" needs to be "shift(@x) + 20" because of precedence,
  444.       and likewise "$y = scalar keys %foo + 30" needs to be instead
  445.       "$y = scalar keys(%foo) + 30".
  446.     * The internal symbol table is called %{PACKAGE::} for any given
  447.       package.  It used to be %{_PACKAGE}.
  448.     * You may no longer (attempt to) write to read-only variables, like $1,
  449.       or assign to a substr() past the end of a string.
  450.     * Various deprecated practices elicit warning messages.
  451.  
  452.  
  453. 1.6) When will Perl stabilize?
  454.  
  455. When asked at what point the Perl code would be frozen, Larry answere:
  456.  
  457.     Part of the redesign of Perl is to *allow* us to more or less freeze
  458.     the language itself.  It won't totally freeze, of course, but I think
  459.     the rate of change of the core of the language is asymptotically
  460.     approaching 0.  In fact, as time goes on, now that we have an official
  461.     extension mechanism, some of the things that are currently in the core
  462.     of the language may move out (transparently) as extensions.  This has
  463.     already happened to dbmopen().
  464.  
  465.     I've also been continuously reminding myself of what Henry Spencer
  466.     calls "second system syndrome", in which everything under the sun gets
  467.     added, resulting in a colossal kludge, like OS 360.  You'll find that
  468.     the new features in Perl 5 are all pretty minimalistic.  The
  469.     object-oriented features in particular added only one new piece of
  470.     syntax, a C++-style method call.
  471.  
  472.     : The whole idea
  473.     : Perl is to be a fast text-processing, system-maintenance, zero-startup
  474.     : time language. If it gets to be so large and complicated that it isn't
  475.     : fast-running and easy to use, it won't be to anyone's benefit.
  476.  
  477.     My motto from the start has been, "If it ain't broke, don't fix it."
  478.     I've been trying very hard not to remove those features from Perl that
  479.     make it what it is.  At the same time, a lot of streamlining has gone
  480.     into the syntax.  The new yacc file is about half the size of the old
  481.     one, and the number of official reserved words has been cut by 2/3.
  482.     All built-in functions have been unified (dualified?) as either list
  483.     operators or unary operators.
  484.  
  485.     : I really like a lot of the features in Perl, but in order for Perl to
  486.     : be useful on a long term basis, those features have to stay put. I
  487.     : bought the Camel book less than a year ago and it sounds like within
  488.     : another year it will be obsolete.
  489.  
  490.     The parts of Perl that the Camel book covers have not changed all that
  491.     much.  Most old scripts still run.  Many scripts from Perl version 1.0
  492.     still run.  We'll certainly be revising the Camel, but the new man
  493.     pages are split up such that it's pretty easy to ferret out the new
  494.     info when you want it.
  495.  
  496.     We did break a few misfeatures in going to Perl 5.  It seemed like the
  497.     first and last chance to do so.  There's a list of the
  498.     incompatibilities in the documentation.
  499.  
  500.     : Not only is it a lot of work to recompile Perl
  501.     : on 20+ machines periodically, but it's hard to write scripts that are
  502.     : useful in the long term if the guts of the language keep changing.
  503.     : (And if I keep having to buy new books. I keep hearing about new
  504.     : features of Perl 5 that aren't documented in any of the perl 5
  505.     : documentation that *I* can find.)
  506.  
  507.     I think you'll find a lot of folks who think that 4.036 has been a
  508.     pretty stable platform.
  509.  
  510.     Perl 5 is a special case.  I've been working on it for years.  (This is
  511.     part of the reason 4.036 has been so stable!)  There are many changes,
  512.     most of them for the better, I hope.  I don't expect the transition to
  513.     be without pain.  But that's why I stuck numbered versions out in your
  514.     bin directory, so that you can upgrade piecemeal if you like.  And
  515.     that's why I made the -w switch warn about many of the incompatibilities.
  516.  
  517.     And overriding all that, I've tried to keep it so that you don't have
  518.     to know much about the new stuff to use the old stuff.  You can upgrade
  519.     your *knowledge* piecemeal too.
  520.  
  521.     The extension mechanism is designed to take over most of the
  522.     evolutionary role from now on.  And it's set up so that, if you don't
  523.     have a particular extension, you know it right up at the front.
  524.  
  525.     : Are there any plans to write a Perl compiler? While interpreted Perl
  526.     : is great for many applications, it would also be cool to be able to
  527.     : precompile many scripts. (Yes, I know you can undump things, but
  528.     : undump isn't provided with Perl and I haven't found a copy.) The
  529.     : creation of a perl library and dynamically-loadable modules seems
  530.     : like a step in that direction.
  531.  
  532.     Yes, part of the design of Perl 5 was to make it *possible* to write a
  533.     compiler for it.  It could even be done as an extension module, I
  534.     suppose.  Anyone looking for a master's thesis topic?
  535.  
  536.     In summary, almost every concern that you might think of has already
  537.     been (at least) thought about.  In a perfect world, every concern
  538.     could be addressed perfectly.  But in this world we just have to slog
  539.     through.
  540.  
  541.  
  542. 1.7) What's the difference between "perl" and "Perl"?
  543.  
  544.     32!  [ ord('p') - ord('P') ]  (Shouldn't that be 42, the Answer to the
  545.         Great Question of Life, the Universe, and Everything?  ;)
  546.  
  547.     Larry now uses "Perl" to signify the language proper and "perl" the
  548.     implementation of it, i.e. the current interpreter.  Hence Tom's
  549.     quip that "Nothing but perl can parse Perl."
  550.  
  551.     On the other hand, the aesthetic value of casewise parallelism in
  552.     "awk", "sed", and "perl" as much require the lower-case version as "C",
  553.     "Pascal", and "Perl" require the upper-case version.  It's also easier
  554.     to type "Perl" in typeset print than to be constantly switching in
  555.     Courier. :-)
  556.  
  557.     In other words, it doesn't matter much, especially if all you're doing
  558.     is hearing someone talk about the language; case is hard to distinguish
  559.     aurally.
  560.  
  561.  
  562. 1.8) Is it a perl program or a perl script?
  563.  
  564.     It depends on whether you are talking about the perl binary or
  565.     something that you wrote using perl.  And, actually, even this isn't
  566.     necessarily true.
  567.  
  568.     "Standard" UNIX terminology is (roughly) this:  programs are compiled
  569.     into machine code once and run multiple times, scripts are translated
  570.     (by a program) each time they are used.  However, some say that a
  571.     program is anything written which is executed on a computer system.
  572.     Larry considers it a program if it is set in stone and you can't change
  573.     it, whereas if you can go in and hack at it, it's a script.  Of course,
  574.     if you have the source code, that makes just about anything a
  575.     script.  ;)
  576.  
  577.     In general, it probably doesn't really matter.  The terms are used
  578.     interchangeably.  If you particularly like one or the other, use it.  If
  579.     you want to call yourself a perl programmer, call them programs.  If
  580.     you want to call yourself a perl scripter, call them scripts.  Randal*
  581.     and I (at least) will call them hacks.  (See question 2.10 ;)
  582.  
  583. 1.9) Is perl difficult to learn?
  584.  
  585.     Not at all.  Many people find Perl extremely easy to learn.  There are
  586.     at least three main reasons for this.
  587.  
  588.     The first reason is that most of Perl has been derived from standard
  589.     utilities, tools, and languages that you are (probably) already
  590.     familiar with.  If you have any knowledge of the C programming language
  591.     and standard C library, the Unix Shell, sed and awk, Perl should be
  592.     simple and fun for you to learn.
  593.  
  594.     The second reason that Perl is easy to learn is that you only have to
  595.     know a very small subset of Perl to be able to get useful results.  In
  596.     fact, once you can master
  597.  
  598.         #!/usr/local/bin/perl
  599.      print "Hello, world\n";
  600.  
  601.     you can start writing Perl scripts.  In fact, you will probably never
  602.     have to (or be able to) know everything about Perl.  As you feel the
  603.     need or desire to use more sophisticated features (such as C structures
  604.     or networking), you can learn these as you go.  The learning curve for
  605.     Perl is not a steep one, especially if you have the headstart of having
  606.     a background in UNIX.  Rather, its learning curve is gentle and
  607.     gradual, but it *is* admittedly rather long.
  608.  
  609.     The third reason is that you can get immediate results from your
  610.     scripts.  Unlike a normal compiled language (like C or Pascal, for
  611.     example), you don't have to continually recompile your program every
  612.     time you change one little thing.  Perl allows you to experiment and
  613.     test/debug quickly and easily.  This ease of experimentation flattens
  614.     the learning curve even more.
  615.  
  616.     If you don't know C or UNIX at all, it'll be a steeper learning curve,
  617.     but what you then learn from Perl will carry over into other areas,
  618.     like using the C library, UNIX system calls, regular expressions, and
  619.     associative arrays, just to name a few.  To know Perl is to know UNIX,
  620.     and vice versa.
  621.  
  622.  
  623. 1.10) Should I program everything in Perl?
  624.  
  625.     Most definitely.  In fact, you should delete the binaries for sed, awk,
  626.     cc, gcc, grep, rm, ls, cat... well, just delete your /bin directory.
  627.  
  628.     But seriously, of course you shouldn't.  As with any job, you should
  629.     use the appropriate tool for the task at hand.  Just because a hammer
  630.     will put screws into a piece of board, you probably don't want to do
  631.     that.
  632.  
  633.     While it's true that the answer to the question "Can I do (some
  634.     arbitrary task) in Perl?" is almost always "yes", that doesn't mean
  635.     this is necessarily a good thing to do.  For many people, Perl serves
  636.     as a great replacement for shell programming.  For a few people, it
  637.     also serves as a replacement for most of what they'd do in C.  But for
  638.     some things, Perl just isn't the optimal choice, such as tasks
  639.     requiring very complex data structures.
  640.  
  641. 1.11) How does Perl compare with other scripting languages, like Tcl, Python
  642.      or REXX?
  643.  
  644.     REXX is an interpreted programming language first seen on IBM systems.
  645.     Python is an interpreted programming language by Guido van Rossum*.
  646.     TCL is John Ousterhout*'s embeddable command language, designed just
  647.     for embedded command extensions, but lately used for larger
  648.     applications.  TCL's most intriguing feature for many people is the
  649.     tcl/tk toolset that allows for interpreted X-based tools.  Others use
  650.     it for its "expect" extension.
  651.  
  652.     To avoid any flamage, if you really want to know the answer to this
  653.     question, probably the best thing to do is try to write equivalent
  654.     code to do a set of tasks.  All three have their own newsgroups in
  655.     which you can learn about (but hopefully not argue about) these
  656.     languages.
  657.  
  658.     To find out more about these or other languages, you might also check
  659.     out David Muir Sharnoff*'s posting "Catalog of Compilers, Interpreters,
  660.     and Other Language Tools" which he posts to comp.lang.misc,
  661.     comp.sources.d, comp.archives.admin, and news.answers newsgroups.  It's
  662.     a comprehensive treatment of many different languages.  (Caveat lector:
  663.     he considers Perl's syntax "unappealing".)
  664.  
  665.  
  666. 1.12) How can I get Perl over the Internet?
  667.  
  668.     Perl is available from any comp.sources.misc archive.  You can use an
  669.     archie server (see the alt.sources FAQ in news.answers) to find these
  670.     if you want.
  671.  
  672.       Version 4:
  673.     Volume    Issues    Patchlevel and Notes
  674.     ------    ------    ------------------------------------------------
  675.       18    19-54    Patchlevel 3, Initial posting.
  676.       20    56-62    Patches 4-10
  677.  
  678.       Version 5:
  679.         Volume    Issues    Patchlevel and Notes
  680.         ------    ------    -----------------------------------------------
  681.           45    64-128    Initial Posting, patchlevel 0.
  682.  
  683.     Since 1993, a number of archives have sprung up specifically for Perl
  684.     and Perl related items.  Larry maintains the official distribution
  685.     site (for both perl4.036 and perl5) at netlabs.  Probably the largest
  686.     archive is at the University of Florida.  In order of probability these
  687.     sites will have the sources.
  688.  
  689.     Site            IP                Directory and notes
  690.     -----------        -------        -------------------------------
  691.     North America:
  692.     ftp.netlabs.com     192.94.48.152   /pub/outgoing/perl[VERSION]/
  693.     ftp.cis.ufl.edu        128.227.100.198 /pub/perl/src/[VERSION]/
  694.         prep.ai.mit.edu        18.71.0.38         /pub/gnu/perl5.000.tar.gz
  695.         ftp.uu.net          192.48.96.9        /languages/perl/perl5.000.tar.gz
  696.         ftp.khoros.unm.edu  198.59.155.28   /pub/perl/perl5.000.tar.gz
  697.         ftp.cbi.tamucc.edu  165.95.1.3        /pub/duff/Perl/perl5.000.tar.gz
  698.     ftp.metronet.com    192.245.137.1   /pub/perl/sources/
  699.         genetics.upenn.edu  128.91.200.37   /perl5/perl5_000.zip
  700.  
  701.     Europe:
  702.         ftp.cs.ruu.nl        131.211.80.17   /pub/PERL/perl5.0/perl5.000.tar.gz
  703.         ftp.funet.fi        128.214.248.6
  704.                             /pub/languages/perl/ports/perl5/perl5.000.tar.gz
  705.         ftp.zrz.tu-berlin.de 130.149.4.40   /pub/unix/perl/perl5.000.tar.gz
  706.         src.doc.ic.ac.uk    146.169.17.5    /packages/perl5/perl5.000.tar.gz
  707.  
  708.     Australia:
  709.         sungear.mame.mu.oz.au 128.250.209.2 /pub/perl/src/5.0/perl5.000.tar.gz
  710.  
  711.     South America (mirror of prep.ai.mit.edu:/pub/gnu):
  712.         ftp.inf.utfsm.cl    146.83.198.3    /pub/gnu/perl5.000.tar.gz
  713.  
  714.     If there is a site in Asia or Japan, please tell us about it.  Thanks!
  715.  
  716.     You can also retrieve perl via non-ftp methods:
  717.  
  718.         http: //src.doc.ic.ac.uk/packages/perl5/perl5.000.tar.gz
  719.         gopher:       //src.doc.ic.ac.uk/0/packages/perl5/perl5.000.tar.gz
  720.  
  721.  
  722. 1.13) How can I get Perl via Email?
  723.  
  724.     The following is a list of known ftpmail sites.  Please attempt to use
  725.     the site closest to you with the ftp archive closest to it.  Many of
  726.     these sites already have perl on them.  For information on how to use
  727.     one of these sites, send email containing the word "help" to the
  728.     address.
  729.  
  730.     United States:
  731.         Massachusetts:    ftpmail@decwrl.dec.com
  732.         New Jersey:        bitftp@pucc.princeton.edu
  733.         North Carolina:    ftpmail@sunsite.unc.edu
  734.  
  735.     Europe/UK:
  736.         Germany:        ftpmail@ftp.uni-stuttgart.de
  737.                 bitftp@vx.gmd.de
  738.         UK:            ftpmail@doc.ic.ac.uk
  739.  
  740.     Australia:        ftpmail@cs.uow.edu.au
  741.  
  742.     Henk P Penning* suggests that if you are in Europe you should try the
  743.     following (if you are in Germany or the UK, you should probably use one
  744.     of the servers listed above):
  745.  
  746.         Email: Send a message to 'mail-server@cs.ruu.nl' containing:
  747.      begin
  748.      path your_email_address
  749.      send help
  750.      send PERL/INDEX
  751.      end
  752.     The path-line may be omitted if your message contains a normal
  753.     From:-line.  You will receive a help-file and an index of the
  754.     directory that contains the Perl stuff.
  755.  
  756.     If all else fails, mail to Larry usually suffices.
  757.  
  758.  
  759. 1.14) How can I get Perl via UUCP?
  760.  
  761.     There currently is no way of getting Perl via UUCP.  If anyone knows of
  762.     a way, please contact me.  The OSU site has discontinued the service.
  763.  
  764.  
  765. 1.15) Are there other ways of getting perl?
  766.  
  767.     Another possibility is to use UUNET, although they charge you for it.
  768.     You have been duly warned.  Here's the advertisement:
  769.  
  770.            Anonymous Access to UUNET's Source Archives
  771.  
  772.                  1-900-GOT-SRCS
  773.  
  774.     UUNET now provides access to its extensive collection of UNIX
  775.     related sources to non- subscribers.  By  calling  1-900-468-7727
  776.     and  using the login "uucp" with no password, anyone may uucp any
  777.     of UUNET's on line source collection.  Callers will be charged 40
  778.     cents  per  minute.   The charges will appear on their next tele-
  779.     phone bill.
  780.  
  781.      The file uunet!/info/help contains instructions.   The  file
  782.     uunet!/index//ls-lR.Z  contains  a  complete  list  of  the files
  783.     available  and is  updated daily.   Files ending  in Z need to be
  784.     uncompressed before being used.  The file uunet!~/compress.tar is
  785.     a tar archive containing the C sources for the uncompress program.
  786.  
  787.      This service provides a  cost  effective  way  of  obtaining
  788.     current  releases  of sources without having to maintain accounts
  789.     with UUNET or some other service.  All modems  connected  to  the
  790.     900  number  are  Telebit T2500 modems.  These modems support all
  791.     standard modem speeds including PEP, V.32 (9600), V.22bis (2400),
  792.     Bell  212a  (1200), and Bell 103 (300).  Using PEP or V.32, a 1.5
  793.     megabyte file such as the GNU C compiler would cost $10  in  con-
  794.     nect  charges.   The  entire  55  megabyte X Window system V11 R4
  795.     would cost only $370 in connect time.  These costs are less  than
  796.     the  official  tape  distribution fees and they are available now
  797.     via modem.
  798.  
  799.               UUNET Communications Services
  800.            3110 Fairview Park Drive, Suite 570
  801.              Falls Church, VA 22042
  802.              +1 703 876 5050 (voice)
  803.               +1 703 876 5059 (fax)
  804.                 info@uunet.uu.net
  805.  
  806.  
  807. 1.16) Has perl been ported to machine FOO?
  808.  
  809.     Perl runs on virtually all Unix machines simply by following the hints
  810.     file and instructions in the Configure script.  This auto-configuration
  811.     script allows Perl to compile on a wide variety of platforms by
  812.     modifying the machine specific parts of the code.  For most Unix
  813.     systems, or VMS systems for v5 perl, no porting is required.  Try to
  814.     compile Perl on your machine.  If you have problems, examine the README
  815.     file carefully.  If all else fails, send a message to comp.lang.perl
  816.     and crosspost to comp.sys.[whatever], there's probably someone out
  817.     there that has already solved your problem and will be able to help you
  818.     out.
  819.  
  820.     Perl4.036 has been ported to many non-Unix systems, although currently
  821.     there are only a few (beta) v5 ports.  All of the following are
  822.     mirrored at ftp.cis.ufl.edu:/pub/perl/src/[OS]/.  The following are the
  823.     (known) official distribution points.  Please contact the porters
  824.     directly (when possible) in case of questions on these ports.
  825.  
  826.     * MS-DOS binaries and source are available at ftp.ee.umanitoba.ca
  827.       [130.179.8.47] in /pub/msdos/perl/perl4
  828.       There are currently half a dozen different ports for MS-DOS.
  829.       BigPerl4 (v4) is perl4.036 compiled with the Watcom C/C++^32
  830.       compiler (32-bit, flat-memory model C compiler) with the
  831.       following features:
  832.         * Up to 32MB of memory can be used.
  833.         * Supports virtual memory.
  834.         * Works under Windows 3.1
  835.         * The perl debugger can be used.
  836.         * Contains Berkeley DB support.  GDBM is no longer supported.
  837.  
  838.       Note that the latest version of BigPerl4 can also be found at
  839.       any SimTel mirror site (ftp.ee.umanitoba.ca does not
  840.       necessarily have the latest version), such as:
  841.  
  842.         ftp://oak.oakland.edu/SimTel/msdos/perl/
  843.  
  844.           A beta-test version of bigperl based on Perl 5.000 can be
  845.           obtained from the following sites:
  846.  
  847.             ftp.einet.net:/pub/perl5
  848.             ftp.khoros.unm.edu:/pub/perl/msdos
  849.             ftp.ee.umanitoba.ca:/pub/msdos/perl/perl5
  850.  
  851.           This beta-test bigperl also contains ported versions of a2p and
  852.           s2p.
  853.  
  854.     * Windows/NT binaries are available from ftp.cis.ufl.edu.  Does
  855.       anyone know the official distribution point?  I got these from
  856.       archive.cis.ohio-state.edu quite awhile back.
  857.  
  858.     * Machintosh binaries and source are available from nic.switch.ch
  859.       [130.59.1.40] in /software/mac/perl.
  860.       Version 4.1.3 is perl4.036 compiled with the MPW C compiler
  861.         * Mac_Perl_413_src.sit.bin        Sources
  862.         * Mac_Perl_413_tool.sit.bin        MPW Tool
  863.         * Mac_Perl_413_appl.sit.bin        Standalone Application
  864.       There is a mailing list for discussing Macintosh Perl.  Contact
  865.       "mpw-perl-request@iis.ee.ethz.ch".
  866.  
  867.       Timothy Murphy* also ported a version of perl to the Macintosh
  868.       using Think C.  It has probably been abandoned in favour of the
  869.       MPW port, but is still available at ftp.maths.tcd.ie
  870.       [134.266.81.10] in the directory /pub/Mac/perl-4.035/.
  871.  
  872.     * OS/2 sources are also available at ftp.cis.ufl.edu in
  873.       /pub/perl/src/os2.  This appears to have been abandoned and added
  874.       to the official distribution.  See the directory os2 in the perl5
  875.       sources.
  876.  
  877.     * VMS systems should be able to build directly from the standard
  878.       distribution.
  879.  
  880.         * Amiga sources are not available from ftp.cis.ufl.edu, but can
  881.           be found at any Aminet archive, notably:
  882.             * ftp://ftp.wustl.edu/pub/aminet/dev/lang/
  883.             * ftp://ftp.uni-erlangen.de/pub/aminet/dev/lang/
  884.             * ftp://ftp.doc.ic.ac.uk/pub/aminet/dev/lang/
  885.             * ftp://ftp.funet.fi/pub/languages/perl/ports/perl4/amiga
  886.  
  887. 1.17) How do I get Perl to compile on Solaris?
  888.  
  889.     The following directions are for perl, version 4.  Perl, version 5,
  890.     should compile more easily.  If not, send mail to The Perl Porters
  891.     Mailing List (perl5-porters@nicoh.com)
  892.  
  893.     John Lees* reports:
  894.  
  895.         I have built perl on Solaris 2.1, 2.2 beta, and 2.2 FCS. Take
  896.     /usr/ucb out of your path and do not use any BSD/UCB libraries.
  897.     Only -lsocket, -lnsl, and -lm are needed. You can use the hint for
  898.     Solaris 2.0, but the one for 2.1 is wrong. Do not use vfork. Do not
  899.     use -I/usr/ucbinclude.  The result works fine for me, but of couse
  900.     does not support a couple of BSDism's.
  901.  
  902.     Casper H.S. Dik* reports
  903.  
  904.         You must remove all the references to /usr/ucblib AND
  905.         /usr/ucbinclude.  And ignore the Solaris_2.1 hints. They are wrong.
  906.         The undefining of vfork() probably has to do with the confusion it
  907.         gives to the compilers.  If you use cc, you mustn't compile
  908.     util.c/tutil.c  with -O.  I only used the following libs: -lsocket
  909.     -lnsl -lm (there is a problem with -lmalloc)
  910.  
  911.     Michael D'Errico* reports:
  912.  
  913.         If you are using Solaris 2.x, the signal handling is broken.  If
  914.     you set up a signal handler such as 'ripper' it will be forgotten
  915.     after the first time the signal is caught.  To fix this, you need
  916.     to recompile Perl.  Just add '#define signal(x,y) sigset((x),(y))'
  917.     after the '#include <signal.h>' directive in each file that it
  918.     occurs, then make it again.
  919.  
  920.  
  921. 1.18) How do I get Perl to compile on a Next?
  922.  
  923.     According to Andreas Koenig*, under NeXTstep 3.2, both perl4.036 and
  924.     perl5.000 compile with the supplied hints file.
  925.  
  926.     However, Bill Eldridge* provides this message to help get perl4.036 on
  927.     NeXTstep 3.0 to work:
  928.  
  929.         To get perl to compile on NeXTs, you need to combine the ANSI
  930.         and BSD headers:
  931.  
  932.         cd /usr/include
  933.         mkdir ansibsd
  934.         cd ansibsd
  935.         ln -s ../ansi
  936.         ln -s ../bsd
  937.  
  938.         Then, follow the configuration instructions for NeXTs, *replacing*
  939.         all mention of -I/usr/include/ansi or -I/usr/include/bsd with
  940.         -I/usr/include/ansibsd.
  941.  
  942.  
  943. 1.19) What extensions are available from Perl and where can I get them?
  944.  
  945.     Some of the more popular extensions include those for windowing,
  946.     graphics, or data base work.  See perlmod(1).
  947.  
  948.     Tk (as in tcl/tk, but sans tcl)
  949.  
  950.       ftp://ftp.cis.ufl.edu/pub/perl/src/tkperl/tkperl5a5.tar.gz
  951.       ftp://ftp.khoros.unm.edu/pub/perl/extensions/tkperl5a5.tar.gz
  952.       ftp://ftp.metronet.com/pub/perlinfo/perl5/tkperl/tkperl5a5.tar.gz
  953.       ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/tkperl5a5.tar.gz
  954.       ftp://black.ox.ac.uk/src/ALPHA/tkperl5a5.tar.gz
  955.  
  956.     Curses (standard C library)
  957.       ftp://ftp.ncsu.edu/pub/math/wsetzer/cursperl5a6.tar.gz
  958.       ftp://ftp.metronet.com/pub/perlinfo/perl5/cursperl5a6.tar.gz
  959.       ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/cursperl5a6.tar.gz
  960.  
  961.     Msql (SQL)
  962.       ftp://ftp.zrz.TU-Berlin.DE/pub/unix/perl/MsqlPerl-a1.tgz
  963.       ftp://ftp.khoros.unm.edu/pub/perl/extensions/MsqlPerl-a1.tgz
  964.       ftp://ftp.metronet.com/pub/perlinfo/perl5/MsqlPerl5-a1.tgz
  965.       ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/MsqlPerl-a1.tar.gz
  966.  
  967.     Sx (Athena & Xlib)
  968.       ftp://ftp.pasteur.fr/pub/Perl/Sx.tar.gz
  969.       ftp://ftp.khoros.unm.edu/pub/perl/extensions/Sx.tar.gz
  970.       ftp://ftp.metronet.com/pub/perlinfo/perl5/Sx.tar.gz
  971.       ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/PerlDoc.ps.gz
  972.  
  973. 1.20) What is dbperl and where can I get it?
  974.  
  975.     Many database-oriented extensions to Perl have been written.
  976.     Basically, these use the usub mechanism (see the usub/ subdirectory) in
  977.     the source distribution) to link in a database library, allowing
  978.     embedded calls to Informix, Ingres, Interbase, Oracle and Sybase.
  979.  
  980.     Here are the authors of the various extensions:
  981.  
  982.     What            Target DB       Who
  983.     --------        -----------     ----------------------------------------
  984.     ?Infoperl       Informix        Kurt Andersen (kurt@hpsdid.sdd.hp.com)
  985.     Ingperl        Ingres        Tim Bunce (timbo@ig.co.uk) and Ted Lemon
  986.     Interperl       Interbase       Buzz Moschetti (buzz@bear.com)
  987.     Isqlperl        Informix        William Hails, bill@tardis.co.uk
  988.     Oraperl         Oracle          Kevin Stock (kstock@Auspex.com)
  989.     Pgperl        Postgres        Igor Metz (metz@iam.unibe.ch)
  990.     *Sqlperl        Ingres          Ted Lemon (mellon@ncd.com)
  991.     Sybperl         Sybase          Michael Peppler (mpeppler@itf.ch)
  992.     Uniperl        Unify 5.0        Rick Wargo (rickers@coe.drexel.edu)
  993.  
  994.     ? Does this one still exist?
  995.     * Sqlperl appears to have been subsumed by Ingperl
  996.  
  997.     Buzz Moschetti* has organized a project to create a higher level
  998.     interface to will allow you to write your queries in a database-
  999.     independent fashion.  If this type of project interests you, send mail
  1000.     to <perldb-interest-request@vix.com> and asked to be placed on the
  1001.     "perldb-interest" mailing lists.
  1002.  
  1003.     Here's a bit of advertising from Buzz:
  1004.  
  1005.     Perl is an interpreted language with powerful string, scalar, and
  1006.     array processing features developed by Larry Wall that "nicely
  1007.     bridges the functionality gap between sh(1) and C."  Since
  1008.     relational DB operations are typically textually oriented, perl is
  1009.     particularly well-suited to manage the data flows.  The C source
  1010.     code, which is available free of charge and runs on many platforms,
  1011.     contains a user-defined function entry point that permits a
  1012.     developer to extend the basic function set of the language.  The
  1013.     DBperl Group seeks to exploit this capability by creating a
  1014.     standardized set of perl function extensions (e.g. db_fetch(),
  1015.     db_attach()) based the SQL model for manipulating a relational DB,
  1016.     thus providing a portable perl interface to a variety of popular
  1017.     RDMS engines including Sybase, Oracle, Ingres, Informix, and
  1018.     Interbase.  In theory, any DB engine that implements a dynamic SQL
  1019.     interpreter in its HLI can be bolted onto the perl front end with
  1020.     predicatable results, although at this time backends exist only for
  1021.     the aforementioned five DB engines.
  1022.  
  1023.     The official archive for DBperl extensions is ftp.demon.co.uk:
  1024.     /pub/perl/db.  It's the home of the evolving DBperl API Specification.
  1025.     Here's an extract from the updated README there:
  1026.  
  1027.     DBI/        The home of the DBI archive. To join the DBI mailing list
  1028.                 send your request to perldb-interest-REQUEST@vix.com
  1029.  
  1030.     DBD/        Database Drivers for the DBI ...
  1031.  
  1032.     Oracle/      By Tim Bunce (not yet ready!)
  1033.     Ingres/      By Tim Bunce (not yet started!)
  1034.  
  1035.     mod/           Other Perl 5 Modules and Extensions ...
  1036.  
  1037.     Sybperl/    By Michael Peppler, mpeppler@itf.ch
  1038.  
  1039.  
  1040.     perl4/         Perl 4 extensions (using the usub C interface)
  1041.  
  1042.        oraperl/   ORACLE 6 & 7  By Kevin Stock (sadly no longer on the net)
  1043.        sybperl/   SYBASE 4      By Michael Peppler, mpeppler@itf.ch
  1044.        ingperl/   INGRES        By Tim Bunce timbo@ig.co.uk and Ted Lemon
  1045.        isqlperl/  INFORMIX      By William Hails, bill@tardis.co.uk
  1046.        interperl/ INTERBASE     By Buzz Moschetti, buzz@bear.com
  1047.        oraperl/   ORACLE 6 & 7  By Kevin Stock (sadly no longer on the net)
  1048.        sybperl/   SYBASE 4      By Michael Peppler, mpeppler@itf.ch
  1049.        ingperl/   INGRES        By Tim Bunce timbo@ig.co.uk and Ted Lemon
  1050.        isqlperl/  INFORMIX      By William Hails, bill@tardis.co.uk
  1051.        interperl/ INTERBASE     By Buzz Moschetti, buzz@bear.com
  1052.        uniperl/   UNIFY 5.0     By Rick Wargo, rickers@coe.drexel.edu
  1053.        pgperl/    POSTGRES      By Igor Metz, metz@iam.unibe.ch
  1054.  
  1055.        btreeperl/ NDBM perl extensions.   By John Conover, john@johncon.com
  1056.        ctreeperl/ C-Tree perl extensions. By John Conover, john@johncon.com
  1057.        duaperl/   X.500 Directory User Agent. By Eric Douglas.
  1058.  
  1059.     scripts/       Perl and shell scripts
  1060.  
  1061.        rdb/       RDB is a perl RDBMS for ASCII files. By Walt Hobbs,
  1062.                     hobbs@rand.org
  1063.        shql/      SHQL is an interactive SQL database engine.  Written as a
  1064.                     shell script, SHQL interprets SQL commands and
  1065.                     manipulates flat files based on those commands. By
  1066.                     Bruce Momjian, root@candle.uucp
  1067.        xbase/     Perl scripts for accessing xBase style files (dBase III)
  1068.  
  1069.  
  1070.    refinfo/       Reference information
  1071.  
  1072.        sqlsyntax/ Yacc and lex syntax and C source code for SQL1 and SQL2
  1073.             from ftp.uu.net:/pub/uunet/published/oreilly/nutshell/yacclex,
  1074.             and a draft SQL3 syntax from Jeff Fried <jfried@informix.com>+
  1075.        formats/   Details of file formats such as Lotus 1-2-3 .WK1
  1076.  
  1077.     There are also a number of non SQL database interfaces for perl
  1078.     available from ftp.demon.co.uk.  These include:
  1079.  
  1080.     Directory    Target System    Authors and notes
  1081.     ---------    -------------    -------------------------------------------
  1082.     btreeperl    NDBM extension    John Conover (john@johncon.com)
  1083.     ctreeperl    CTree extension John Conover (john@johncon.com)
  1084.     duaperl    X.500 DUA    Eric Douglas
  1085.     rdb        RDBMS        Walt Hobbs (hobbs@rand.org)
  1086.     shql    SQL Engine    Bruce Momjian (root@candle.uucp)
  1087.  
  1088.  
  1089. 1.21) Which DBM should I use?
  1090.  
  1091.     As shipped, Perl (version 5) comes with interfaces for several DBM
  1092.     packages (SDBM, old DBM, NDBM, GDBM, Berkeley DBM) that are not supplied
  1093.     but either come with your system are readily accessible via FTP.  SDBM
  1094.     is guaranteed to be there.  For a comparison, see AnyDBM_File(3pm)
  1095.     and DB_File(3pm).
  1096.  
  1097.  
  1098. 1.22) Is there an SNMP aware Perl?
  1099.  
  1100.     snmperl was written by Guy Streeter (streeter@ingr.com), and was
  1101.     posted in late February 1993 to comp.protocols.snmp.  It can be found
  1102.     archived at one of two (known) places:
  1103.  
  1104.     Host liasun3.epfl.ch
  1105.  
  1106.     Location: /pub/net/snmp
  1107.            FILE -rw-rw-r--       3407  Aug 11 1992  snmperl.README
  1108.            FILE -rw-r--r--      17678  Aug 11 1992  snmperl.tar.Z
  1109.  
  1110.     Host ftp.cis.ufl.edu
  1111.     Location: /pub/perl/scripts/snmp
  1112.  
  1113.     Here is the gist of the README:
  1114.  
  1115.     README  $Revision: 2.5 $
  1116.  
  1117.     This directory contains the source code to add callable C subroutines
  1118.     to perl.  The subroutines implement the SNMP functions "get",
  1119.     "getnext", and "set".  They use the freely-distributable SNMP package
  1120.     (version 1.1b) from CMU.
  1121.  
  1122.     USE:
  1123.       There are four subroutines defined in the callable interface:
  1124.     snmp_get, snmp_next, snmp_set, and snmp_error.
  1125.  
  1126.       snmp_get and snmp_next implement the GET and GETNEXT operations,
  1127.     respectively.  The first two calling arguments are the hostname and
  1128.     Community string.  The IP address of the host, as a dotted-quad ASCII
  1129.     string, may be used as the hostname.  The rest of the calling
  1130.     arguments are a list of variables.  See the CMU package documentation
  1131.     for how variables may be specified.
  1132.       snmp_set also takes hostname and Community string as arguments.  The
  1133.     remaining arguments are a list of triples consisting of variable name,
  1134.     variable type, and value.  The variable type is a string, such as
  1135.     "INTEGER" or "IpAddress".
  1136.       snmp_get, snmp_next, and snmp_set return a list containing
  1137.     alternating variables and values.  snmp_get and snmp_next will simply
  1138.     omit non-existent variables on return.  snmp_set will fail completely
  1139.     if one of the specified variables does not exist (or is read-only).
  1140.       snmp_error will return a text string containing some error
  1141.     information about the most recent snmp_get|next|set call, if it had an
  1142.     error.
  1143.  
  1144.     OTHER NOTES:
  1145.       I didn't find all the places where the CMU library writes to stderr
  1146.     or calls exit() directly.
  1147.       The changes I made to mib.c involve the formatting of variable values
  1148.     for return to the caller.  I took out the descriptive prefix so the
  1149.     string contains only the value.
  1150.       Enumerated types are returned as a string containing the symbolic
  1151.     representation followed in parentheses by the numeric.
  1152.  
  1153.     DISTRIBUTION and OWNERSHIP
  1154.       perl and the CMU SNMP package have their own statements.  Read them.
  1155.     The work I've done is free and clear.  Just don't say you wrote it if
  1156.     you didn't, and don't say I wrote it if you change it.
  1157.  
  1158.     Guy Streeter
  1159.     streeter@ingr.com
  1160.     April 1, 1992 (not a joke!)
  1161.  
  1162. --
  1163. Stephen P Potter        spp@vx.com        Varimetrix Corporation
  1164. 2350 Commerce Park Drive, Suite 4                Palm Bay, FL 32905
  1165. (407) 676-3222                           CAD/CAM/CAE/Software
  1166.  
  1167.  
  1168.  
  1169. Archive-name: perl-faq/part2
  1170. Version: $Id: part2,v 2.5 1995/01/31 14:45:00 spp Exp spp $
  1171. Posting-Frequency: bi-weekly
  1172. Change-Log: $Log: part2,v $
  1173. # Revision 2.5  1995/01/31  14:45:00  spp
  1174. # 12/07/94 -  Fleshed out section on bug reports.  Noted that you should make
  1175. #             sure you are using the most recent version available.
  1176. # 01/23/95 -  Fixed perl5-porters address
  1177. # 01/30/05 -  Added Section on "Teach Yourself Perl in 21 Days"
  1178. #
  1179.  
  1180.  
  1181. This posting contains answers to general information questions, mostly of
  1182. about information sources.
  1183.  
  1184. 2.1) Is there a USENET group for Perl?
  1185.  
  1186.     Yes there is: comp.lang.perl.  This group, which currently can get up
  1187.     to 100 messages per day, contains all kinds of discussions about Perl;
  1188.     everything from bug reports to new features to the history to humour
  1189.     and trivia.  This is the best source of information about anything Perl
  1190.     related, especially what's new with Perl5.  Because of it's vast array
  1191.     of topics, it functions as both a comp.lang.* style newsgroup
  1192.     (providing technical information) and also as a rec.* style newsgroup,
  1193.     kind of a support group for Perl addicts (PerlAnon?).
  1194.  
  1195.     Larry is a frequent poster to this group as well as most (all?) of the
  1196.     seasoned Perl programmers.  Questions will be answered by some of the
  1197.     most knowledgable Perl Hackers, often within minutes of a question
  1198.     being posted (give or take distribution times).
  1199.  
  1200.  
  1201. 2.2) Have any books or magazine articles been published about Perl?
  1202.  
  1203.     There have been quite a few books and articles.  The most well known
  1204.     and most useful book for 4.036 and earlier is _Programming Perl_
  1205.     (affectionately known as ``the Camel Book''), written by Larry and
  1206.     Randal Schwartz*, published by O'Reilly & Associates as part of their
  1207.     Nutshell Handbook Series (ISBN: 0-937175-64-1).  Besides serving as a
  1208.     reference guide for Perl, it also contains tutorial material and is a
  1209.     great source of examples and cookbook procedures, as well as wit and
  1210.     wisdom, tricks and traps, pranks and pitfalls.  The code examples
  1211.     contained therein are available via anonymous FTP from ftp.ora.com in
  1212.     /pub/examples/nutshell/programming_perl/perl.tar.Z or at
  1213.     ftp.cis.ufl.edu in /pub/perl/ora/programming_perl.  Corrections and
  1214.     additions to the book can be found in the Perl man page right before
  1215.     the BUGS section under the heading ERRATA AND ADDENDA.
  1216.  
  1217.     A new version of the Camel book, revised and updated for Perl5 is due
  1218.     out sometime around spring of next year.  Until then, the man pages
  1219.     distributed with the perl 5.000 source contain a wealth of information
  1220.     on the new features and old incompatibilities with perl4.036.
  1221.  
  1222.     Also available is ``the Llama Book'', _Learning Perl_ by Randal
  1223.     Schwartz, another Nutshell book.  This book is a collection of some of
  1224.     the best introductory and tutorial information available about Perl.  A
  1225.     definite must for novice Perl users.  The examples and code from this
  1226.     book are available from the same places as the Camel book code.  (ISBN:
  1227.     1-56592-042-2)
  1228.  
  1229.     At this point, the "writing committe" (Larry, Randal, and Tom; aka
  1230.     <perlbook@perl.com>) is looking at:
  1231.  
  1232.     o A heavy rewrite of the Camel to update it to Perl5.  Expect the
  1233.       tutorial section to be cut in deference to the Llama.
  1234.     o A light update of the Llama to eliminate the "I think perl5 will
  1235.       do ..." and a change to "Perl5 will do ..."
  1236.     o A new book, "Learning More Perl" (working title), a sequel to the
  1237.       Llama, covering what wasn't covered in the Llama including Perl5.
  1238.  
  1239.     According to Randal, this is the current priority.  However, he notes
  1240.     that the new Camel won't be out until sometime in the spring of next
  1241.     year, at the earliest.
  1242.  
  1243.     If you can't find these books in your local technical bookstore, they
  1244.     may be ordered directly from O'Reilly by calling 1-800-998-9938 if in
  1245.     North America and 1-707-829-0515 otherwise.
  1246.  
  1247.     Larry routinely carries around a camel stamp to use when autographing
  1248.     copies of his book.  If you can catch him at a conference you can
  1249.     usually get him to sign your book for you.
  1250.  
  1251.     Prentice Hall also has two perl books, either on the shelves or in the
  1252.     works.  The first is ``Perl by Example'' by Ellie Quigley. (385 pages,
  1253.     $26.96, ISBN 0-13-122839-0) A perl tutorial (perl4); every feature is
  1254.     presented via an annotated example and sample output.
  1255.  
  1256.     The second book is called ``Software Engineering with Perl'' by Carl
  1257.     Dichter and Mark Pease.  Randal Schwartz has reviewed the second book
  1258.     (SEwP) and has this to say about it:
  1259.  
  1260.     SWwP is not meant as instruction in the Perl language, but rather
  1261.     as an example of how Perl may be used to assist in the semi-formal
  1262.     software engineering development cycles.  There's a lot of Perl
  1263.     code that's fairly well commented, but most of the book describes
  1264.     software engineering methodologies.  For the perl-challenged,
  1265.     there's a *light* treatment of the language as well, but they refer
  1266.     to the llama and the camel for the real meat.
  1267.  
  1268.     A new book has recently been released by SAMS Publishing.  It is part
  1269.     of their "Teach Yourself [Foo] in 21 Days" series, and is called "Teach
  1270.     Yourself Perl in 21 Days".  ISBN 0-672-30586-0 Price: $29.95, 841
  1271.     Pages.  This book is the first book to have a section devoted to
  1272.     version 5.000.
  1273.  
  1274.     Starting in the March, 1995 edition of ``Unix Review''. Randal
  1275.     Schwartz* will be authoring a bi-monthly Perl column.  No details as
  1276.     yet as to what exactly it will cover, but expect a lot of good
  1277.     information to come out of this.
  1278.  
  1279.     Larry Wall has published a 3-part article on perl in Unix World
  1280.     (August through October of 1991), and Rob Kolstad also had a 3-parter
  1281.     in Unix Review (May through July of 1990).  Tom Christiansen also has
  1282.     a brief overview article in the trade newsletter Unix Technology
  1283.     Advisor from November of 1989.  You might also investigate "The Wisdom
  1284.     of Perl" by Gordon Galligher from SunExpert magazine;  April 1991
  1285.     Volume 2 Number 4.  The Dec 92 Computer Language magazine also
  1286.     contains a cover article on Perl, "Perl: the Programmers Toolbox".
  1287.  
  1288.     Many other articles on Perl have been recently published.  If you
  1289.     have references, especially on-line copies, please mail them to
  1290.     the FAQ maintainer for inclusion is this notice.
  1291.  
  1292.     The USENIX LISA (Large Installations Systems Administration) Conference
  1293.     have for several years now included many papers of tools written in
  1294.     Perl.  Old proceedings of these conferences are available; look in
  1295.     your current issue of ";login:" or send mail to office@usenix.org
  1296.     for further information.
  1297.  
  1298.     Japan seems to be jumping with Perl books.  If you can read japanese
  1299.     here are a few you might be interested in.  Thanks to Jeffrey Friedl*
  1300.     and Ken Lunde* for this list (NOTE: my screen cannot handle japanese
  1301.     characters, so this is all in English for the moment  NOTE2: These
  1302.     books are written in Japanese, these titles are just translations):
  1303.  
  1304.     Title: Welcome to Perl Country    (Perl-no Kuni-he Youkoso)
  1305.     Authors: Kaoru Maeda, Hiroshi Koyama, Yasushi Saito and Arihito
  1306.          Fuse
  1307.         Pages: 268+9                    Publisher: Science Company
  1308.         Pub. Date: April 25, 1993       ISBN: 4-7819-0697-4
  1309.         Price: 2472Y                    Author Email: maeda@src.ricoh.co.jp
  1310.     Comments: Written during the time the Camel book was being
  1311.     translated.  A useful introduction, but uses jperl (Japanese Perl)
  1312.     which is not necessarily compatible.
  1313.  
  1314.     Title: How to Write Perl        (Perl Shohou)
  1315.     Author: Toshiyuki Masui
  1316.         Pages: 352                      Publisher: ASCII Corporation
  1317.         Pub. Date: July 1, 1993            ISBN: 4-7561-0281-6
  1318.         Price: 3200Y                    Author Email: masui@shocsl.sharp.co.jp
  1319.     Comments: More advanced than "Welcome.." and not meant as an
  1320.     introduction.  Uses the standard perl and has examples for handling
  1321.     Japanese text.
  1322.  
  1323.     Title: Introduction to Perl     (Nyuumon Perl)
  1324.     Author: Shinji Kono
  1325.         Pages: 203                      Publisher: ASCII Corporation
  1326.         Date: July 11, 1994             ISBN: 4-7561-0292-1
  1327.         Price: 1800Y                    Author Email: kono@csl.sony.co.jp
  1328.     Comments: Uses the interactive Perl debugger to explain how things
  1329.     work.
  1330.  
  1331.     Title: Perl Programming
  1332.     Authors: L Wall & R Schwartz    Translator: Yoshiyuki Kondo
  1333.         Pages: 637+32                    Publisher: Softbank Corporation
  1334.         Pub. Date: February 28, 1993    ISBN: 4-89052-384-7
  1335.         Price: 4500Y                    Author Email: cond@lsi-j.co.jp
  1336.     Comments: Official Japanese translation of the Camel book,
  1337.         "Programming Perl".  Somewhat laced with translator notes to
  1338.         explain the humour.  The most useful book.  Also includes the Perl
  1339.         Quick Reference -- in Japanese!
  1340.  
  1341.     There is also a German translation of Programming Perl.  It is available
  1342.     from Hanser Verlag:
  1343.  
  1344.     Title: Programmieren in Perl
  1345.     ISBN: 3-446-17257-2
  1346.  
  1347.  
  1348. 2.3) When will the Camel and Llama books be updated?
  1349.  
  1350.     When they do. :-)  Actually, we're looking at having them in print
  1351.     about 6 months from now (for floating values of now :-).  Send the
  1352.     writing committee mail (perlbook@perl.com) mail if you have suggestions.
  1353.  
  1354.  
  1355. 2.4) What FTP resources are available?
  1356.  
  1357.     Since 1993, several ftp sites have sprung up for Perl and Perl related
  1358.     items.  The site with the biggest repository of Perl scripts right now
  1359.     seems to be ftp.cis.ufl.edu [128.227.100.198] in /pub/perl.  The
  1360.     scripts directory has an INDEX with over 400 lines in it, each
  1361.     describing what the script does.  The src directory has sources and/or
  1362.     binaries for a number of different perl ports, including MS-Dos,
  1363.     Macintosh and Windows/NT.  This is maintained by the Computing Staff at
  1364.     UF*.
  1365.  
  1366.     Note:  European users please use the site src.doc.ic.ac.uk
  1367.     [149.169.2.1] in /pub/computing/programming/languages/perl/
  1368.     The link speed would be a lot better for all.  Contact
  1369.     L.McLoughlin@doc.ic.ac.uk for more information.  It is updated
  1370.     daily.
  1371.  
  1372.     There are also a number of other sites.  I'll add more of them as I get
  1373.     information on them.
  1374.     [site maintainers: if you want to add a blurb here, especially if you
  1375.     have something unique, please let me know. -spp]
  1376.  
  1377.  
  1378. 2.5) What WWW/gopher resources are available?
  1379.  
  1380.     The World Wide Web is exploding with new Perl sites all the time.  Some
  1381.     of the more notable ones are:
  1382.     http://www.cis.ufl.edu/perl
  1383.     http://www.metronet.com/1h/perlinfo, which has a great section on
  1384.         Perl5.
  1385.     http://www.eecs.nwu.edu/perl/perl.html
  1386.         http://web.nexor.co.uk/perl/perl.html, a great site for European
  1387.         and UK users.
  1388.  
  1389.  
  1390. 2.6) Can people who don't have access to USENET get comp.lang.perl?
  1391.  
  1392.     "Perl-Users" is the mailing list version of the comp.lang.perl
  1393.     newsgroup.  If you're not lucky enough to be on USENET you can post to
  1394.     comp.lang.perl by sending to one of the following addresses.  Which one
  1395.     will work best for you depends on which nets your site is hooked into.
  1396.     Ask your local network guru if you're not certain.
  1397.  
  1398.     Internet: PERL-USERS@VIRGINIA.EDU
  1399.               Perl-Users@UVAARPA.VIRGINIA.EDU
  1400.  
  1401.     BitNet: Perl@Virginia
  1402.  
  1403.     uucp: ...!uunet!virginia!perl-users
  1404.  
  1405.     The Perl-Users list is bidirectionally gatewayed with the USENET
  1406.     newsgroup comp.lang.perl.  This means that VIRGINIA functions as a
  1407.     reflector.  All traffic coming in from the non-USENET side is
  1408.     immediately posted to the newsgroup.  Postings from the USENET side are
  1409.     periodically digested and mailed out to the Perl-Users mailing list.  A
  1410.     digest is created and distributed at least once per day, more often if
  1411.     traffic warrants.
  1412.  
  1413.     All requests to be added to or deleted from this list, problems,
  1414.     questions, etc., should be sent to:
  1415.  
  1416.     Internet: Perl-Users-Request@Virginia.EDU
  1417.               Perl-Users-Request@uvaarpa.Virginia.EDU
  1418.  
  1419.     BitNet: Perl-Req@Virginia
  1420.  
  1421.     uucp: ...!uunet!virginia!perl-users-request
  1422.  
  1423.     Coordinator: Marc Rouleau <mer6g@VIRGINIA.EDU>
  1424.  
  1425.  
  1426. 2.7) Are archives of comp.lang.perl available?
  1427.  
  1428.     Yes, there are.  ftp.cis.ufl.edu:/pub/perl/comp.lang.perl/monthly has
  1429.     an almost complete collection dating back to 12/89 (missing 08/91
  1430.     through 12/93).  They are kept as one large file for each month.
  1431.  
  1432.     A more sophisticated query and retrieval mechanism is desirable.
  1433.     Preferably one that allows you to retrieve article using a fast-access
  1434.     indices, keyed on at least author, date, subject, thread (as in "trn")
  1435.     and probably keywords.  Right now, the MH pick command works for this,
  1436.     but it is very slow to select on 18000 articles.
  1437.  
  1438.     If you have, or know where I can find, the missing sections, please let
  1439.     perlfaq@perl.com know.
  1440.  
  1441.  
  1442. 2.8) Is there a WAIS server for comp.lang.perl?
  1443.  
  1444.     Yes there is.  Set your WAIS client to
  1445.     archive.orst.edu:9000/comp.lang.perl.  According to their introduction,
  1446.     they have a complete selection from 1989 on.
  1447.  
  1448.     Bill Middleton <wjm@feenix.metronet.com> offers this:
  1449.  
  1450.     "I have setup a perl script retrieval service and WaisSearch here at
  1451.     feenix.  To check it out, just point your gopher at us, and select the
  1452.     appropriate menu option.  The WaisSearch is of the iubio type, which
  1453.     means you can do boolean searching.  Thus you might try something
  1454.     like:
  1455.  
  1456.     caller
  1457.     ioctl and fcntl
  1458.     grep and socket not curses
  1459.  
  1460.     and other things to see examples of how other folks have done this
  1461.     or that.  This service is still under construction, but I'd like to
  1462.     get feedback, if you have some time.
  1463.  
  1464.     There's also a WaisSearch into all the RFC's and some other fairly
  1465.     nifty stuff."
  1466.  
  1467.  
  1468. 2.9) What other sources of information about Perl or training are available?
  1469.  
  1470.     Johan Vromans* created an beautiful reference guide.  The reference
  1471.     guide comes with the Camel book in a nice, glossy format.  The LaTeX
  1472.     (source) and PostScript (ready to print) versions are available for FTP
  1473.     from ftp.cs.ruu.nl:/pub/DOC/perlref-4.036.1.tar.Z in Europe or from
  1474.     ftp.cis.ufl.edu:/pub/perl/doc/perlref-4.036.tar.gz in the United
  1475.     States.  Obsolete versions in TeX or troff may still be available, but
  1476.     these versions don't print as nicely.  See also:
  1477.  
  1478.         [] ftp://ftp.uu.net/languages/perl/perlref-4.036.1.tar.gz
  1479.         [] ftp://ftp.cs.ruu.nl/pub/DOC/perlref-4.036.1.tar.gz
  1480.         [] ftp://ftp.khoros.unm.edu/pub/perl/perlref-4.036.1.tar.gz
  1481.  
  1482.     Johan is working on an update to this document to include perl5
  1483.     functionality, which he hopes to release by Christmas 1994.
  1484.  
  1485.     There is a #Perl channel on IRC (Internet Relay Chat) where Tom and
  1486.     Randal have been known to hang out.  Here you can get immediate answers
  1487.     to questions from some of the most well-known Perl Hackers.
  1488.  
  1489.     The perl5-porters (perl5-porters@nicoh.com) mailing list was created to
  1490.     aid in communication among the people working on perl5.  However, it
  1491.     has overgrown this function and now also handles a good deal of traffic
  1492.     about perl internals.
  1493.  
  1494. 2.10) Where can I get training classes on Perl?
  1495.  
  1496.     USENIX, LISA, SUG, WCSAS, AUUG, FedUnix and Europen sponsor tutorials
  1497.     of varying lengths on Perl at the System Administration and General
  1498.     Conferences.  These public classes are typically taught by Tom
  1499.     Christiansen*.
  1500.  
  1501.     In part, Tom and Randal teach Perl to help keep bread on their tables
  1502.     long enough while they continue their pro bono efforts of documenting
  1503.     perl (Tom keeps writing more man pages for it :-) and expanding the
  1504.     perl toolkit through extension libraries, work which they enjoy doing
  1505.     as it's fun and helps out the whole world, but which really doesn't
  1506.     pay the bills.  Such is the nature of free(ly available) software.
  1507.     Send mail to <perlclasses@perl.com> for details and availability.
  1508.  
  1509.     Tom is also available to teach on-site classes, included courses on
  1510.     advanced perl and perl5.  Classes run anywhere from one day to week
  1511.     long sessions and cover a wide range of subject matter.  Classes can
  1512.     include lab time with exercises, a generally beneficial aspect.  If you
  1513.     would like more information regarding Perl classes or when the next
  1514.     public appearances are, please contact Tom directly.
  1515.  
  1516.     Randal Schwartz* provides a 2-day lecture-only and a 4-5 day lecture-lab
  1517.     course based on his popular book "Learning Perl".  For details, contact
  1518.     Randal directly via email or at 1.503.777.0095.
  1519.  
  1520.     Internet One provides a 2 day "Introduction to Perl"  and  2  day
  1521.     "Advanced Perl" workshop. The 50% hands-on and 50% lecture format
  1522.     allow attendees to write several programs  themselves.   Supplied
  1523.     are  the user manuals, reference copies of Larry Wall's "Program-
  1524.     ming Perl", and a UNIX directory of  all  training  examples  and
  1525.     labs. To obtain outlines, pricing, or scheduling information, use
  1526.     the following:
  1527.  
  1528.         o Phone: 1.303.444.1993
  1529.         o Email: info@InternetOne.COM
  1530.         o See our Ad in the "SysAdmin" magazine
  1531.         o View the outlines via the Web: http://www.InternetOne.COM/
  1532.  
  1533.  
  1534. 2.11) What companies use or ship Perl?
  1535.  
  1536.     At this time, the known list of companies that ship Perl includes at
  1537.     least the following, although some have snuck it into /usr/contrib or
  1538.     its moral equivalent:
  1539.  
  1540.     BSDI
  1541.     Comdisco Systems
  1542.     CONVEX Computer Corporation
  1543.     Crosspoint Solutions
  1544.     Dell
  1545.     DRD Corporation
  1546.     IBM (SP systems)
  1547.     Intergraph
  1548.     Kubota Pacific
  1549.     Netlabs
  1550.     SGI (without taintperl)
  1551.     Univel
  1552.  
  1553.     Furthermore, the following vendors are reported to begin shipping perl
  1554.     standard with their systems in the very near future:
  1555.  
  1556.     Sun
  1557.  
  1558.     Some companies ship it on their "User Contributed Software Tape",
  1559.     such as DEC and HP.  Apple Computer has shipped the MPW version of
  1560.     Macintosh Perl on one of their Developer CDs (Essentials*Tools*Objects
  1561.     #11) (and they included it under "Essentials" :-)
  1562.  
  1563.     Many other companies use Perl internally for purposes of tools
  1564.     development, systems administration, installation scripts, and test
  1565.     suites.  Rumor has it that the large workstation vendors (the TLA set)
  1566.     are seriously looking into shipping Perl with their standard systems
  1567.     "soon".
  1568.  
  1569.     People with support contracts with their vendors are actively
  1570.     encouraged to submit enhancement requests that Perl be shipped
  1571.     as part of their standard system.  It would, at the very least,
  1572.     reduce the FTP load on the Internet. :-)
  1573.  
  1574.     If you know of any others, please send them in.
  1575.  
  1576.  
  1577. 2.12) Is there commercial, third-party support for Perl?
  1578.  
  1579.     Not really.  Although perl is included in the GNU distribution, at last
  1580.     check, Cygnus does not offer support for it.  However, it's unclear
  1581.     whether they've ever been offered sufficient financial incentive to do
  1582.     so.
  1583.  
  1584.     On the other hand, you do have comp.lang.perl as a totally gratis
  1585.     support mechanism.  As long as you ask "interesting" questions, you'll
  1586.     probably get plenty of help. :-)
  1587.  
  1588.     While some vendors do ship Perl with their platforms, that doesn't mean
  1589.     they support it on arbitrary other platforms.  And in fact, all they'll
  1590.     probably do is forward any bug reports on to Larry.  In practice, this
  1591.     is far better support than you could hope for from nearly any vendor.
  1592.  
  1593.     The companies who won't use something unless they can pay money for it
  1594.     will be left out.  Often they're motivated by wanting someone whom they
  1595.     could sue.  If all they want is someone to help them out with Perl
  1596.     problems, there's always the net.  And if they really want to pay
  1597.     someone for that help, well, Tom and Randal are always looking for a
  1598.     job. :-)
  1599.  
  1600.     If companies want "commercial support" for it badly enough, speak up --
  1601.     something might be able to be arranged.
  1602.  
  1603.  
  1604. 2.13) What is a JAPH?  What does "Will hack perl for ..." mean?
  1605.  
  1606.     These are the "just another perl hacker" signatures that some people
  1607.     sign their postings with.  About 100 of the of the earlier ones are
  1608.     available from the various FTP sites.
  1609.  
  1610.     When people started running out of tricky and interesting JAPHs, some
  1611.     of them turned to writing "Will hack perl for ..." quotes.  While
  1612.     sometimes humourous, they just didn't have the flair of the JAPHs and
  1613.     have since almost completely vanished.
  1614.  
  1615. 2.14) Where can I get a list of Larry Wall witticisms?
  1616.  
  1617.     Over a hundred quips by Larry, from postings of his or source code,
  1618.     can be found in many of the FTP sites or through the World Wide Web at
  1619.     "ftp://ftp.cis.ufl.edu/pub/perl/misc/lwall-quotes"
  1620.  
  1621.  
  1622. 2.15) What are the known bugs?
  1623.  
  1624.     These apply to version 5 of perl.  See also: `What does "Malformed
  1625.     command links" mean?' for a version 4 bug.
  1626.  
  1627.        The README says it's a pre-release.
  1628.            Workaround: ignore this sentence.
  1629.  
  1630.        Installs perl0.000 and sperl0.000 instead of 5.000.
  1631.            Workaround: rename the files.
  1632.  
  1633.        The debugger appears to be broken on "my" variables;
  1634.            Workaround: none yet
  1635.  
  1636.        Recursive signal handlers eventually core dump.
  1637.            Workaround: ease up on the ^C key.
  1638.  
  1639.        The following code misbehaves: print ++$_ . "\n" until /0/;
  1640.            Workaround: initialize your variable
  1641.  
  1642.        Destructors can clobber $@ on exit from an eval
  1643.            Workaround: local $@; eval {...};
  1644.  
  1645.  
  1646. 2.16) Where should I post bugs?
  1647.  
  1648.     Before posting about a bug, please make sure that you are using the
  1649.     most recent versions of perl (currently 4.036 and 5.000) available.
  1650.     If you are not using one of these versions, chances are you will be
  1651.     told to upgrade because the bug has already been fixed.
  1652.  
  1653.     If you are reporting a bug in perl5, the best place to send your bug
  1654.     is <perlbug@perl.com>, which is currently just an alias for
  1655.     <perl5-porters@nicoh.com>.  In the past, there have been problems with
  1656.     the perlbug address.  If you have problems with it, please send your
  1657.     bug directly to <perl5-porters@nicoh.com>.  You may subscribe to the list
  1658.     in the customary fashion via mail to <perl5-porters-request@nicoh.com>.
  1659.     Feel free to post your bugs to the comp.lang.perl newsgroup as well,
  1660.     but do make sure they still go to the mailing list.
  1661.  
  1662.     To enhance your chances of getting any bug you report fixed:
  1663.  
  1664.     1. Make sure you are using a production version of perl.  Alpha and
  1665.        Beta version problems have probably already been reported and
  1666.        fixed.
  1667.  
  1668.     2. Try to narrow the problem down to as small a piece of code as
  1669.        possible.  If you can get it down to 1 line of Perl then so much
  1670.        the better.
  1671.  
  1672.     3. Include a copy of the output from the myconfig script from the
  1673.        Perl source distribution in your posting.
  1674.  
  1675. 2.17) Where should I post source code?
  1676.  
  1677.     You should post source code to whichever group is most appropriate,
  1678.     but feel free to cross-post to comp.lang.perl.  If you want to
  1679.     cross-post to alt.sources, please make sure it follows their
  1680.     posting standards, including setting the Followups-To header
  1681.     line to NOT include alt.sources; see their FAQ for details.
  1682.  
  1683.  
  1684. 2.18) Where can I learn about object-oriented Perl programming?
  1685.  
  1686.     The perlobj(1) man page is a good place to start, and then you can
  1687.     check out the excellent perlbot(1) man page written by the dean of perl
  1688.     o-o himself, Dean Roehrich.  Areas covered include the following:
  1689.  
  1690.         Idx  Subsections in perlobj.1          Lines
  1691.          1   NAME                                  2
  1692.          2   DESCRIPTION                          16
  1693.          3   An Object is Simply a Reference      60
  1694.          4   A Class is Simply a Package          31
  1695.          5   A Method is Simply a Subroutine      34
  1696.          6   Method Invocation                    75
  1697.          7   Destructors                          14
  1698.          8   Summary                               7
  1699.  
  1700.         Idx  Subsections in perlbot.1          Lines
  1701.          1   NAME                                  2
  1702.          2   INTRODUCTION                          9
  1703.          3   Instance Variables                   43
  1704.          4   Scalar Instance Variables            21
  1705.          5   Instance Variable Inheritance        35
  1706.          6   Object Relationships                 33
  1707.          7   Overriding Superclass Methods        49
  1708.          8   Using Relationship with Sdbm         45
  1709.          9   Thinking of Code Reuse              111
  1710.  
  1711.     The section on instance variables should prove very helpful to those
  1712.     wondering how to get data inheritance in perl.
  1713.  
  1714.  
  1715. 2.19) Where can I learn about linking C with Perl? [h2xs]
  1716.  
  1717.     While it used to be deep magic, how to do this is now revealed in the
  1718.     perlapi(1), perlguts(1), and perlcall(1) man pages, which treat with
  1719.     this matter extensively.
  1720.  
  1721.  
  1722. 2.20) What is perl.com?
  1723.  
  1724.     Perl.com is just Tom's domain name, registered as dedicated to "Perl
  1725.     training and consulting".  While not a proper ftp site (he hasn't got
  1726.     the bandwidth (yet)), it does serve as a clearinghouse for certain
  1727.     perl related mailing list.  That means that you should always be able
  1728.     to get to Larry, Tom, or Randal through that host.  The following
  1729.     aliases work:
  1730.  
  1731.         perl-packrats:          The archivist list
  1732.           perl-porters:           The porters list
  1733.         perlbook:               The Camel/Llama/Alpaca writing committee
  1734.         perlbugs:               The bug list (perl-porters for now)
  1735.         perlclasses:            Info on Perl training
  1736.         perlfaq:                Submissions/Errata to the Perl FAQ
  1737.                                 (Tom and Steve)
  1738.         perlrefguide:           Submissions/Errata to the Perl FAQ (Johan)
  1739.  
  1740. y
  1741. 2.21) What do the asterisks (*) throughout the FAQ stand for?
  1742.  
  1743.     To keep from cluttering up the FAQ and for easy reference all email
  1744.     addresses have been collected in this location.  For each person
  1745.     listed, I offer my thanks for their input and help.
  1746.  
  1747.     * Larry Wall        <lwall@netlabs.com>
  1748.     * Tom Christiansen        <tchrist@wraeththu.cs.colorado.edu>
  1749.     * Stephen P Potter        <spp@vx.com>
  1750.     * Andreas Koenig        <k@franz.ww.TU-Berlin.DE>
  1751.     * Bill Eldridge        <bill@cognet.ucla.edu>
  1752.     * Buzz Moschetti        <buzz@toxicavenger.bear.com>
  1753.     * Casper H.S. Dik        <casper@fwi.uva.nl>
  1754.     * David Muir Sharnoff   <muir@tfs.com>
  1755.     * Dean Roehrich         <roehrich@ironwood.cray.com>
  1756.     * Dominic Giampaolo     <dbg@sgi.com>,
  1757.     * Frederic Chauveau     <fmc@pasteur.fr>
  1758.     * Gene Spafford         <spaf@cs.purdue.edu>
  1759.     * Guido van Rossum        <guido@cwi.nl>
  1760.     * Henk P Penning        <henkp@cs.ruu.nl>
  1761.     * Jeff Friedl        <jfriedl@omron.co.jp>
  1762.     * Johan Vromans        <jv@NL.net>
  1763.     * John Dallman        <jgd@cix.compulink.co.uk>
  1764.     * John Lees            <lees@pixel.cps.msu.edu>
  1765.     * John Ousterhout        <ouster@eng.sun.com>
  1766.     * Jon Biggar        <jon@netlabs.com>
  1767.     * Ken Lunde                <lunde@mv.us.adobe.com>
  1768.     * Malcolm Beattie        <mbeattie@sable.ox.ac.uk>
  1769.     * Michael D'Errico        <mike@software.com>
  1770.     * Randal Schwartz        <merlyn@stonehenge.com>
  1771.     * Roberto Salama        <rs@fi.gs.com>
  1772.     * Steven L Kunz        <skunz@iastate.edu>
  1773.     * Theodore C. Law        <TEDLAW@TOROLAB6.VNET.IBM.COM>
  1774.     * Thomas R. Kimpton        <tom@dtint.dtint.com>
  1775.     * Timothy Murphy        <tim@maths.tcd.ie>
  1776.     * UF Computer Staff        <consult@cis.ufl.edu>
  1777.  
  1778.  
  1779. --
  1780. Stephen P Potter        spp@vx.com        Varimetrix Corporation
  1781. 2350 Commerce Park Drive, Suite 4                Palm Bay, FL 32905
  1782. (407) 676-3222                           CAD/CAM/CAE/Software
  1783.  
  1784.  
  1785.  
  1786. Archive-name: perl-faq/part3
  1787. Version: $Id: part3,v 2.3 1995/01/31 14:45:08 spp Exp spp $
  1788. Posting-Frequency: bi-weekly
  1789. Change-Log: $Log: part3,v $
  1790. # Revision 2.3  1995/01/31  14:45:08  spp
  1791. # 12/13/94 -  Modified section on using menu.pl with perl 5.000.
  1792. # 01/23/95 -  Modified section on using menu.pl with perl 5.000.
  1793. # 01/30/95 -  Serious rewrite of 3.5) perl - to - C/binary
  1794. #
  1795.  
  1796.  
  1797. This posting contains answers to general information questions, mostly
  1798. about programming aids.
  1799.  
  1800.  
  1801. 3.1) How can I use Perl interactively?
  1802.  
  1803.     The easiest way to do this is to run Perl under its debugger.  If you
  1804.     have no program to debug, you can invoke the debugger on an `empty'
  1805.     program like this:
  1806.  
  1807.         perl -de 0
  1808.  
  1809.     (The more positive hackers prefer "perl -de 1". :-)
  1810.  
  1811.     Now you can type in any legal Perl code, and it will be immediately
  1812.     evaluated.  You can also examine the symbol table, get stack
  1813.     backtraces, check variable values, and if you want to, set breakpoints
  1814.     and do the other things you can do in a symbolic debugger.
  1815.  
  1816.  
  1817. 3.2) Is there a Perl profiler?
  1818.  
  1819.     While there isn't one included with the perl source distribution (yet)
  1820.     various folks have written packages that allow you to do at least some
  1821.     sort of profiling.  The strategy usually includes modifying the perl
  1822.     debugger to handle profiling.  Authors of these packages include
  1823.  
  1824.     Wayne Thompson         <me@anywhere.EBay.Sun.COM>
  1825.     Ray Lischner         <lisch@sysserver1.mentor.com>
  1826.     Kresten Krab Thorup     <krab@iesd.auc.dk>
  1827.  
  1828.     The original articles by these folks containing their profilers are
  1829.     available on convex.com in /pub/perl/information/profiling.shar via
  1830.     anon ftp.
  1831.  
  1832.  
  1833. 3.3) Is there a yacc for Perl?
  1834.  
  1835.     Yes!! It's a version of Berkeley yacc that outputs Perl code instead
  1836.     of C code!  You can get this from ftp.sterling.com [192.124.9.1] in
  1837.     /local/perl-byacc1.8.2.tar.Z, or send the author mail for details.
  1838.  
  1839.  
  1840. 3.4) Is there a pretty-printer for Perl?
  1841.  
  1842.     That depends on what you mean.  If you want something that works like
  1843.     vgrind on Perl programs, then the answer is "yes, nearly".  Here's a
  1844.     vgrind entry for perl:
  1845.  
  1846.     PERL|perl|Perl:\
  1847.         :pb=^\d?(sub|package)\d\p\d:\
  1848.         :bb={:be=}:cb=#:ce=$:sb=":se=\e":lb=':\
  1849.         :le=\e':tl:\
  1850.         :id=_:\
  1851.         :kw=\
  1852.         if for foreach unless until while continue else elsif \
  1853.         do eval require \
  1854.         die exit \
  1855.         defined delete reset \
  1856.         goto last redo next dump \
  1857.         local undef return  \
  1858.         write format  \
  1859.         sub package
  1860.  
  1861.     It doesn't actually do everything right; in particular,
  1862.     things like $#, $', s#/foo##, and $foo'bar all confuse it.
  1863.  
  1864.     David Levine uses this:
  1865.  
  1866.     # perl 4.x                    David Levine <levine@ics.uci.edu> 05 apr 1993
  1867.     # Derived from Tom Christiansen's perl vgrindef.  I'd like to treat all  of
  1868.     # perl's built-ins as  keywords,  but vgrind   fields are  limited  to 1024
  1869.     # characters  and the built-ins overflow that (surprise  :-).  So, I didn't
  1870.     # include the dbm*, end*, get*, msg*, sem*, set*,  and  shm* functions.   I
  1871.     # couldn't come up with an easy way to  distinguish beginnings  of literals
  1872.     # ('...') from package prefixes, so literals are not marked.
  1873.     # Be sure to:
  1874.     # 1) include whitespace between a subprogram name and its opening {
  1875.     # 2) include whitespace before a comment (so that $# doesn't get
  1876.     # interpreted as one).
  1877.     perl4:\
  1878.         :pb=^\d?(sub|package)\d\p\d:\
  1879.         :id=$%@_:\
  1880.         :bb=\e{:be=\e}:cb=\d\e#:ce=$:sb=\e":se=\e":\
  1881.         :kw=accept alarm atan2 bind binmode caller chdir chmod chop \
  1882.     chown chroot close closedir connect continue cos crypt defined delete \
  1883.     die do dump each else elsif eof eval exec exit exp fcntl fileno flock \
  1884.     for foreach fork format getc gmtime goto grep hex if include index int \
  1885.     ioctl join keys kill last length link listen local localtime log lstat \
  1886.     m mkdir next oct open opendir ord pack package pipe pop print printf \
  1887.     push q qq qx rand read readdir readlink recv redo rename require reset \
  1888.     return reverse rewinddir rindex rmdir s scalar seek seekdir select send \
  1889.     shift shutdown sin sleep socket socketpair sort splice split sprintf \
  1890.     sqrt srand stat study sub substr symlink syscall sysread system \
  1891.     syswrite tell telldir time times tr truncate umask undef unless unlink \
  1892.     unpack unshift until utime values vec wait waitpid wantarray warn while \
  1893.     write y:
  1894.  
  1895.     If what you mean is whether there is a program that will reformat the
  1896.     program much as indent(1) will do for C, then the answer is no.  The
  1897.     complex feedback between the scanner and the parser (as in the things
  1898.     that confuse vgrind) make it challenging at best to write a stand-alone
  1899.     Perl parser.
  1900.  
  1901.  
  1902. 3.5) How can I convert my perl scripts directly to C or compile them into
  1903.      binary form?
  1904.  
  1905.     The short answer is: "No, you can't compile perl into C.  Period."
  1906.  
  1907.     However, having said that, it is believed that it would be possible to
  1908.     write a perl to C translator, although it is a PhD thesis waiting to
  1909.     happen.  Anyone need a good challenging thesis?
  1910.  
  1911.     In the way of further, detailed explication, it seems that the reasons
  1912.     people want to do this usaully break down into one or more of the
  1913.     following:
  1914.  
  1915.     A) speed
  1916.     B) secrecy
  1917.     C) maintainability
  1918.  
  1919.     SPEED:
  1920.  
  1921.     1)    You can't turn perl source code or perl intermediary code into
  1922.     native machine code to make it run faster, and saving the perl
  1923.     intermediary code doesn't really buy you as much as you'd like.
  1924.     If you really must, check out the undump and unexec alternatives.
  1925.     If your motivations are speed, then this may or may not help you
  1926.     much.
  1927.  
  1928.     You might also look into autoloading functions on the fly, which
  1929.     can greatly reduce start-up time.
  1930.  
  1931.     If you have a few routines that are bogging you down, you just
  1932.     possibly might wish to hand-translate just them into C, then
  1933.     dynamically load these in.  See perlapi(1) for details.  Most
  1934.     of the time, however, reorganizing your perl algorithm is the best
  1935.     way to address this.
  1936.  
  1937.  
  1938.     SOURCE-CODE SECRECY:
  1939.  
  1940.     2)  If you're trying to stop people from seeing what you're doing,
  1941.     you can shroud it, i.e. turn all the idents into silly stuff,
  1942.     rearrange strings, and remove redundant white space.  There's
  1943.     a program out there called ShroudIt! that works on Perl,
  1944.     but I lost my pointer to it.
  1945.  
  1946.     3)  You might also look into the cryptswitch() stuff in the perl
  1947.         source, which would allow you to ship something in a form they
  1948.         can't read.  This isn't particulary well-documented.
  1949.  
  1950.     4)  If you're worried about them using your software without licence,
  1951.     you put some huge disclaimer at the top that says something like
  1952.     the following.  This is actually the best solution, because only a
  1953.     legal solution will really work if legality is what you're worried
  1954.     about: trying to solve legal problems with technical solutions
  1955.     is not worth the effort, and too easily circumvented.
  1956.  
  1957.         This is UNPUBLISHED PROPRIETARY SOURCE CODE of XYZZY, Inc.; the
  1958.         contents of this file may not be disclosed to third parties,
  1959.         copied or duplicated in any form, in whole or in part, without
  1960.         the prior written permission of XYZZY, Inc.
  1961.  
  1962.         Permission is hereby granted soley to the licencee for use of
  1963.         this source code in its unaltered state.  This source code may
  1964.         not be modified by licencee except under direction of XYZZY
  1965.         Inc.  Neither may this source code be given under any
  1966.         circumstances to non-licensees in any form, including source
  1967.         or binary.  Modification of this source constitutes breach of
  1968.         contract, which voids any potential pending support
  1969.         responsibilities by XYZZY Inc.  Divulging the exact or
  1970.         paraphrased contents of this source code to unlicensed parties
  1971.         either directly or indirectly constitutes violation of federal
  1972.         and international copyright and trade secret laws, and will be
  1973.         duly prosecuted to the fullest extent permitted under law.
  1974.  
  1975.         This software is provided by XYZZY Inc. ``as is'' and any
  1976.         express or implied warranties, including, but not limited to,
  1977.         the implied warranties of merchantability and fitness for a
  1978.         particular purpose are disclaimed.  In no event shall the
  1979.         regents or contributors be liable for any direct, indirect,
  1980.         incidental, special, exemplary, or consequential damages
  1981.         (including, but not limited to, procurement of substitute
  1982.         goods or services; loss of use, data, or profits; or business
  1983.         interruption) however caused and on any theory of liability,
  1984.         whether in contract, strict liability, or tort (including
  1985.         negligence or otherwise) arising in any way out of the use of
  1986.         this software, even if advised of the possibility of such
  1987.         damage.
  1988.  
  1989.     MAINTAINABILITY:
  1990.  
  1991.     5)  If you just want to stop people from changing it because you're
  1992.     concerned about support issues, you can put in a big disclaimer at
  1993.     the top that says that if they touch the file they void the
  1994.     warranty, and then make them give you a size, checksum, and
  1995.     version of the file before answering any questions about it.
  1996.  
  1997.     If you maintain a central site that distributes software to
  1998.     internal client machines, use rdist(1) to send around a proper
  1999.     version periodically, perhaps using the -y option on the install
  2000.     to flag destinations younger than the source.
  2001.  
  2002.     Let it be noted than in the many, many years that Perl's author
  2003.     has been releasing and supporting freely redistributable software,
  2004.     he has NEVER ONCE been bitten by a bogus bug report generated by
  2005.     someone breaking his code because they had access to it.  Rather,
  2006.     he and many other open software provided (where open software
  2007.     means that for which the source is provided, the only truly open
  2008.     software) have saved themselves countless hours of labor thousands
  2009.     of times over because they've allowed people to inspect the source
  2010.     for themselves.  Proprietary source-code hoarding is its own
  2011.         headache.
  2012.  
  2013.     Thus, obscurity for the sake of maintainability would seem to be a
  2014.     red herring.
  2015.  
  2016.     6)  If you can't count on perl being installed at the destination
  2017.         customer, then by all means, merely ship it with your program.
  2018.         This is no hardship, since software providers are accustomed to
  2019.         shipping software in machine-specific binary form.  The basic idea
  2020.         is as simple as:
  2021.  
  2022.         shar /usr/local/{lib,bin,man}/perl myprog
  2023.  
  2024.     Although don't overwrite their own Perl installation if they have
  2025.         one!
  2026.  
  2027.  
  2028. 3.6) Where can I get a perl-mode for emacs?
  2029.  
  2030.     Since Emacs version 19 patchlevel 22 or so, there has been both a
  2031.     perl-mode.el and support for the perl debugger built in.  These should
  2032.     come with the standard Emacs 19 distribution.
  2033.  
  2034.     In the perl source directory, you'll find a directory called
  2035.     "emacs", which contains several files that should help you.
  2036.  
  2037.     Note that the perl-mode of emacs will have fits with "main'foo" (single
  2038.     quote), and mess up the indention and hilighting.  However, note that
  2039.     in perl5, you should be using "main::foo".  By the way, did we mention
  2040.     that you should upgrade?
  2041.  
  2042.  
  2043. 3.7) Is there a Perl shell?
  2044.  
  2045.     Not really.  Perl is a programming language, not a command
  2046.     interpreter.  There is a very simple one called "perlsh"
  2047.     included in the Perl source distribution.  It just does this:
  2048.  
  2049.     $/ = '';        # set paragraph mode
  2050.     $SHlinesep = "\n";
  2051.     while ($SHcmd = <>) {
  2052.         $/ = $SHlinesep;
  2053.         eval $SHcmd; print $@ || "\n";
  2054.         $SHlinesep = $/; $/ = '';
  2055.     }
  2056.  
  2057.     Not very interesting, eh?
  2058.  
  2059.     Daniel Smith <dansmith@autodesk.com> is working on an interactive Perl
  2060.     shell called SoftList.  It's currently at version 3.0beta.  SoftList
  2061.     3.0 has tcsh-like command line editing, can let you define a file of
  2062.     aliases so that you can run chunks of perl or UNIX commands, and so
  2063.     on.  You can send mail to him for further information and availability.
  2064.  
  2065. 3.8) How can I use curses with perl?
  2066.  
  2067.     In release 4 of perl, the only way to do this was was to build a
  2068.     curseperl binary by linking in your C curses library as described in
  2069.     the usub subdirectory of the perl sources.  This requires a modicum of
  2070.     work, but it will be reasonably fast since it's all in C (assuming you
  2071.     consider curses reasonably fast. :-) Programs written using this
  2072.     method require the modified curseperl, not vanilla perl, to run.
  2073.     While this is something of a disadvantage, experience indicates that
  2074.     it's better to use curseperl than to try to roll your own using
  2075.     termcap directly.
  2076.  
  2077.     Fortunately, in version 5, Curses is a dynamically loaded extension
  2078.     by William Setzer*.  You should be able to pick it up wherever you
  2079.     get Perl 5 from, or at least these places:
  2080.  
  2081.     ftp://ftp.ncsu.edu/pub/math/wsetzer/cursperl5a6.tar.gz
  2082.         ftp://ftp.metronet.com/pub/perlinfo/perl5/cursperl5a6.tar.gz
  2083.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/cursperl5a6.tar.gz
  2084.  
  2085.     For a good example of using (v4) curseperl, you might want to pick up a
  2086.     copy of  Steven L Kunz's* "perl menus" package ("menu.pl") via
  2087.     anonymous FTP from "ftp.iastate.edu".  It's in the /pub/perl as
  2088.  
  2089.         menu.pl.v3.0.1.tar.Z
  2090.  
  2091.     menu.pl is a complete menu front-end for perl+curses and demonstates
  2092.     a lot of things (plus it is useful to boot if you want full-screen
  2093.     menu selection ability).  It provides full-screen menu selection
  2094.     ability for three menu styles (single-selection, multiple-selection,
  2095.     and "radio-button").
  2096.  
  2097.     New in version 3.0 is a provision for full-screen data entry.  A
  2098.     "template" concept is implemented to create a simple (yet flexible)
  2099.     perl interface for building data entry screens for registration,
  2100.     database, or other record-oriented tasks.
  2101.  
  2102.     Version 3.0 of menu.pl is supported on Perl4/curseperl and
  2103.     Perl5/Curses.  Complete user documentation is provided along with
  2104.     several demos and "beginner applications".  A menu utility module is
  2105.     provided that is a collection of useful perl curses routines (such as
  2106.     "pop-up" query boxes) that may be called from your applications.
  2107.  
  2108.     Another possibility is to use Henk Penning's cterm package, a curses
  2109.     emulation library written in perl.  cterm is actually a separate
  2110.     program with which you communicate via a pipe.  It is available from
  2111.     ftp.cs.ruu.nl [131.211.80.17] via anonymous ftp. in the directory
  2112.     pub/PERL.  You may also acquire the package via email in compressed,
  2113.     uuencoded form by sending a message to mail-server@cs.ruu.nl
  2114.     containing these lines:
  2115.  
  2116.     begin
  2117.     send PERL/cterm.shar.Z
  2118.     end
  2119.  
  2120.     See the question on retrieving perl via mail for more information on
  2121.     how to retrieve other items of interest from the mail server
  2122.     there.
  2123.  
  2124.  
  2125. 3.9) How can I use X with Perl?
  2126.  
  2127.     Right now, you have several choices.  If you are still using perl4, use
  2128.     the WAFE or STDWIN packages, or try to make your own usub binding.
  2129.  
  2130.     However, if you've upgraded to version 5, you have several exciting
  2131.     possibilities, with more popping up each day.  Right now, Tk and Sx
  2132.     are the best known such extensions.
  2133.  
  2134.     If you like the tk package, you should get the Tk extension kit,
  2135.     written by Malcolm Beattie*.  Here are some places to get it:
  2136.  
  2137.      Tk (as in tcl/tk, but sans tcl)
  2138.  
  2139.     ftp://ftp.cis.ufl.edu/pub/perl/src/tkperl/tkperl5a4.tar.gz
  2140.         ftp://ftp.khoros.unm.edu/pub/perl/extensions/tkperl5a4.tar.gz
  2141.         ftp://ftp.metronet.com/pub/perlinfo/perl5/tkperl/tkperl5a4.tar.gz
  2142.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/tkperl5a4.tar.gz
  2143.         ftp://black.ox.ac.uk/src/ALPHA/tkperl5a4.tar.gz
  2144.  
  2145.         (try 5a5 everywhere after 2pm UST Thu 20 Oct 1994, as in)
  2146.  
  2147.         ftp://sable.ox.ac.uk/pub/perl/tkperl5a5.tar.gz
  2148.  
  2149.     You may also use the old Sx package, (Athena & Xlib), written by
  2150.     originally written by by Dominic Giampaolo*, then and rewritten for Sx
  2151.     by Frederic Chauveau*.fr>.  It's available from these sites:
  2152.  
  2153.         ftp://ftp.pasteur.fr/pub/Perl/Sx.tar.gz
  2154.         ftp://ftp.khoros.unm.edu/pub/perl/extensions/Sx.tar.gz
  2155.         ftp://ftp.metronet.com/pub/perlinfo/perl5/Sx.tar.gz
  2156.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/PerlDoc.ps.gz
  2157.  
  2158.     STDWIN is a library written by Guido van Rossum* (author of the Python
  2159.     programming language) that is portable between Mac, Dos and X11.  One
  2160.     could write a Perl agent to speak to this STDWIN server.
  2161.  
  2162.     WAFE is a package that implements a symbolic interface to the Athena
  2163.     widgets (X11R5). A typical Wafe application consists in our framework
  2164.     of two parts: the front-end (we call it Wafe for Widget[Athena]front
  2165.     end) and an application program running typically as a separate
  2166.     process.  The application program can be implemented in an arbitrary
  2167.     programming language and talks to the front-end via stdio.  Since Wafe
  2168.     (the front-end) was developed using the extensible TCL shell (cite John
  2169.     Ousterhout), an application program can dynamically submit requests to
  2170.     the front-end to build up the graphical user interface; the
  2171.     application can even down-load application specific procedures into
  2172.     the front-end.  The distribution contains sample application programs
  2173.     in Perl, GAWK, Prolog, TCL, and C talking to the same Wafe binary.
  2174.     Many of the demo applications are implemented in Perl.  Wafe 0.9 can
  2175.     be obtained via anonymous ftp from
  2176.     ftp.wu-wien.ac.at[137.208.3.5]:pub/src/X11/wafe-0.9.tar.Z
  2177.  
  2178.     Alternatively, you could use wish from tcl.
  2179.  
  2180.     #!/usr/local/bin/perl
  2181.     #####################################################################
  2182.     #  An example of calling wish as a subshell under Perl and
  2183.     #  interactively communicating with it through sockets.
  2184.     #
  2185.     #  The script is directly based on Gustaf Neumann's perlwafe script.
  2186.     #
  2187.     #  Dov Grobgeld dov@menora.weizmann.ac.il
  2188.     #  1993-05-17
  2189.     #####################################################################
  2190.  
  2191.     $wishbin = "/usr/local/bin/wish";
  2192.  
  2193.     die "socketpair unsuccessful: $!!\n" unless socketpair(W0,WISH,1,1,0);
  2194.     if ($pid=fork) {
  2195.         select(WISH); $| = 1;
  2196.         select(STDOUT);
  2197.  
  2198.     # Create some TCL procedures
  2199.         print WISH 'proc echo {s} {puts stdout $s; flush stdout}',"\n";
  2200.  
  2201.     # Create the widgets
  2202.     print WISH <<TCL;
  2203.     # This is a comment "inside" wish
  2204.  
  2205.     frame .f -relief raised -border 1 -bg green
  2206.     pack append . .f {top fill expand}
  2207.  
  2208.     button .f.button-pressme -text "Press me" -command {
  2209.         echo "That's nice."
  2210.     }
  2211.     button .f.button-quit -text quit -command {
  2212.         echo "quit"
  2213.     }
  2214.     pack append .f .f.button-pressme {top fill expand} \\
  2215.                .f.button-quit {top expand}
  2216.  
  2217. TCL
  2218.     ;
  2219.     # Here is the main loop which receives and sends commands
  2220.     # to wish.
  2221.     while (<WISH>) {
  2222.         chop;
  2223.         print "Wish sais: <$_>\n";
  2224.         if (/^quit/) { print WISH "destroy .\n"; last; }
  2225.     }
  2226.         wait;
  2227.     } elsif (defined $pid) {
  2228.     open(STDOUT, ">&W0");
  2229.     open(STDIN, ">&W0");
  2230.     close(W0);
  2231.     select(STDOUT); $| = 1;
  2232.     exec "$wishbin --";
  2233.     } else {
  2234.     die "fork error: $!\n";
  2235.     }
  2236.  
  2237.  
  2238. 3.10) Can I dynamically load C user routines?
  2239.  
  2240.     Yes -- dynamic loading comes with the distribution.  That means that
  2241.     you no longer need 18 different versions of fooperl floating around.
  2242.     In fact, all of perl can be stuck into a libperl.so library and
  2243.     then your /usr/local/bin/perl binary reduced to just 50k or so.
  2244.     See DynLoader(3pm) for details.
  2245.  
  2246.     In perl4, the answer is kinda.  One package has been released that does
  2247.     this, by Roberto Salama*.  He writes:
  2248.  
  2249.     Here is a version of dylperl, dynamic linker for perl. The code here is
  2250.     based on Oliver Sharp's May 1993 article in Dr. Dobbs Journal (Dynamic
  2251.     Linking under Berkeley UNIX).
  2252.  
  2253.           dyl.h
  2254.           dyl.c - code extracted from Oliver Sharp's article
  2255.  
  2256.           hash.h
  2257.           hash.c - Berkeley's hash functions, should use perl's but
  2258.                could not be bothered
  2259.  
  2260.        dylperl.c - perl usersubs
  2261.           user.c - userinit function
  2262.  
  2263.         sample.c - sample code to be dyl'ed
  2264.        sample2.c -          "
  2265.          test.pl - sample perl script that dyl's sample*.o
  2266.  
  2267.     The Makefile assumes that uperl.o is in /usr/local/src/perl/... You
  2268.     will probable have to change this to reflect your installation. Other
  2269.     than that, just type 'make'...
  2270.  
  2271.     The idea behind being able to dynamically link code into perl is that
  2272.     the linked code should become perl functions, i.e. they can be invoked
  2273.     as &foo(...).  For this to happen, the incrementally loaded code must
  2274.     use the perl stack, look at sample.c to get a better idea.
  2275.  
  2276.     The few functions that make up this package are outlined below.
  2277.  
  2278.     &dyl("file.o"): dynamically link file.o. All functions and non-static
  2279.            variables become visible from within perl. This
  2280.            function returns a pointer to an internal hash table
  2281.            corresponding to the symbol table of the newly loaded
  2282.            code.
  2283.  
  2284.            eg: $ht = &dyl("sample.o")
  2285.  
  2286.        This function can also be called with the -L and -l ld options.
  2287.  
  2288.            eg: $ht = &dyl(""sample2.o", "-L/usr/lib", "-lm")
  2289.                will also pick up the math library if sample.o
  2290.                accesses any symbols there.
  2291.  
  2292.     &dyl_find("func"): find symbol 'func' and return its symbol table entry
  2293.  
  2294.     &dyl_functions($ht): print the contents of the internal hash table
  2295.     &dyl_print_symbols($f): prints the contents of the symbol returned by
  2296.     dyl_find()
  2297.  
  2298.     There is very little documentation, maybe something to do for a future
  2299.     release.  The files sample.o, and sample2.o contain code to be
  2300.     incrementally loaded, test.pl is the test perl script.
  2301.  
  2302.     Comments are welcome. I submit this code for public consumption and,
  2303.     basically, am not responsible for it in any way.
  2304.  
  2305.  
  2306. 3.11) What is undump and where can I get it?
  2307.  
  2308.     The undump program comes from the TeX distribution.  If you have TeX,
  2309.     then you may have a working undump.  If you don't, and you can't get
  2310.     one, *AND* you have a GNU emacs working on your machine that can clone
  2311.     itself, then you might try taking its unexec() function and compiling
  2312.     Perl with -DUNEXEC, which will make Perl call unexec() instead of
  2313.     abort().  You'll have to add unexec.o to the objects line in the
  2314.     Makefile.  If you succeed, post to comp.lang.perl about your experience
  2315.     so others can benefit from it.
  2316.  
  2317.     If you have a version of undump that works with Perl, please submit
  2318.     its anon-FTP whereabouts to the FAQ maintainer.
  2319.  
  2320.  
  2321. 3.12) How can I get '#!perl' to work under MS-DOS?
  2322.  
  2323.     John Dallman* has written a program "#!perl.exe" which will do this.
  2324.     It is available through anonymous ftp from ftp.ee.umanitoba.ca in the
  2325.     directory /pub/msdos/perl/hbp_20.zip.  This program works by finding
  2326.     the script and perl.exe, building a command line and running perl.exe
  2327.     as a child process.  For more information on this, contact John
  2328.     directly.
  2329.  
  2330. --
  2331. Stephen P Potter        spp@vx.com        Varimetrix Corporation
  2332. 2350 Commerce Park Drive, Suite 4                Palm Bay, FL 32905
  2333. (407) 676-3222                           CAD/CAM/CAE/Software
  2334.  
  2335.  
  2336.  
  2337. Archive-name: perl-faq/part4
  2338. Version: $Id: part4,v 2.5 1995/01/31 14:45:52 spp Exp spp $
  2339. Posting-Frequency: bi-weekly
  2340. Change-Log: $Log: part4,v $
  2341. # Revision 2.5  1995/01/31  14:45:52  spp
  2342. # 12/07/94 -  Changed 4.29 to reflect reality as blessed by Randal. ;-)
  2343. # 12/13/94 -  Modified question referencing perl-byacc to point to the
  2344. #             perl-byacc question in part 3.
  2345. # 01/23/95 -  Fixed typos and modified blank-stripping question as discussed
  2346. #             by Jeffrey Friedl.
  2347. # 01/31/95 -  Modified (no)echo and single-reading to include
  2348. #             Curses::(no){cbreak, echo}.
  2349. #
  2350.  
  2351.  
  2352. This posting contains answers to the following questions about General
  2353. Programming, Regular Expressions (Regexp) and Input/Output:
  2354.  
  2355.  
  2356. 4.1) What are all these $@%*<> signs and how do I know when to use them?
  2357.  
  2358.     Those are type specifiers:
  2359.     $ for scalar values
  2360.     @ for indexed arrays
  2361.     % for hashed arrays (associative arrays)
  2362.     * for all types of that symbol name.  These are sometimes used like
  2363.         pointers
  2364.     <> are used for inputting a record from a filehandle.
  2365.  
  2366.     See the question on arrays of arrays for more about Perl pointers.
  2367.  
  2368.     While there are a few places where you don't actually need these type
  2369.     specifiers, except for files, you should always use them.  Note that
  2370.     <FILE> is NOT the type specifier for files; it's the equivalent of awk's
  2371.     getline function, that is, it reads a line from the handle FILE.  When
  2372.     doing open, close, and other operations besides the getline function on
  2373.     files, do NOT use the brackets.
  2374.  
  2375.     Beware of saying:
  2376.     $foo = BAR;
  2377.     Which wil be interpreted as
  2378.     $foo = 'BAR';
  2379.     and not as
  2380.     $foo = <BAR>;
  2381.     If you always quote your strings, you'll avoid this trap.
  2382.  
  2383.     Normally, files are manipulated something like this (with appropriate
  2384.     error checking added if it were production code):
  2385.  
  2386.     open (FILE, ">/tmp/foo.$$");
  2387.     print FILE "string\n";
  2388.     close FILE;
  2389.  
  2390.     If instead of a filehandle, you use a normal scalar variable with file
  2391.     manipulation functions, this is considered an indirect reference to a
  2392.     filehandle.  For example,
  2393.  
  2394.     $foo = "TEST01";
  2395.     open($foo, "file");
  2396.  
  2397.     After the open, these two while loops are equivalent:
  2398.  
  2399.     while (<$foo>) {}
  2400.     while (<TEST01>) {}
  2401.  
  2402.     as are these two statements:
  2403.  
  2404.     close $foo;
  2405.     close TEST01;
  2406.  
  2407.     but NOT to this:
  2408.  
  2409.     while (<$TEST01>) {} # error
  2410.         ^
  2411.         ^ note spurious dollar sign
  2412.  
  2413.     This is another common novice mistake; often it's assumed that
  2414.  
  2415.     open($foo, "output.$$");
  2416.  
  2417.     will fill in the value of $foo, which was previously undefined.  This
  2418.     just isn't so -- you must set $foo to be the name of a filehandle
  2419.     before you attempt to open it.
  2420.  
  2421.  
  2422. 4.2) How come Perl operators have different precedence than C operators?
  2423.  
  2424.     Actually, they don't; all C operators have the same precedence in Perl as
  2425.     they do in C.  The problem is with a class of functions called list
  2426.     operators, e.g. print, chdir, exec, system, and so on.  These are somewhat
  2427.     bizarre in that they have different precedence depending on whether you
  2428.     look on the left or right of them.  Basically, they gobble up all things
  2429.     on their right.  For example,
  2430.  
  2431.     unlink $foo, "bar", @names, "others";
  2432.  
  2433.     will unlink all those file names.  A common mistake is to write:
  2434.  
  2435.     unlink "a_file" || die "snafu";
  2436.  
  2437.     The problem is that this gets interpreted as
  2438.  
  2439.     unlink("a_file" || die "snafu");
  2440.  
  2441.     To avoid this problem, you can always make them look like function calls
  2442.     or use an extra level of parentheses:
  2443.  
  2444.     unlink("a_file")  || die "snafu";
  2445.     (unlink "a_file") || die "snafu";
  2446.  
  2447.     In perl5, there are low precedence "and", "or", and "not" operators,
  2448.     which bind less tightly than comma.  This allows you to write:
  2449.  
  2450.     unlink $foo, "bar", @names, "others"     or die "snafu";
  2451.  
  2452.     Sometimes you actually do care about the return value:
  2453.  
  2454.     unless ($io_ok = print("some", "list")) { }
  2455.  
  2456.     Yes, print() returns I/O success.  That means
  2457.  
  2458.     $io_ok = print(2+4) * 5;
  2459.  
  2460.     returns 5 times whether printing (2+4) succeeded, and
  2461.     print(2+4) * 5;
  2462.     returns the same 5*io_success value and tosses it.
  2463.  
  2464.     See the perlop(1) man page's section on Precedence for more gory details,
  2465.     and be sure to use the -w flag to catch things like this.
  2466.  
  2467.  
  2468. 4.3) What's the difference between dynamic and static (lexical) scoping?
  2469.      What are my() and local()?
  2470.  
  2471.     [NOTE: This question refers to perl5 only.  There is no my() in perl4]
  2472.     Scoping refers to visibility of variables.  A dynamic variable is
  2473.     created via local() and is just a local value for a global variable,
  2474.     whereas a lexical variable created via my() is more what you're
  2475.     expecting from a C auto.  (See also "What's the difference between
  2476.     deep and shallow binding.")  In general, we suggest you use lexical
  2477.     variables wherever possible, as they're faster to access and easier to
  2478.     understand.   The "use strict vars" pragma will enforce that all
  2479.     variables are either lexical, or full classified by package name.  We
  2480.     strongly suggest that you develop your code with "use strict;" and the
  2481.     -w flag.  (When using formats, however, you will still have to use
  2482.     dynamic variables.)  Here's an example of the difference:
  2483.  
  2484.     $scount = 1; $lcount = 2;
  2485.         sub foo {
  2486.             my($i,$j) = @_;
  2487.             my    $scount = 10;
  2488.             local $lcount = 20;
  2489.             &bar();
  2490.         }
  2491.         sub bar {
  2492.             print "scount is $scount\en";
  2493.             print "lcount is $lcount\en";
  2494.         }
  2495.  
  2496.     This prints:
  2497.  
  2498.         scount is 1
  2499.         lcount is 20
  2500.  
  2501.     Notice that the variables declared with my() are visible only within
  2502.     the scope of the block which names them.  They are not visible outside
  2503.     of this block, not even in routines or blocks that it calls.  local()
  2504.     variables, on the other hand, are visible to routines that are called
  2505.     from the block where they are declared.  Neither is visible after the
  2506.     end (the final closing curly brace) of the block at all.
  2507.  
  2508.     Oh, lexical variables are only available in perl5.  Have we
  2509.     mentioned yet that you might consider upgrading? :-)
  2510.  
  2511.  
  2512. 4.4) What's the difference between deep and shallow binding?
  2513.  
  2514.     This only matters when you're making subroutines yourself, at least
  2515.     so far.   This will give you shallow binding:
  2516.  
  2517.     {
  2518.           my $x = time;
  2519.           $coderef = sub { $x };
  2520.         }
  2521.  
  2522.     When you call &$coderef(), it will get whatever dynamic $x happens
  2523.     to be around when invoked.  However, you can get the other behaviour
  2524.     this way:
  2525.  
  2526.     {
  2527.           my $x = time;
  2528.           $coderef = eval "sub { \$x }";
  2529.         }
  2530.  
  2531.     Now you'll access the lexical variable $x which is set to the
  2532.     time the subroutine was created.  Note that the difference in these
  2533.     two behaviours can be considered a bug, not a feature, so you should
  2534.     in particular not rely upon shallow binding, as it will likely go
  2535.     away in the future.  See perlref(1).
  2536.  
  2537.  
  2538. 4.5) How can I manipulate fixed-record-length files?
  2539.  
  2540.     The most efficient way is using pack and unpack.  This is faster than
  2541.     using substr.  Here is a sample chunk of code to break up and put back
  2542.     together again some fixed-format input lines, in this case, from ps.
  2543.  
  2544.     # sample input line:
  2545.     #   15158 p5  T      0:00 perl /mnt/tchrist/scripts/now-what
  2546.     $ps_t = 'A6 A4 A7 A5 A*';
  2547.     open(PS, "ps|");
  2548.     $_ = <PS>; print;
  2549.     while (<PS>) {
  2550.         ($pid, $tt, $stat, $time, $command) = unpack($ps_t, $_);
  2551.         for $var ('pid', 'tt', 'stat', 'time', 'command' ) {
  2552.         print "$var: <", eval "\$$var", ">\n";
  2553.         }
  2554.         print 'line=', pack($ps_t, $pid, $tt, $stat, $time, $command),
  2555.  "\n";
  2556.     }
  2557.  
  2558.  
  2559. 4.6) How can I make a file handle local to a subroutine?
  2560.  
  2561.     You must use the type-globbing *VAR notation.  Here is some code to
  2562.     cat an include file, calling itself recursively on nested local
  2563.     include files (i.e. those with #include "file", not #include <file>):
  2564.  
  2565.     sub cat_include {
  2566.         local($name) = @_;
  2567.         local(*FILE);
  2568.         local($_);
  2569.  
  2570.         warn "<INCLUDING $name>\n";
  2571.         if (!open (FILE, $name)) {
  2572.         warn "can't open $name: $!\n";
  2573.         return;
  2574.         }
  2575.         while (<FILE>) {
  2576.         if (/^#\s*include "([^"]*)"/) {
  2577.             &cat_include($1);
  2578.         } else {
  2579.             print;
  2580.         }
  2581.         }
  2582.         close FILE;
  2583.     }
  2584.  
  2585.  
  2586. 4.7) How can I call alarm() or usleep() from Perl?
  2587.  
  2588.     If you want finer granularity than 1 second (as usleep() provides) and
  2589.     have itimers and syscall() on your system, you can use the following.
  2590.     You could also use select().
  2591.  
  2592.     It takes a floating-point number representing how long to delay until
  2593.     you get the SIGALRM, and returns a floating- point number representing
  2594.     how much time was left in the old timer, if any.  Note that the C
  2595.     function uses integers, but this one doesn't mind fractional numbers.
  2596.  
  2597.     # alarm; send me a SIGALRM in this many seconds (fractions ok)
  2598.     # tom christiansen <tchrist@convex.com>
  2599.     sub alarm {
  2600.     require 'syscall.ph';
  2601.     require 'sys/time.ph';
  2602.  
  2603.     local($ticks) = @_;
  2604.     local($in_timer,$out_timer);
  2605.     local($isecs, $iusecs, $secs, $usecs);
  2606.  
  2607.     local($itimer_t) = 'L4'; # should be &itimer'typedef()
  2608.  
  2609.     $secs = int($ticks);
  2610.     $usecs = ($ticks - $secs) * 1e6;
  2611.  
  2612.     $out_timer = pack($itimer_t,0,0,0,0);
  2613.     $in_timer  = pack($itimer_t,0,0,$secs,$usecs);
  2614.  
  2615.     syscall(&SYS_setitimer, &ITIMER_REAL, $in_timer, $out_timer)
  2616.         && die "alarm: setitimer syscall failed: $!";
  2617.  
  2618.     ($isecs, $iusecs, $secs, $usecs) = unpack($itimer_t,$out_timer);
  2619.     return $secs + ($usecs/1e6);
  2620.     }
  2621.  
  2622.  
  2623. 4.8) How can I do an atexit() or setjmp()/longjmp() in Perl?  (Exception
  2624. handling)
  2625.  
  2626.     Perl's exception-handling mechanism is its eval operator.  You
  2627.     can use eval as setjmp and die as longjmp.  Here's an example
  2628.     of Larry's for timed-out input, which in C is often implemented
  2629.     using setjmp and longjmp:
  2630.  
  2631.       $SIG{ALRM} = TIMEOUT;
  2632.       sub TIMEOUT { die "restart input\n" }
  2633.  
  2634.       do { eval { &realcode } } while $@ =~ /^restart input/;
  2635.  
  2636.       sub realcode {
  2637.           alarm 15;
  2638.           $ans = <STDIN>;
  2639.           alarm 0;
  2640.       }
  2641.  
  2642.    Here's an example of Tom's for doing atexit() handling:
  2643.  
  2644.     sub atexit { push(@_exit_subs, @_) }
  2645.  
  2646.     sub _cleanup { unlink $tmp }
  2647.  
  2648.     &atexit('_cleanup');
  2649.  
  2650.     eval <<'End_Of_Eval';  $here = __LINE__;
  2651.     # as much code here as you want
  2652.     End_Of_Eval
  2653.  
  2654.     $oops = $@;  # save error message
  2655.  
  2656.     # now call his stuff
  2657.     for (@_exit_subs) { &$_() }
  2658.  
  2659.     $oops && ($oops =~ s/\(eval\) line (\d+)/$0 .
  2660.         " line " . ($1+$here)/e, die $oops);
  2661.  
  2662.     You can register your own routines via the &atexit function now.  You
  2663.     might also want to use the &realcode method of Larry's rather than
  2664.     embedding all your code in the here-is document.  Make sure to leave
  2665.     via die rather than exit, or write your own &exit routine and call
  2666.     that instead.   In general, it's better for nested routines to exit
  2667.     via die rather than exit for just this reason.
  2668.  
  2669.     In Perl5, it is easy to set this up because of the automatic processing
  2670.     of per-package END functions.
  2671.  
  2672.     Eval is also quite useful for testing for system dependent features,
  2673.     like symlinks, or using a user-input regexp that might otherwise
  2674.     blowup on you.
  2675.  
  2676.  
  2677. 4.9) How do I catch signals in perl?
  2678.  
  2679.     Perl allows you to trap signals using the %SIG associative array.
  2680.     Using the signals you want to trap as the key, you can assign a
  2681.     subroutine to that signal.  The %SIG array will only contain those
  2682.     values which the programmer defines.  Therefore, you do not have to
  2683.     assign all signals.  For example, to exit cleanly from a ^C:
  2684.  
  2685.     $SIG{'INT'} = 'CLEANUP';
  2686.     sub CLEANUP {
  2687.         print "\n\nCaught Interrupt (^C), Aborting\n";
  2688.         exit(1);
  2689.     }
  2690.  
  2691.     There are two special "routines" for signals called DEFAULT and IGNORE.
  2692.     DEFAULT erases the current assignment, restoring the default value of
  2693.     the signal.  IGNORE causes the signal to be ignored.  In general, you
  2694.     don't need to remember these as you can emulate their functionality
  2695.     with standard programming features.  DEFAULT can be emulated by
  2696.     deleting the signal from the array and IGNORE can be emulated by any
  2697.     undeclared subroutine.
  2698.  
  2699. 4.10) Why doesn't Perl interpret my octal data octally?
  2700.  
  2701.     Perl only understands octal and hex numbers as such when they occur
  2702.     as literals in your program.  If they are read in from somewhere and
  2703.     assigned, then no automatic conversion takes place.  You must
  2704.     explicitly use oct() or hex() if you want this kind of thing to happen.
  2705.     Actually, oct() knows to interpret both hex and octal numbers, while
  2706.     hex only converts hexadecimal ones.  For example:
  2707.  
  2708.     {
  2709.         print "What mode would you like? ";
  2710.         $mode = <STDIN>;
  2711.         $mode = oct($mode);
  2712.         unless ($mode) {
  2713.         print "You can't really want mode 0!\n";
  2714.         redo;
  2715.         }
  2716.         chmod $mode, $file;
  2717.     }
  2718.  
  2719.     Without the octal conversion, a requested mode of 755 would turn
  2720.     into 01363, yielding bizarre file permissions of --wxrw--wt.
  2721.  
  2722.     If you want something that handles decimal, octal and hex input,
  2723.     you could follow the suggestion in the man page and use:
  2724.  
  2725.     $val = oct($val) if $val =~ /^0/;
  2726.  
  2727.  
  2728. 4.11) How can I compare two date strings?
  2729.  
  2730.     If the dates are in an easily parsed, predetermined format, then you
  2731.     can break them up into their component parts and call &timelocal from
  2732.     the distributed perl library.  If the date strings are in arbitrary
  2733.     formats, however, it's probably easier to use the getdate program from
  2734.     the Cnews distribution, since it accepts a wide variety of dates.  Note
  2735.     that in either case the return values you will really be comparing will
  2736.     be the total time in seconds as returned by time().
  2737.  
  2738.     Here's a getdate function for perl that's not very efficient; you can
  2739.     do better than this by sending it many dates at once or modifying
  2740.     getdate to behave better on a pipe.  Beware the hardcoded pathname.
  2741.  
  2742.     sub getdate {
  2743.         local($_) = shift;
  2744.  
  2745.         s/-(\d{4})$/+$1/ || s/\+(\d{4})$/-$1/;
  2746.         # getdate has broken timezone sign reversal!
  2747.  
  2748.         $_ = `/usr/local/lib/news/newsbin/getdate '$_'`;
  2749.         chop;
  2750.         $_;
  2751.     }
  2752.  
  2753.     Richard Ohnemus <Rick_Ohnemus@Sterling.COM> actually has a getdate.y for
  2754.     use with the Perl yacc (see question 3.3 "Is there a yacc for Perl?").
  2755.  
  2756.     You might also consider using these:
  2757.  
  2758.     date.pl        - print dates how you want with the sysv +FORMAT method
  2759.     date.shar      - routines to manipulate and calculate dates
  2760.     ftp-chat2.shar - updated version of ftpget. includes library and demo
  2761.              programs
  2762.     getdate.shar   - returns number of seconds since epoch for any given
  2763.              date
  2764.     ptime.shar     - print dates how you want with the sysv +FORMAT method
  2765.  
  2766.     You probably want 'getdate.shar'... these and other files can be ftp'd
  2767.     from the /pub/perl/scripts directory on ftp.cis.ufl.edu. See the README
  2768.     file in the /pub/perl directory for time and the European mirror site
  2769.     details.
  2770.  
  2771.  
  2772. 4.12) How can I find the Julian Day?
  2773.  
  2774.     Here's an example of a Julian Date function provided by Thomas R.
  2775.     Kimpton*.
  2776.  
  2777.     #!/usr/local/bin/perl
  2778.  
  2779.     @theJulianDate = ( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 );
  2780.  
  2781.     #************************************************************************
  2782.     #****   Return 1 if we are after the leap day in a leap year.       *****
  2783.     #************************************************************************
  2784.  
  2785.     sub leapDay
  2786.     {
  2787.         my($year,$month,$day) = @_;
  2788.  
  2789.         if (year % 4) {
  2790.         return(0);
  2791.         }
  2792.  
  2793.         if (!(year % 100)) {             # years that are multiples of 100
  2794.                                      # are not leap years
  2795.         if (year % 400) {            # unless they are multiples of 400
  2796.             return(0);
  2797.         }
  2798.         }
  2799.         if (month < 2) {
  2800.             return(0);
  2801.         } elsif ((month == 2) && (day < 29)) {
  2802.         return(0);
  2803.         } else {
  2804.         return(1);
  2805.         }
  2806.     }
  2807.  
  2808.     #************************************************************************
  2809.     #****   Pass in the date, in seconds, of the day you want the       *****
  2810.     #****   julian date for.  If your localtime() returns the year day  *****
  2811.     #****   return that, otherwise figure out the julian date.          *****
  2812.     #************************************************************************
  2813.  
  2814.     sub julianDate
  2815.     {
  2816.         my($dateInSeconds) = @_;
  2817.         my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday);
  2818.  
  2819.         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) =
  2820.         localtime($dateInSeconds);
  2821.         if (defined($yday)) {
  2822.         return($yday+1);
  2823.         } else {
  2824.         return($theJulianDate[$mon] + $mday + &leapDay($year,$mon,$mday));
  2825.         }
  2826.  
  2827.     }
  2828.  
  2829.     print "Today's julian date is: ",&julianDate(time),"\n";
  2830.  
  2831.  
  2832. 4.13) What's the fastest way to code up a given task in perl?
  2833.  
  2834.     Post it to comp.lang.perl and ask Tom or Randal a question about it.
  2835.     ;)
  2836.  
  2837.     Because Perl so lends itself to a variety of different approaches for
  2838.     any given task, a common question is which is the fastest way to code a
  2839.     given task.  Since some approaches can be dramatically more efficient
  2840.     that others, it's sometimes worth knowing which is best.
  2841.     Unfortunately, the implementation that first comes to mind, perhaps as
  2842.     a direct translation from C or the shell, often yields suboptimal
  2843.     performance.  Not all approaches have the same results across different
  2844.     hardware and software platforms.  Furthermore, legibility must
  2845.     sometimes be sacrificed for speed.
  2846.  
  2847.     While an experienced perl programmer can sometimes eye-ball the code
  2848.     and make an educated guess regarding which way would be fastest,
  2849.     surprises can still occur.  So, in the spirit of perl programming
  2850.     being an empirical science, the best way to find out which of several
  2851.     different methods runs the fastest is simply to code them all up and
  2852.     time them. For example:
  2853.  
  2854.     $COUNT = 10_000; $| = 1;
  2855.  
  2856.     print "method 1: ";
  2857.  
  2858.         ($u, $s) = times;
  2859.         for ($i = 0; $i < $COUNT; $i++) {
  2860.         # code for method 1
  2861.         }
  2862.         ($nu, $ns) = times;
  2863.         printf "%8.4fu %8.4fs\n", ($nu - $u), ($ns - $s);
  2864.  
  2865.     print "method 2: ";
  2866.  
  2867.         ($u, $s) = times;
  2868.         for ($i = 0; $i < $COUNT; $i++) {
  2869.         # code for method 2
  2870.         }
  2871.         ($nu, $ns) = times;
  2872.         printf "%8.4fu %8.4fs\n", ($nu - $u), ($ns - $s);
  2873.  
  2874.     Perl5 includes a new module called Benchmark.pm.  You can now simplify
  2875.     the code to use the Benchmarking, like so:
  2876.  
  2877.         use Benchmark;
  2878.  
  2879.             timethese($count, {
  2880.                 Name1 => '...code for method 1...',
  2881.                 Name2 => '...code for method 2...',
  2882.                 ... });
  2883.  
  2884.     It will output something that looks similar to this:
  2885.  
  2886.         Benchmark: timing 100 iterations of Name1, Name2...
  2887.                 Name1:  2 secs (0.50 usr 0.00 sys = 0.50 cpu)
  2888.                 Name2:  1 secs (0.48 usr 0.00 sys = 0.48 cpu)
  2889.  
  2890.  
  2891.     For example, the following code will show the time difference between
  2892.     three different ways of assigning the first character of a string to
  2893.     a variable:
  2894.  
  2895.     use Benchmark;
  2896.     timethese(100000, {
  2897.         'regex1' => '$str="ABCD"; $str =~ s/^(.)//; $ch = $1',
  2898.         'regex2' => '$str="ABCD"; $str =~ s/^.//; $ch = $&',
  2899.         'substr' => '$str="ABCD"; $ch=substr($str,0,1);
  2900. substr($str,0,1)="",
  2901.     });
  2902.  
  2903.     The results will be returned like this:
  2904.  
  2905.     Benchmark: timing 100000 iterations of regex1, regex2, substr...
  2906.        regex1: 11 secs (10.80 usr   0.00 sys =  10.80 cpu)
  2907.        regex2: 10 secs (10.23 usr   0.00 sys =  10.23 cpu)
  2908.        substr:  7 secs ( 5.62 usr    0.00 sys =   5.62 cpu)
  2909.  
  2910.     For more specific tips, see the section on Efficiency in the
  2911.     ``Other Oddments'' chapter at the end of the Camel Book.
  2912.  
  2913.  
  2914. 4.14) Do I always/never have to quote my strings or use semicolons?
  2915.  
  2916.     You don't have to quote strings that can't mean anything else in the
  2917.     language, like identifiers with any upper-case letters in them.
  2918.     Therefore, it's fine to do this:
  2919.  
  2920.     $SIG{INT} = Timeout_Routine;
  2921.     or
  2922.  
  2923.     @Days = (Sun, Mon, Tue, Wed, Thu, Fri, Sat, Sun);
  2924.  
  2925.     but you can't get away with this:
  2926.  
  2927.     $foo{while} = until;
  2928.  
  2929.     in place of
  2930.  
  2931.     $foo{'while'} = 'until';
  2932.  
  2933.     The requirements on semicolons have been increasingly relaxed.  You no
  2934.     longer need one at the end of a block, but stylistically, you're better
  2935.     to use them if you don't put the curly brace on the same line:
  2936.  
  2937.     for (1..10) { print }
  2938.  
  2939.     is ok, as is
  2940.  
  2941.     @nlist = sort { $a <=> $b } @olist;
  2942.  
  2943.     but you probably shouldn't do this:
  2944.  
  2945.     for ($i = 0; $i < @a; $i++) {
  2946.         print "i is $i\n"  # <-- oops!
  2947.     }
  2948.  
  2949.     because you might want to add lines later, and anyway, it looks
  2950.     funny. :-)
  2951.  
  2952.  
  2953. 4.15) What is variable suicide and how can I prevent it?
  2954.  
  2955.     Variable suicide is a nasty side effect of dynamic scoping and the way
  2956.     variables are passed by reference.  If you say
  2957.  
  2958.     $x = 17;
  2959.     &munge($x);
  2960.     sub munge {
  2961.         local($x);
  2962.         local($myvar) = $_[0];
  2963.         ...
  2964.     }
  2965.  
  2966.     Then you have just clobbered $_[0]!  Why this is occurring is pretty
  2967.     heavy wizardry: the reference to $x stored in $_[0] was temporarily
  2968.     occluded by the previous local($x) statement (which, you're recall,
  2969.     occurs at run-time, not compile-time).  The work around is simple,
  2970.     however: declare your formal parameters first:
  2971.  
  2972.     sub munge {
  2973.         local($myvar) = $_[0];
  2974.         local($x);
  2975.         ...
  2976.     }
  2977.  
  2978.     That doesn't help you if you're going to be trying to access @_
  2979.     directly after the local()s.  In this case, careful use of the package
  2980.     facility is your only recourse.
  2981.  
  2982.     Another manifestation of this problem occurs due to the magical nature
  2983.     of the index variable in a foreach() loop.
  2984.  
  2985.     @num = 0 .. 4;
  2986.     print "num begin  @num\n";
  2987.     foreach $m (@num) { &ug }
  2988.     print "num finish @num\n";
  2989.     sub ug {
  2990.         local($m) = 42;
  2991.         print "m=$m  $num[0],$num[1],$num[2],$num[3]\n";
  2992.     }
  2993.  
  2994.     Which prints out the mysterious:
  2995.  
  2996.     num begin  0 1 2 3 4
  2997.     m=42  42,1,2,3
  2998.     m=42  0,42,2,3
  2999.     m=42  0,1,42,3
  3000.     m=42  0,1,2,42
  3001.     m=42  0,1,2,3
  3002.     num finish 0 1 2 3 4
  3003.  
  3004.     What's happening here is that $m is an alias for each element of @num.
  3005.     Inside &ug, you temporarily change $m.  Well, that means that you've
  3006.     also temporarily changed whatever $m is an alias to!!  The only
  3007.     workaround is to be careful with global variables, using packages,
  3008.     and/or just be aware of this potential in foreach() loops.
  3009.  
  3010.     The perl5 static autos via "my" will not have this problem.
  3011.  
  3012.  
  3013. 4.16) What does "Malformed command links" mean?
  3014.  
  3015.     This is a bug in 4.035.  While in general it's merely a cosmetic
  3016.     problem, it often comanifests with a highly undesirable coredumping
  3017.     problem.  Programs known to be affected by the fatal coredump include
  3018.     plum and pcops.  This bug has been fixed since 4.036.  It did not
  3019.     resurface in 5.000.
  3020.  
  3021.  
  3022. 4.17) How can I set up a footer format to be used with write()?
  3023.  
  3024.     While the $^ variable contains the name of the current header format,
  3025.     there is no corresponding mechanism to automatically do the same thing
  3026.     for a footer.  Not knowing how big a format is going to be until you
  3027.     evaluate it is one of the major problems.
  3028.  
  3029.     If you have a fixed-size footer, you can get footers by checking for
  3030.     line left on page ($-) before each write, and printing the footer
  3031.     yourself if necessary.
  3032.  
  3033.     Another strategy is to open a pipe to yourself, using open(KID, "|-")
  3034.     and always write()ing to the KID, who then postprocesses its STDIN to
  3035.     rearrange headers and footers however you like.  Not very convenient,
  3036.     but doable.
  3037.  
  3038.  
  3039. 4.18) Why does my Perl program keep growing in size?
  3040.  
  3041.     This is caused by a strange occurance that Larry has dubbed "feeping
  3042.     creaturism".  Larry is always adding one more feature, always getting
  3043.     Perl to handle one more problem.  Hence, it keeps growing.  Once you've
  3044.     worked with perl long enough, you will probably start to do the same
  3045.     thing.  You will then notice this problem as you see your scripts
  3046.     becoming larger and larger.
  3047.  
  3048.     Oh, wait... you meant a currently running program and it's stack size.
  3049.     Mea culpa, I misunderstood you.  ;)  While there may be a real memory
  3050.     leak in the Perl source code or even whichever malloc() you're using,
  3051.     common causes are incomplete eval()s or local()s in loops.
  3052.  
  3053.     An eval() which terminates in error due to a failed parsing will leave
  3054.     a bit of memory unusable.
  3055.  
  3056.     A local() inside a loop:
  3057.  
  3058.     for (1..100) {
  3059.         local(@array);
  3060.     }
  3061.  
  3062.     will build up 100 versions of @array before the loop is done.  The
  3063.     work-around is:
  3064.  
  3065.     local(@array);
  3066.     for (1..100) {
  3067.         undef @array;
  3068.     }
  3069.  
  3070.     Larry reports that this behavior is fixed for perl5.
  3071.  
  3072.  
  3073. 4.19) Can I do RPC in Perl?
  3074.  
  3075.     Yes, you can, since Perl has access to sockets.  An example of the rup
  3076.     program written in Perl can be found in the script ruptime.pl at the
  3077.     scripts archive on ftp.cis.ufl.edu.  I warn you, however, that it's not
  3078.     a pretty sight, as it's used nothing from h2ph or c2ph, so everything is
  3079.     utterly hard-wired.
  3080.  
  3081.  
  3082. 4.20) Why doesn't my program, which uses sockets, work under Solaris?
  3083.  
  3084.     For some strange and unknown reason, some of the socket constants have
  3085.     been changed in Solaris.  What is probably causing your problems is
  3086.     that SOCK_STEAM now has a value of 2, whereas SunOS 4.X uses a value of
  3087.     1.  Instead of hardcoding this value, you should require Sockets and
  3088.     use SOCK_STREAM.
  3089.  
  3090.  
  3091. 4.21) How can I quote a variable to use in a regexp?
  3092.  
  3093.     From the manual:
  3094.  
  3095.     $pattern =~ s/(\W)/\\$1/g;
  3096.  
  3097.     Now you can freely use /$pattern/ without fear of any unexpected meta-
  3098.     characters in it throwing off the search.  If you don't know whether a
  3099.     pattern is valid or not, enclose it in an eval to avoid a fatal run-
  3100.     time error.
  3101.  
  3102.     Perl5 provides a vastly improved way of doing this.  Simply use the
  3103.     new quotemeta character (\Q) within your variable.
  3104.  
  3105. 4.22) How can I change the first N letters of a string?
  3106.  
  3107.     Remember that the substr() function produces an lvalue, that is, it may
  3108.     be assigned to.  Therefore, to change the first character to an S, you
  3109.     could do this:
  3110.  
  3111.     substr($var,0,1) = 'S';
  3112.  
  3113.     This assumes that $[ is 0;  for a library routine where you can't know
  3114.     $[, you should use this instead:
  3115.  
  3116.     substr($var,$[,1) = 'S';
  3117.  
  3118.     While it would be slower, you could in this case use a substitute:
  3119.  
  3120.     $var =~ s/^./S/;
  3121.  
  3122.     But this won't work if the string is empty or its first character is a
  3123.     newline, which "." will never match.  So you could use this instead:
  3124.  
  3125.     $var =~ s/^[^\0]?/S/;
  3126.  
  3127.     To do things like translation of the first part of a string, use
  3128.     substr, as in:
  3129.  
  3130.     substr($var, $[, 10) =~ tr/a-z/A-Z/;
  3131.  
  3132.     If you don't know the length of what to translate, something like this
  3133.     works:
  3134.  
  3135.     /^(\S+)/ && substr($_,$[,length($1)) =~ tr/a-z/A-Z/;
  3136.  
  3137.     For some things it's convenient to use the /e switch of the substitute
  3138.     operator:
  3139.  
  3140.     s/^(\S+)/($tmp = $1) =~ tr#a-z#A-Z#, $tmp/e
  3141.  
  3142.     although in this case, it runs more slowly than does the previous
  3143.     example.
  3144.  
  3145.  
  3146. 4.23) Can I use Perl regular expressions to match balanced text?
  3147.  
  3148.     No, or at least, not by themselves.
  3149.  
  3150.     Regexps just aren't powerful enough.  Although Perl's patterns aren't
  3151.     strictly regular because they do backreferencing (the \1 notation), you
  3152.     still can't do it.  You need to employ auxiliary logic.  A simple
  3153.     approach would involve keeping a bit of state around, something
  3154.     vaguely like this (although we don't handle patterns on the same line):
  3155.  
  3156.     while(<>) {
  3157.         if (/pat1/) {
  3158.         if ($inpat++ > 0) { warn "already saw pat1" }
  3159.         redo;
  3160.         }
  3161.         if (/pat2/) {
  3162.         if (--$inpat < 0) { warn "never saw pat1" }
  3163.         redo;
  3164.         }
  3165.     }
  3166.  
  3167.     A rather more elaborate subroutine to pull out balanced and possibly
  3168.     nested single chars, like ` and ', { and }, or ( and ) can be found
  3169.     on convex.com in /pub/perl/scripts/pull_quotes.
  3170.  
  3171.  
  3172. 4.24) What does it mean that regexps are greedy?  How can I get around it?
  3173.  
  3174.     The basic idea behind regexps being greedy is that they will match the
  3175.     maximum amount of data that they can, sometimes resulting in incorrect
  3176.     or strange answers.
  3177.  
  3178.     For example, I recently came across something like this:
  3179.  
  3180.     $_="this (is) an (example) of multiple parens";
  3181.     while ( m#\((.*)\)#g ) {
  3182.         print "$1\n";
  3183.     }
  3184.  
  3185.     This code was supposed to match everything between a set of
  3186.     parentheses.  The expected output was:
  3187.  
  3188.     is
  3189.     example
  3190.  
  3191.     However, the backreference ($1) ended up containing "is) an (example",
  3192.     clearly not what was intended.
  3193.  
  3194.     In perl4, the way to stop this from happening is to use a negated
  3195.     group.  If the above example is rewritten as follows, the results are
  3196.     correct:
  3197.  
  3198.     while ( m#\(([^)]*)\)#g ) {
  3199.  
  3200.     In perl5 there is a new minimal matching metacharacter, '?'.  This
  3201.     character is added to the normal metacharacters to modify their
  3202.     behaviour, such as "*?", "+?", or even "??".  The example would now be
  3203.     written in the following style:
  3204.  
  3205.     while (m#\((.*?)\)#g )
  3206.  
  3207.     Hint: This new operator leads to a very elegant method of stripping
  3208.     comments from C code:
  3209.  
  3210.     s:/\*.*?\*/::gs
  3211.  
  3212.  
  3213. 4.25) How do I use a regular expression to strip C style comments from a
  3214.       file?
  3215.  
  3216.     Since we're talking about how to strip comments under perl5, now is a
  3217.     good time to talk about doing it in perl4.  The easiest way to strip
  3218.     comments in perl4 is to transform the comment close (*/) into something
  3219.     that can't be in the string, or is at least extremely unlikely to be in
  3220.     the string.  I find \256 (the registered or reserved sign, an R inside
  3221.     a circle) is fairly unlikely to be used and is easy to remember.  So,
  3222.     our code looks something like this:
  3223.  
  3224.     s:\*/:\256:g;        # Change all */ to circled R
  3225.     s:/\*[^\256]*\256::g;   # Remove everything from \* to circled R
  3226.     print;
  3227.  
  3228.     To ensure that you correctly handle multi-line comments, don't forget
  3229.     to set $* to 1, informing perl that it should do multi-line pattern
  3230.     matching.
  3231.  
  3232.     [Untested changes.  If it's wrong or you don't understand it, check
  3233.         with Jeff.  If it's wrong, let me know so I can change it. ]
  3234.  
  3235.     Jeff Friedl* suggests that the above solution is incorrect.  He says it
  3236.     will fail on imbedded comments and function proto-typing as well as on
  3237.     comments that are part of strings.  The following regexp should handle
  3238.     everything:
  3239.  
  3240.         $/ = undef;
  3241.         $_ = <>;
  3242.  
  3243.         s#/\*[^*]*\*+([^/*][^*]*\*+)*/|([^/"']*("[^"\\]*(\\[\d\D][^"\\]*)*"[^/"']*|'[^'\\]*(\\[\d\D][^'\\]*)*'[^/"']*|/+[^*/][^/"']*)*)#$2#g;
  3244.         print;
  3245.  
  3246.  
  3247. 4.26) Why doesn't "local($foo) = <FILE>;" work right?
  3248.  
  3249.     Well, it does.  The thing to remember is that local() provides an array
  3250.     context, and that the <FILE> syntax in an array context will read all the
  3251.     lines in a file.  To work around this, use:
  3252.  
  3253.     local($foo);
  3254.     $foo = <FILE>;
  3255.  
  3256.     You can use the scalar() operator to cast the expression into a scalar
  3257.     context:
  3258.  
  3259.     local($foo) = scalar(<FILE>);
  3260.  
  3261.  
  3262. 4.27) How can I detect keyboard input without reading it?
  3263.  
  3264.     You should check out the Frequently Asked Questions list in
  3265.     comp.unix.* for things like this: the answer is essentially the same.
  3266.     It's very system dependent.  Here's one solution that works on BSD
  3267.     systems:
  3268.  
  3269.     sub key_ready {
  3270.         local($rin, $nfd);
  3271.         vec($rin, fileno(STDIN), 1) = 1;
  3272.         return $nfd = select($rin,undef,undef,0);
  3273.     }
  3274.  
  3275.  
  3276. 4.28) How can I read a single character from the keyboard under UNIX and DOS?
  3277.  
  3278.     A closely related question to the no-echo question below is how to
  3279.     input a single character from the keyboard.  Again, this is a system
  3280.     dependent operation.  The following code may or may not help you.  It
  3281.     should work on both SysV and BSD flavors of UNIX:
  3282.  
  3283.     $BSD = -f '/vmunix';
  3284.     if ($BSD) {
  3285.         system "stty cbreak </dev/tty >/dev/tty 2>&1";
  3286.     }
  3287.     else {
  3288.         system "stty", '-icanon',
  3289.         system "stty", 'eol', "\001";
  3290.     }
  3291.  
  3292.     $key = getc(STDIN);
  3293.  
  3294.     if ($BSD) {
  3295.         system "stty -cbreak </dev/tty >/dev/tty 2>&1";
  3296.     }
  3297.     else {
  3298.         system "stty", 'icanon';
  3299.         system "stty", 'eol', '^@'; # ascii null
  3300.     }
  3301.     print "\n";
  3302.  
  3303.     You could also handle the stty operations yourself for speed if you're
  3304.     going to be doing a lot of them.  This code works to toggle cbreak
  3305.     and echo modes on a BSD system:
  3306.  
  3307.     sub set_cbreak { # &set_cbreak(1) or &set_cbreak(0)
  3308.     local($on) = $_[0];
  3309.     local($sgttyb,@ary);
  3310.     require 'sys/ioctl.ph';
  3311.     $sgttyb_t   = 'C4 S' unless $sgttyb_t;  # c2ph: &sgttyb'typedef()
  3312.  
  3313.     ioctl(STDIN,&TIOCGETP,$sgttyb) || die "Can't ioctl TIOCGETP: $!";
  3314.  
  3315.     @ary = unpack($sgttyb_t,$sgttyb);
  3316.     if ($on) {
  3317.         $ary[4] |= &CBREAK;
  3318.         $ary[4] &= ~&ECHO;
  3319.     } else {
  3320.         $ary[4] &= ~&CBREAK;
  3321.         $ary[4] |= &ECHO;
  3322.     }
  3323.     $sgttyb = pack($sgttyb_t,@ary);
  3324.  
  3325.     ioctl(STDIN,&TIOCSETP,$sgttyb) || die "Can't ioctl TIOCSETP: $!";
  3326.     }
  3327.  
  3328.     Note that this is one of the few times you actually want to use the
  3329.     getc() function; it's in general way too expensive to call for normal
  3330.     I/O.  Normally, you just use the <FILE> syntax, or perhaps the read()
  3331.     or sysread() functions.
  3332.  
  3333.     For perspectives on more portable solutions, use anon ftp to retrieve
  3334.     the file /pub/perl/info/keypress from convex.com.
  3335.  
  3336.     Under Perl5, with William Setzer's Curses module, you can call
  3337.     &Curses::cbreak() and &Curses::nocbreak() to turn cbreak mode on and
  3338.     off.  You can then use getc() to read each character.  This should work
  3339.     under both BSD and SVR systems.  If anyone can confirm or deny
  3340.     (especially William), please contact the maintainers.
  3341.  
  3342.     For DOS systems, Dan Carson <dbc@tc.fluke.COM> reports:
  3343.  
  3344.     To put the PC in "raw" mode, use ioctl with some magic numbers gleaned
  3345.     from msdos.c (Perl source file) and Ralf Brown's interrupt list (comes
  3346.     across the net every so often):
  3347.  
  3348.     $old_ioctl = ioctl(STDIN,0,0);     # Gets device info
  3349.     $old_ioctl &= 0xff;
  3350.     ioctl(STDIN,1,$old_ioctl | 32);    # Writes it back, setting bit 5
  3351.  
  3352.     Then to read a single character:
  3353.  
  3354.     sysread(STDIN,$c,1);               # Read a single character
  3355.  
  3356.     And to put the PC back to "cooked" mode:
  3357.  
  3358.     ioctl(STDIN,1,$old_ioctl);         # Sets it back to cooked mode.
  3359.  
  3360.  
  3361.     So now you have $c.  If ord($c) == 0, you have a two byte code, which
  3362.     means you hit a special key.  Read another byte (sysread(STDIN,$c,1)),
  3363.     and that value tells you what combination it was according to this
  3364.     table:
  3365.  
  3366.     # PC 2-byte keycodes = ^@ + the following:
  3367.  
  3368.     # HEX     KEYS
  3369.     # ---     ----
  3370.     # 0F      SHF TAB
  3371.     # 10-19   ALT QWERTYUIOP
  3372.     # 1E-26   ALT ASDFGHJKL
  3373.     # 2C-32   ALT ZXCVBNM
  3374.     # 3B-44   F1-F10
  3375.     # 47-49   HOME,UP,PgUp
  3376.     # 4B      LEFT
  3377.     # 4D      RIGHT
  3378.     # 4F-53   END,DOWN,PgDn,Ins,Del
  3379.     # 54-5D   SHF F1-F10
  3380.     # 5E-67   CTR F1-F10
  3381.     # 68-71   ALT F1-F10
  3382.     # 73-77   CTR LEFT,RIGHT,END,PgDn,HOME
  3383.     # 78-83   ALT 1234567890-=
  3384.     # 84      CTR PgUp
  3385.  
  3386.     This is all trial and error I did a long time ago, I hope I'm reading the
  3387.     file that worked.
  3388.  
  3389.  
  3390. 4.29) How can I get input from the keyboard without it echoing to the
  3391.       screen?
  3392.  
  3393.     Terminal echoing is generally handled directly by the shell.
  3394.     Therefore, there is no direct way in perl to turn echoing on and off.
  3395.     However, you can call the command "stty [-]echo".  The following will
  3396.     allow you to accept input without it being echoed to the screen, for
  3397.     example as a way to accept passwords (error checking deleted for
  3398.     brevity):
  3399.  
  3400.     print "Please enter your password: ";
  3401.         system("stty -echo");
  3402.     chop($password=<STDIN>);
  3403.     print "\n";
  3404.     system("stty echo");
  3405.  
  3406.     Again, under perl 5, you can use Curses and call &Curses::noecho() and
  3407.     &Curses::echo() to turn echoing off and on.
  3408.  
  3409.  
  3410. 4.30) Is there any easy way to strip blank space from the beginning/end of
  3411.     a string?
  3412.  
  3413.     Yes, there is.  Using the substitution command, you can match the
  3414.     blanks and replace it with nothing.  For example, if you have the
  3415.     string "     String     " you can use this:
  3416.  
  3417.         s/^\s+|\s+$//g;
  3418.  
  3419.     or even
  3420.  
  3421.         s/^\s+//; s/\s+$//;
  3422.  
  3423.     Note however that Jeffrey Friedl* says these are only good for shortish
  3424.     strings.  For longer strings, and worse-case scenarios, they tend to
  3425.     break-down and become inefficient.
  3426.  
  3427.     For the longer strings, he suggests using either
  3428.  
  3429.         $_ = $1 if m/^\s*((.*\S)?)/;
  3430.  
  3431.     or
  3432.  
  3433.         s/^\s*((.*\S)?)\s*$/$1/;
  3434.  
  3435.     It should also be noted that for generally nice strings, these tend to
  3436.     be noticably slower than the simple ones above.  It is suggested that
  3437.     you use whichever one will fit your situation best, understanding that
  3438.     the first examples will work in roughly ever situation known even if
  3439.     slow at times.
  3440. --
  3441. Stephen P Potter        spp@vx.com        Varimetrix Corporation
  3442. 2350 Commerce Park Drive, Suite 4                Palm Bay, FL 32905
  3443. (407) 676-3222                           CAD/CAM/CAE/Software
  3444.  
  3445.  
  3446.  
  3447. Archive-name: perl-faq/part5
  3448. Version: $Id: part5,v 2.4 1995/01/31 14:46:00 spp Exp spp $
  3449. Posting-Frequency: bi-weekly
  3450. Change-Log: $Log: part5,v $
  3451. # Revision 2.4  1995/01/31  14:46:00  spp
  3452. # 12/12/94 -  Added question about retaining return codes from backticked
  3453. #             commands.
  3454. # 01/31/95 -  Added 5.24
  3455. #
  3456.  
  3457.  
  3458. This posting contains answers to the following questions about Array, Shell
  3459. and External Program Interactions with Perl:
  3460.  
  3461.  
  3462. 5.1) What is the difference between $array[1] and @array[1]?
  3463.  
  3464.     Always make sure to use a $ for single values and @ for multiple ones.
  3465.     Thus element 2 of the @foo array is accessed as $foo[2], not @foo[2],
  3466.     which is a list of length one (not a scalar), and is a fairly common
  3467.     novice mistake.  Sometimes you can get by with @foo[2], but it's
  3468.     not really doing what you think it's doing for the reason you think
  3469.     it's doing it, which means one of these days, you'll shoot yourself
  3470.     in the foot; ponder for a moment what these will really do:
  3471.     @foo[0] = `cmd args`;
  3472.     @foo[2] = <FILE>;
  3473.     Just always say $foo[2] and you'll be happier.
  3474.  
  3475.     This may seem confusing, but try to think of it this way:  you use the
  3476.     character of the type which you *want back*.  You could use @foo[1..3] for
  3477.     a slice of three elements of @foo, or even @foo{A,B,C} for a slice of
  3478.     of %foo.  This is the same as using ($foo[1], $foo[2], $foo[3]) and
  3479.     ($foo{A}, $foo{B}, $foo{C}) respectively.  In fact, you can even use
  3480.     lists to subscript arrays and pull out more lists, like @foo[@bar] or
  3481.     @foo{@bar}, where @bar is in both cases presumably a list of subscripts.
  3482.  
  3483.  
  3484. 5.2) How can I make an array of arrays or other recursive data types?
  3485.  
  3486.  
  3487.     In Perl5, it's quite easy to declare these things.  For example
  3488.  
  3489.     @A = (
  3490.         [ 'ww' .. 'xx'  ],
  3491.         [ 'xx' .. 'yy'  ],
  3492.         [ 'yy' .. 'zz'  ],
  3493.         [ 'zz' .. 'zzz' ],
  3494.     );
  3495.  
  3496.     And now reference $A[2]->[0] to pull out "yy".  These may also nest
  3497.     and mix with tables:
  3498.  
  3499.     %T = (
  3500.         key0, { k0, v0, k1, v1 },
  3501.         key1, { k2, v2, k3, v3 },
  3502.         key2, { k2, v2, k3, [ 0, 'a' .. 'z' ] },
  3503.     );
  3504.  
  3505.     Allowing you to reference $T{key2}->{k3}->[3] to pull out 'c'.
  3506.  
  3507.     Perl4 is infinitely more difficult.  Remember that Perl[0..4] isn't
  3508.     about nested data structures.  It's about flat ones, so if you're
  3509.     trying to do this, you may be going about it the wrong way or using the
  3510.     wrong tools.  You might try parallel arrays with common subscripts.
  3511.  
  3512.     But if you're bound and determined, you can use the multi-dimensional
  3513.     array emulation of $a{'x','y','z'}, or you can make an array of names
  3514.     of arrays and eval it.
  3515.  
  3516.     For example, if @name contains a list of names of arrays, you can get
  3517.     at a the j-th element of the i-th array like so:
  3518.  
  3519.     $ary = $name[$i];
  3520.     $val = eval "\$$ary[$j]";
  3521.  
  3522.     or in one line
  3523.  
  3524.     $val = eval "\$$name[$i][\$j]";
  3525.  
  3526.     You could also use the type-globbing syntax to make an array of *name
  3527.     values, which will be more efficient than eval.  Here @name hold a list
  3528.     of pointers, which we'll have to dereference through a temporary
  3529.     variable.
  3530.  
  3531.     For example:
  3532.  
  3533.     { local(*ary) = $name[$i]; $val = $ary[$j]; }
  3534.  
  3535.     In fact, you can use this method to make arbitrarily nested data
  3536.     structures.  You really have to want to do this kind of thing badly to
  3537.     go this far, however, as it is notationally cumbersome.
  3538.  
  3539.     Let's assume you just simply *have* to have an array of arrays of
  3540.     arrays.  What you do is make an array of pointers to arrays of
  3541.     pointers, where pointers are *name values described above.  You
  3542.     initialize the outermost array normally, and then you build up your
  3543.     pointers from there.  For example:
  3544.  
  3545.     @w = ( 'ww' .. 'xx' );
  3546.     @x = ( 'xx' .. 'yy' );
  3547.     @y = ( 'yy' .. 'zz' );
  3548.     @z = ( 'zz' .. 'zzz' );
  3549.  
  3550.     @ww = reverse @w;
  3551.     @xx = reverse @x;
  3552.     @yy = reverse @y;
  3553.     @zz = reverse @z;
  3554.  
  3555.     Now make a couple of arrays of pointers to these:
  3556.  
  3557.     @A = ( *w, *x, *y, *z );
  3558.     @B = ( *ww, *xx, *yy, *zz );
  3559.  
  3560.     And finally make an array of pointers to these arrays:
  3561.  
  3562.     @AAA = ( *A, *B );
  3563.  
  3564.     To access an element, such as AAA[i][j][k], you must do this:
  3565.  
  3566.     local(*foo) = $AAA[$i];
  3567.     local(*bar) = $foo[$j];
  3568.     $answer = $bar[$k];
  3569.  
  3570.     Similar manipulations on associative arrays are also feasible.
  3571.  
  3572.     You could take a look at recurse.pl package posted by Felix Lee*, which
  3573.     lets you simulate vectors and tables (lists and associative arrays) by
  3574.     using type glob references and some pretty serious wizardry.
  3575.  
  3576.     In C, you're used to creating recursive datatypes for operations like
  3577.     recursive decent parsing or tree traversal.  In Perl, these algorithms
  3578.     are best implemented using associative arrays.  Take an array called
  3579.     %parent, and build up pointers such that $parent{$person} is the name
  3580.     of that person's parent.  Make sure you remember that $parent{'adam'}
  3581.     is 'adam'. :-) With a little care, this approach can be used to
  3582.     implement general graph traversal algorithms as well.
  3583.  
  3584.  
  3585. 5.3) How do I make an array of structures containing various data types?
  3586.  
  3587.     This answer will work under perl5 only.  Did we mention that you should
  3588.     upgrade?  There is a perl4 solution, but you are using perl5 now,
  3589.     anyway, so there's no point in posting it.  Right?
  3590.  
  3591.     The best way to do this is to use an associative array to model your
  3592.     structure, then either a regular array (AKA list) or another
  3593.     associative array (AKA hash, table, or hash table) to store it.
  3594.  
  3595.     %foo = (
  3596.                'field1'        => "value1",
  3597.                    'field2'        => "value2",
  3598.                    'field3'        => "value3",
  3599.                    ...
  3600.                );
  3601.     ...
  3602.  
  3603.         @all = ( \%foo, \%bar, ... );
  3604.  
  3605.         print $all[0]{'field1'};
  3606.  
  3607.     Or even
  3608.  
  3609.     @all = (
  3610.            {
  3611.                'field1'        => "value1",
  3612.                'field2'        => "value2",
  3613.                'field3'        => "value3",
  3614.                ...
  3615.            },
  3616.            {
  3617.                'field1'        => "value1",
  3618.                'field2'        => "value2",
  3619.                'field3'        => "value3",
  3620.                ...
  3621.            },
  3622.            ...
  3623.     )
  3624.  
  3625.     See perlref(1).
  3626.  
  3627.  
  3628. 5.4) How can I extract just the unique elements of an array?
  3629.  
  3630.     There are several possible ways, depending on whether the
  3631.     array is ordered and you wish to preserve the ordering.
  3632.  
  3633.     a) If @in is sorted, and you want @out to be sorted:
  3634.  
  3635.     $prev = 'nonesuch';
  3636.     @out = grep($_ ne $prev && (($prev) = $_), @in);
  3637.  
  3638.        This is nice in that it doesn't use much extra memory,
  3639.        simulating uniq's behavior of removing only adjacent
  3640.        duplicates.
  3641.  
  3642.     b) If you don't know whether @in is sorted:
  3643.  
  3644.     undef %saw;
  3645.     @out = grep(!$saw{$_}++, @in);
  3646.  
  3647.     c) Like (b), but @in contains only small integers:
  3648.  
  3649.     @out = grep(!$saw[$_]++, @in);
  3650.  
  3651.     d) A way to do (b) without any loops or greps:
  3652.  
  3653.     undef %saw;
  3654.     @saw{@in} = ();
  3655.     @out = sort keys %saw;  # remove sort if undesired
  3656.  
  3657.     e) Like (d), but @in contains only small positive integers:
  3658.  
  3659.     undef @ary;
  3660.     @ary[@in] = @in;
  3661.     @out = sort @ary;
  3662.  
  3663.  
  3664. 5.5) How can I tell whether an array contains a certain element?
  3665.  
  3666.     There are several ways to approach this.  If you are going to make
  3667.     this query many times and the values are arbitrary strings, the
  3668.     fastest way is probably to invert the original array and keep an
  3669.     associative array lying about whose keys are the first array's values.
  3670.  
  3671.     @blues = ('turquoise', 'teal', 'lapis lazuli');
  3672.     undef %is_blue;
  3673.     for (@blues) { $is_blue{$_} = 1; }
  3674.  
  3675.     Now you can check whether $is_blue{$some_color}.  It might have been
  3676.     a good idea to keep the blues all in an assoc array in the first place.
  3677.  
  3678.     If the values are all small integers, you could use a simple
  3679.     indexed array.  This kind of an array will take up less space:
  3680.  
  3681.     @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
  3682.     undef @is_tiny_prime;
  3683.     for (@primes) { $is_tiny_prime[$_] = 1; }
  3684.  
  3685.     Now you check whether $is_tiny_prime[$some_number].
  3686.  
  3687.     If the values in question are integers instead of strings, you can save
  3688.     quite a lot of space by using bit strings instead:
  3689.  
  3690.     @articles = ( 1..10, 150..2000, 2017 );
  3691.     undef $read;
  3692.     grep (vec($read,$_,1) = 1, @articles);
  3693.  
  3694.     Now check whether vec($read,$n,1) is true for some $n.
  3695.  
  3696.  
  3697. 5.6) How do I sort an associative array by value instead of by key?
  3698.  
  3699.     You have to declare a sort subroutine to do this, or use an inline
  3700.     function.  Let's assume you want an ASCII sort on the values of the
  3701.     associative array %ary.  You could do so this way:
  3702.  
  3703.     foreach $key (sort by_value keys %ary) {
  3704.         print $key, '=', $ary{$key}, "\n";
  3705.     }
  3706.     sub by_value { $ary{$a} cmp $ary{$b}; }
  3707.  
  3708.     If you wanted a descending numeric sort, you could do this:
  3709.  
  3710.     sub by_value { $ary{$b} <=> $ary{$a}; }
  3711.  
  3712.     You can also inline your sort function, like this, at least if
  3713.     you have a relatively recent patchlevel of perl4 or are running perl5:
  3714.  
  3715.     foreach $key ( sort { $ary{$b} <=> $ary{$a} } keys %ary ) {
  3716.         print $key, '=', $ary{$key}, "\n";
  3717.     }
  3718.  
  3719.     If you wanted a function that didn't have the array name hard-wired
  3720.     into it, you could so this:
  3721.  
  3722.     foreach $key (&sort_by_value(*ary)) {
  3723.         print $key, '=', $ary{$key}, "\n";
  3724.     }
  3725.     sub sort_by_value {
  3726.         local(*x) = @_;
  3727.         sub _by_value { $x{$a} cmp $x{$b}; }
  3728.         sort _by_value keys %x;
  3729.     }
  3730.  
  3731.     If you want neither an alphabetic nor a numeric sort, then you'll
  3732.     have to code in your own logic instead of relying on the built-in
  3733.     signed comparison operators "cmp" and "<=>".
  3734.  
  3735.     Note that if you're sorting on just a part of the value, such as a
  3736.     piece you might extract via split, unpack, pattern-matching, or
  3737.     substr, then rather than performing that operation inside your sort
  3738.     routine on each call to it, it is significantly more efficient to
  3739.     build a parallel array of just those portions you're sorting on, sort
  3740.     the indices of this parallel array, and then to subscript your original
  3741.     array using the newly sorted indices.  This method works on both
  3742.     regular and associative arrays, since both @ary[@idx] and @ary{@idx}
  3743.     make sense.  See page 245 in the Camel Book on "Sorting an Array by a
  3744.     Computable Field" for a simple example of this.
  3745.  
  3746.  
  3747. 5.7) How can I know how many entries are in an associative array?
  3748.  
  3749.     While the number of elements in a @foobar array is simply @foobar when
  3750.     used in a scalar, you can't figure out how many elements are in an
  3751.     associative array in an analogous fashion.  That's because %foobar in
  3752.     a scalar context returns the ratio (as a string) of number of buckets
  3753.     filled versus the number allocated.  For example, scalar(%ENV) might
  3754.     return "20/32".  While perl could in theory keep a count, this would
  3755.     break down on associative arrays that have been bound to dbm files.
  3756.  
  3757.     However, while you can't get a count this way, one thing you *can* use
  3758.     it for is to determine whether there are any elements whatsoever in
  3759.     the array, since "if (%table)" is guaranteed to be false if nothing
  3760.     has ever been stored in it.
  3761.  
  3762.     As of perl4.035, you can says
  3763.  
  3764.         $count = keys %ARRAY;
  3765.  
  3766.     keys() when used in a scalar context will return the number of keys,
  3767.     rather than the keys themselves.
  3768.  
  3769.  
  3770. 5.8) What's the difference between "delete" and "undef" with %arrays?
  3771.  
  3772.     Pictures help...  here's the %ary table:
  3773.  
  3774.           keys  values
  3775.         +------+------+
  3776.         |  a   |  3   |
  3777.         |  x   |  7   |
  3778.         |  d   |  0   |
  3779.         |  e   |  2   |
  3780.         +------+------+
  3781.  
  3782.     And these conditions hold
  3783.  
  3784.         $ary{'a'}                       is true
  3785.         $ary{'d'}                       is false
  3786.         defined $ary{'d'}               is true
  3787.         defined $ary{'a'}               is true
  3788.         exists $ary{'a'}            is true (perl5 only)
  3789.         grep ($_ eq 'a', keys %ary)     is true
  3790.  
  3791.     If you now say
  3792.  
  3793.         undef $ary{'a'}
  3794.  
  3795.     your table now reads:
  3796.  
  3797.  
  3798.           keys  values
  3799.         +------+------+
  3800.         |  a   | undef|
  3801.         |  x   |  7   |
  3802.         |  d   |  0   |
  3803.         |  e   |  2   |
  3804.         +------+------+
  3805.  
  3806.     and these conditions now hold; changes in caps:
  3807.  
  3808.         $ary{'a'}                       is FALSE
  3809.         $ary{'d'}                       is false
  3810.         defined $ary{'d'}               is true
  3811.         defined $ary{'a'}               is FALSE
  3812.         exists $ary{'a'}            is true (perl5 only)
  3813.         grep ($_ eq 'a', keys %ary)     is true
  3814.  
  3815.     Notice the last two: you have an undef value, but a defined key!
  3816.  
  3817.     Now, consider this:
  3818.  
  3819.         delete $ary{'a'}
  3820.  
  3821.     your table now reads:
  3822.  
  3823.           keys  values
  3824.         +------+------+
  3825.         |  x   |  7   |
  3826.         |  d   |  0   |
  3827.         |  e   |  2   |
  3828.         +------+------+
  3829.  
  3830.     and these conditions now hold; changes in caps:
  3831.  
  3832.         $ary{'a'}                       is false
  3833.         $ary{'d'}                       is false
  3834.         defined $ary{'d'}               is true
  3835.         defined $ary{'a'}               is false
  3836.         exists $ary{'a'}            is FALSE (perl5 only)
  3837.         grep ($_ eq 'a', keys %ary)     is FALSE
  3838.  
  3839.     See, the whole entry is gone!
  3840.  
  3841.  
  3842. 5.9) Why don't backticks work as they do in shells?
  3843.  
  3844.     Several reason.  One is because backticks do not interpolate within
  3845.     double quotes in Perl as they do in shells.
  3846.  
  3847.     Let's look at two common mistakes:
  3848.  
  3849.          $foo = "$bar is `wc $file`";  # WRONG
  3850.  
  3851.     This should have been:
  3852.  
  3853.      $foo = "$bar is " . `wc $file`;
  3854.  
  3855.     But you'll have an extra newline you might not expect.  This
  3856.     does not work as expected:
  3857.  
  3858.       $back = `pwd`; chdir($somewhere); chdir($back); # WRONG
  3859.  
  3860.     Because backticks do not automatically eat trailing or embedded
  3861.     newlines.  The chop() function will remove the last character from
  3862.     a string.  This should have been:
  3863.  
  3864.       chop($back = `pwd`); chdir($somewhere); chdir($back);
  3865.  
  3866.     You should also be aware that while in the shells, embedding
  3867.     single quotes will protect variables, in Perl, you'll need
  3868.     to escape the dollar signs.
  3869.  
  3870.     Shell: foo=`cmd 'safe $dollar'`
  3871.     Perl:  $foo=`cmd 'safe \$dollar'`;
  3872.  
  3873.  
  3874. 5.10) How come my converted awk/sed/sh script runs more slowly in Perl?
  3875.  
  3876.     The natural way to program in those languages may not make for the fastest
  3877.     Perl code.  Notably, the awk-to-perl translator produces sub-optimal code;
  3878.     see the a2p man page for tweaks you can make.
  3879.  
  3880.     Two of Perl's strongest points are its associative arrays and its regular
  3881.     expressions.  They can dramatically speed up your code when applied
  3882.     properly.  Recasting your code to use them can help a lot.
  3883.  
  3884.     How complex are your regexps?  Deeply nested sub-expressions with {n,m} or
  3885.     * operators can take a very long time to compute.  Don't use ()'s unless
  3886.     you really need them.  Anchor your string to the front if you can.
  3887.  
  3888.     Something like this:
  3889.     next unless /^.*%.*$/;
  3890.     runs more slowly than the equivalent:
  3891.     next unless /%/;
  3892.  
  3893.     Note that this:
  3894.     next if /Mon/;
  3895.     next if /Tue/;
  3896.     next if /Wed/;
  3897.     next if /Thu/;
  3898.     next if /Fri/;
  3899.     runs faster than this:
  3900.     next if /Mon/ || /Tue/ || /Wed/ || /Thu/ || /Fri/;
  3901.     which in turn runs faster than this:
  3902.     next if /Mon|Tue|Wed|Thu|Fri/;
  3903.     which runs *much* faster than:
  3904.     next if /(Mon|Tue|Wed|Thu|Fri)/;
  3905.  
  3906.     There's no need to use /^.*foo.*$/ when /foo/ will do.
  3907.  
  3908.     Remember that a printf costs more than a simple print.
  3909.  
  3910.     Don't split() every line if you don't have to.
  3911.  
  3912.     Another thing to look at is your loops.  Are you iterating through
  3913.     indexed arrays rather than just putting everything into a hashed
  3914.     array?  For example,
  3915.  
  3916.     @list = ('abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stv');
  3917.  
  3918.     for $i ($[ .. $#list) {
  3919.         if ($pattern eq $list[$i]) { $found++; }
  3920.     }
  3921.  
  3922.     First of all, it would be faster to use Perl's foreach mechanism
  3923.     instead of using subscripts:
  3924.  
  3925.     foreach $elt (@list) {
  3926.         if ($pattern eq $elt) { $found++; }
  3927.     }
  3928.  
  3929.     Better yet, this could be sped up dramatically by placing the whole
  3930.     thing in an associative array like this:
  3931.  
  3932.     %list = ('abc', 1, 'def', 1, 'ghi', 1, 'jkl', 1,
  3933.          'mno', 1, 'pqr', 1, 'stv', 1 );
  3934.     $found += $list{$pattern};
  3935.  
  3936.     (but put the %list assignment outside of your input loop.)
  3937.  
  3938.     You should also look at variables in regular expressions, which is
  3939.     expensive.  If the variable to be interpolated doesn't change over the
  3940.     life of the process, use the /o modifier to tell Perl to compile the
  3941.     regexp only once, like this:
  3942.  
  3943.     for $i (1..100) {
  3944.         if (/$foo/o) {
  3945.         &some_func($i);
  3946.         }
  3947.     }
  3948.  
  3949.     Finally, if you have a bunch of patterns in a list that you'd like to
  3950.     compare against, instead of doing this:
  3951.  
  3952.     @pats = ('_get.*', 'bogus', '_read', '.*exit', '_write');
  3953.     foreach $pat (@pats) {
  3954.         if ( $name =~ /^$pat$/ ) {
  3955.         &some_func();
  3956.         last;
  3957.         }
  3958.     }
  3959.  
  3960.     If you build your code and then eval it, it will be much faster.
  3961.     For example:
  3962.  
  3963.     @pats = ('_get.*', 'bogus', '_read', '.*exit', '_write');
  3964.     $code = <<EOS
  3965.         while (<>) {
  3966.             study;
  3967. EOS
  3968.     foreach $pat (@pats) {
  3969.         $code .= <<EOS
  3970.         if ( /^$pat\$/ ) {
  3971.             &some_func();
  3972.             next;
  3973.         }
  3974. EOS
  3975.     }
  3976.     $code .= "}\n";
  3977.     print $code if $debugging;
  3978.     eval $code;
  3979.  
  3980.  
  3981.  
  3982. 5.11) How can I call my system's unique C functions from Perl?
  3983.  
  3984.     If these are system calls and you have the syscall() function, then
  3985.     you're probably in luck -- see the next question.  If you're using a
  3986.     POSIX function, and are running perl5, you're also in luck: see
  3987.     POSIX(3m).  For arbitrary library functions, however, it's not quite so
  3988.     straight-forward.  See "Where can I learn about linking C with Perl?".
  3989.  
  3990.  
  3991. 5.12) Where do I get the include files to do ioctl() or syscall()? [h2ph]
  3992.  
  3993.     [Note: as of perl5, you probably want to just use h2xs instead, at
  3994.     least, if your system supports dynamic loading.]
  3995.  
  3996.     These are generated from your system's C include files using the h2ph
  3997.     script (once called makelib) from the Perl source directory.  This will
  3998.     make files containing subroutine definitions, like &SYS_getitimer, which
  3999.     you can use as arguments to your function.
  4000.  
  4001.     You might also look at the h2pl subdirectory in the Perl source for how to
  4002.     convert these to forms like $SYS_getitimer; there are both advantages and
  4003.     disadvantages to this.  Read the notes in that directory for details.
  4004.  
  4005.     In both cases, you may well have to fiddle with it to make these work; it
  4006.     depends how funny-looking your system's C include files happen to be.
  4007.  
  4008.     If you're trying to get at C structures, then you should take a look
  4009.     at using c2ph, which uses debugger "stab" entries generated by your
  4010.     BSD or GNU C compiler to produce machine-independent perl definitions
  4011.     for the data structures.  This allows to you avoid hardcoding
  4012.     structure layouts, types, padding, or sizes, greatly enhancing
  4013.     portability.  c2ph comes with the perl distribution.  On an SCO
  4014.     system, GCC only has COFF debugging support by default, so you'll have
  4015.     to build GCC 2.1 with DBX_DEBUGGING_INFO defined, and use -gstabs to
  4016.     get c2ph to work there.
  4017.  
  4018.     See the file /pub/perl/info/ch2ph on convex.com via anon ftp
  4019.     for more traps and tips on this process.
  4020.  
  4021.  
  4022. 5.13) Why do setuid Perl scripts complain about kernel problems?
  4023.  
  4024.     This message:
  4025.  
  4026.     YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!
  4027.     FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!
  4028.  
  4029.     is triggered because setuid scripts are inherently insecure due to a
  4030.     kernel bug.  If your system has fixed this bug, you can compile Perl
  4031.     so that it knows this.  Otherwise, create a setuid C program that just
  4032.     execs Perl with the full name of the script.  Here's what the
  4033.     perldiag(1) man page says about this message:
  4034.  
  4035.     YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!
  4036.       (F) And you probably never will, since you probably don't have
  4037.           the sources to your kernel, and your vendor probably doesn't
  4038.           give a rip about what you want.  Your best bet is to use the
  4039.           wrapsuid script in the eg directory to put a setuid C wrapper
  4040.           around your script.
  4041.  
  4042.  
  4043. 5.14) How do I open a pipe both to and from a command?
  4044.  
  4045.     In general, this is a dangerous move because you can find yourself in a
  4046.     deadlock situation.  It's better to put one end of the pipe to a file.
  4047.     For example:
  4048.  
  4049.     # first write some_cmd's input into a_file, then
  4050.     open(CMD, "some_cmd its_args < a_file |");
  4051.     while (<CMD>) {
  4052.  
  4053.     # or else the other way; run the cmd
  4054.     open(CMD, "| some_cmd its_args > a_file");
  4055.     while ($condition) {
  4056.         print CMD "some output\n";
  4057.         # other code deleted
  4058.     }
  4059.     close CMD || warn "cmd exited $?";
  4060.  
  4061.     # now read the file
  4062.     open(FILE,"a_file");
  4063.     while (<FILE>) {
  4064.  
  4065.     If you have ptys, you could arrange to run the command on a pty and
  4066.     avoid the deadlock problem.  See the chat2.pl package in the
  4067.     distributed library for ways to do this.
  4068.  
  4069.     At the risk of deadlock, it is theoretically possible to use a
  4070.     fork, two pipe calls, and an exec to manually set up the two-way
  4071.     pipe.  (BSD system may use socketpair() in place of the two pipes,
  4072.     but this is not as portable.)  The open2 library function distributed
  4073.     with the current perl release will do this for you.
  4074.  
  4075.     It assumes it's going to talk to something like adb, both writing to
  4076.     it and reading from it.  This is presumably safe because you "know"
  4077.     that commands like adb will read a line at a time and output a line at
  4078.     a time.  Programs like sort that read their entire input stream first,
  4079.     however, are quite apt to cause deadlock.
  4080.  
  4081.     There's also an open3.pl library that handles this for stderr as well.
  4082.  
  4083.  
  4084. 5.15) How can I capture STDERR from an external command?
  4085.  
  4086.     There are three basic ways of running external commands:
  4087.  
  4088.     system $cmd;
  4089.     $output = `$cmd`;
  4090.     open (PIPE, "cmd |");
  4091.  
  4092.     In the first case, both STDOUT and STDERR will go the same place as
  4093.     the script's versions of these, unless redirected.  You can always put
  4094.     them where you want them and then read them back when the system
  4095.     returns.  In the second and third cases, you are reading the STDOUT
  4096.     *only* of your command.  If you would like to have merged STDOUT and
  4097.     STDERR, you can use shell file-descriptor redirection to dup STDERR to
  4098.     STDOUT:
  4099.  
  4100.     $output = `$cmd 2>&1`;
  4101.     open (PIPE, "cmd 2>&1 |");
  4102.  
  4103.     Another possibility is to run STDERR into a file and read the file
  4104.     later, as in
  4105.  
  4106.     $output = `$cmd 2>some_file`;
  4107.     open (PIPE, "cmd 2>some_file |");
  4108.  
  4109.     Here's a way to read from both of them and know which descriptor
  4110.     you got each line from.  The trick is to pipe only STDERR through
  4111.     sed, which then marks each of its lines, and then sends that
  4112.     back into a merged STDOUT/STDERR stream, from which your Perl program
  4113.     then reads a line at a time:
  4114.  
  4115.         open (CMD,
  4116.           "cmd args | sed 's/^/STDOUT:/' |");
  4117.  
  4118.         while (<CMD>) {
  4119.           if (s/^STDOUT://)  {
  4120.               print "line from stdout: ", $_;
  4121.           } else {
  4122.               print "line from stdeff: ", $_;
  4123.           }
  4124.         }
  4125.  
  4126.     Be apprised that you *must* use Bourne shell redirection syntax in
  4127.     backticks, not csh!  For details on how lucky you are that perl's
  4128.     system() and backtick and pipe opens all use Bourne shell, fetch the
  4129.     file from convex.com called /pub/csh.whynot -- and you'll be glad that
  4130.     perl's shell interface is the Bourne shell.
  4131.  
  4132.     There's an &open3 routine out there which was merged with &open2 in
  4133.     perl5 production.
  4134.  
  4135.  
  4136. 5.16) Why doesn't open return an error when a pipe open fails?
  4137.  
  4138.     These statements:
  4139.  
  4140.     open(TOPIPE, "|bogus_command") || die ...
  4141.     open(FROMPIPE, "bogus_command|") || die ...
  4142.  
  4143.     will not fail just for lack of the bogus_command.  They'll only
  4144.     fail if the fork to run them fails, which is seldom the problem.
  4145.  
  4146.     If you're writing to the TOPIPE, you'll get a SIGPIPE if the child
  4147.     exits prematurely or doesn't run.  If you are reading from the
  4148.     FROMPIPE, you need to check the close() to see what happened.
  4149.  
  4150.     If you want an answer sooner than pipe buffering might otherwise
  4151.     afford you, you can do something like this:
  4152.  
  4153.     $kid = open (PIPE, "bogus_command |");   # XXX: check defined($kid)
  4154.     (kill 0, $kid) || die "bogus_command failed";
  4155.  
  4156.     This works fine if bogus_command doesn't have shell metas in it, but
  4157.     if it does, the shell may well not have exited before the kill 0.  You
  4158.     could always introduce a delay:
  4159.  
  4160.     $kid = open (PIPE, "bogus_command </dev/null |");
  4161.     sleep 1;
  4162.     (kill 0, $kid) || die "bogus_command failed";
  4163.  
  4164.     but this is sometimes undesirable, and in any event does not guarantee
  4165.     correct behavior.  But it seems slightly better than nothing.
  4166.  
  4167.     Similar tricks can be played with writable pipes if you don't wish to
  4168.     catch the SIGPIPE.
  4169.  
  4170.  
  4171. 5.##) How do I capture the exit status from an external program?
  4172.  
  4173.     Perl provides a builtin variable which holds the status of the last
  4174.     backtick command: $?.  Here is exactly what the perlvar page says about
  4175.     this variable:
  4176.  
  4177.      $?      The status returned by the last pipe close, backtick
  4178.              (``) command, or system() operator.  Note that this
  4179.              is the status word returned by the wait() system
  4180.              call, so the exit value of the subprocess is
  4181.              actually ($? >> 8).  Thus on many systems, $? & 255
  4182.              gives which signal, if any, the process died from,
  4183.              and whether there was a core dump.  (Mnemonic:
  4184.              similar to sh and ksh.)
  4185.  
  4186.  
  4187. 5.17) Why can't my perl program read from STDIN after I gave it ^D (EOF) ?
  4188.  
  4189.     Because some stdio's set error and eof flags that need clearing.
  4190.  
  4191.     Try keeping around the seekpointer and go there, like this:
  4192.      $where = tell(LOG);
  4193.      seek(LOG, $where, 0);
  4194.  
  4195.     If that doesn't work, try seeking to a different part of the file and
  4196.     then back.  If that doesn't work, try seeking to a different part of
  4197.     the file, reading something, and then seeking back.  If that doesn't
  4198.     work, give up on your stdio package and use sysread.  You can't call
  4199.     stdio's clearerr() from Perl, so if you get EINTR from a signal
  4200.     handler, you're out of luck.  Best to just use sysread() from the
  4201.     start for the tty.
  4202.  
  4203.  
  4204. 5.18) How can I translate tildes in a filename?
  4205.  
  4206.     Perl doesn't expand tildes -- the shell (ok, some shells) do.
  4207.     The classic request is to be able to do something like:
  4208.  
  4209.     open(FILE, "~/dir1/file1");
  4210.     open(FILE, "~tchrist/dir1/file1");
  4211.  
  4212.     which doesn't work.  (And you don't know it, because you
  4213.     did a system call without an "|| die" clause! :-)
  4214.  
  4215.     If you *know* you're on a system with the csh, and you *know*
  4216.     that Larry hasn't internalized file globbing, then you could
  4217.     get away with
  4218.  
  4219.     $filename = <~tchrist/dir1/file1>;
  4220.  
  4221.     but that's pretty iffy.
  4222.  
  4223.     A better way is to do the translation yourself, as in:
  4224.  
  4225.     $filename =~ s#^~(\w+)(/.*)?$#(getpwnam($1))[7].$2#e;
  4226.  
  4227.     More robust and efficient versions that checked for error conditions,
  4228.     handed simple ~/blah notation, and cached lookups are all reasonable
  4229.     enhancements.
  4230.  
  4231.  
  4232. 5.19) How can I convert my shell script to Perl?
  4233.  
  4234.     Larry's standard answer is to send it through the shell to perl filter,
  4235.     otherwise known at tchrist@perl.com.  Contrary to popular belief, Tom
  4236.     Christiansen isn't a real person.  He is actually a highly advanced
  4237.     artificial intelligence experiment written by a graduate student at the
  4238.     University of Colorado.  Some of the earlier tasks he was programmed to
  4239.     perform included:
  4240.  
  4241.     * monitor comp.lang.perl and collect statistics on which questions
  4242.       were asked with which frequency and to respond to them with stock
  4243.       answers.  Tom's programming has since outgrown this paltry task,
  4244.       and it has been assigned to an undergraduate student from the
  4245.       University of Florida.  After all, we all know that student from
  4246.       UF aren't able to do much more than documentation anyway.  ;-)
  4247.     * convert shell programs to perl programs
  4248.  
  4249.     Actually, there is no automatic machine translator.  Even if there
  4250.     were, you wouldn't gain a lot, as most of the external programs would
  4251.     still get called.  It's the same problem as blind translation into C:
  4252.     you're still apt to be bogged down by exec()s.  You have to analyze
  4253.     the dataflow and algorithm and rethink it for optimal speedup.  It's
  4254.     not uncommon to see one, two, or even three orders of magnitude of
  4255.     speed difference between the brute-force and the recoded approaches.
  4256.  
  4257.  
  4258. 5.20) Can I use Perl to run a telnet or ftp session?
  4259.  
  4260.     Sure, you can connect directly to them using sockets, or you can run a
  4261.     session on a pty.  In either case, Randal's chat2 package, which is
  4262.     distributed with the perl source, will come in handly.  It address
  4263.     much the same problem space as Don Libes's expect package does.  Two
  4264.     examples of using managing an ftp session using chat2 can be found on
  4265.     convex.com in /pub/perl/scripts/ftp-chat2.shar .
  4266.  
  4267.     Caveat lector: chat2 is documented only by example, may not run on
  4268.     System V systems, and is subtly machine dependent both in its ideas
  4269.     of networking and in pseudottys.
  4270.  
  4271.     Randal also has code showing an example socket session for handling the
  4272.     telnet protocol.  You might nudge him for a copy.
  4273.  
  4274.     Gene Spafford* has a nice ftp library package that will help with ftp.
  4275.  
  4276.  
  4277. 5.21) Why do I somestimes get an "Arguments too long" when I use <*>?
  4278.  
  4279.     As of perl4.036, there is a certain amount of globbing that is passed
  4280.     out to the shell and not handled internally.  The following code (which
  4281.     will, roughly, emulate "chmod 0644 *")
  4282.  
  4283.     while (<*>) {
  4284.         chmod 0644, $_;
  4285.     }
  4286.  
  4287.     is the equivalent of
  4288.  
  4289.     open(FOO, "echo * | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
  4290.     while (<FOO>) {
  4291.         chop;
  4292.         chmod 0644, $_;
  4293.     }
  4294.  
  4295.     Until globbing is built into Perl, you will need to use some form of
  4296.     non-globbing work around.
  4297.  
  4298.     Something like the following will work:
  4299.  
  4300.     opendir(DIR,'.');
  4301.     chmod 0644, grep(/\.c$/, readdir(DIR));
  4302.     closedir(DIR);
  4303.  
  4304.     This example is taken directly from "Programming Perl" page 78.
  4305.  
  4306.     If you've installed tcsh as /bin/csh, you'll never have this problem.
  4307.  
  4308. 5.22) How do I do a "tail -f" in Perl?
  4309.  
  4310.     Larry says that the solution is to put a call to seek in yourself.
  4311.     First try
  4312.  
  4313.         seek(GWFILE, 0, 1);
  4314.  
  4315.     If that doesn't work (depends on your stdio implementation), then
  4316.     you need something more like this:
  4317.  
  4318.  
  4319.     for (;;) {
  4320.     for ($curpos = tell(GWFILE); $_ = <GWFILE>; $curpos = tell(GWFILE)) {
  4321.         # search for some stuff and put it into files
  4322.     }
  4323.     sleep for a while
  4324.     seek(GWFILE, $curpos, 0);
  4325.     }
  4326.  
  4327.  
  4328. 5.23) Is there a way to hide perl's command line from programs such as "ps"?
  4329.  
  4330.     Generally speaking, if you need to do this you're either using poor
  4331.     programming practices or are far too paranoid for your own good.  If you
  4332.     need to do this to hide a password being entered on the command line,
  4333.     recode the program to read the password from a file or to prompt for
  4334.     it. (see question 4.24)  Typing a password on the command line is
  4335.     inherently insecure as anyone can look over your shoulder to see it.
  4336.  
  4337.     If you feel you really must overwrite the command line and hide it, you
  4338.     can assign to the variable "$0".  For example:
  4339.  
  4340.         #!/usr/local/bin/perl
  4341.         $0 = "Hidden from prying eyes";
  4342.  
  4343.         open(PS, "ps") || die "Can't PS: $!";
  4344.         while (<PS>) {
  4345.             next unless m/$$/;
  4346.             print
  4347.         }
  4348.  
  4349.     It should be noted that some OSes, like Solaris 2.X, read directly from
  4350.     the kernel information, instead of from the program's stack, and hence
  4351.     don't allow you to change the command line.
  4352.  
  4353.  
  4354. 5.24) I {changed directory, modified my environment} in a perl script.  How
  4355.       come the change disappeared when I exited the script?  How do I get
  4356.       my changes to be visible?
  4357.  
  4358.     In the strictest sense, it "can't" be done.  However, there is special
  4359.     shell magic which may allow you to do it.  I suggest checking out
  4360.     comp.unix.shell and reading the comp.unix.questions FAQ.
  4361.  
  4362.     When perl is started, you are creating a child process.  Due to the way
  4363.     the Unix system is designed, children cannot permanently affect their
  4364.     parent shells.
  4365.  
  4366.     When a child process is created, it inherits a copy of it's parents
  4367.     environment (variables, current directory, etc).  When the child
  4368.     changes this environment, it is changing the copy and not the original,
  4369.     so the parent isn't affected.
  4370.  
  4371.     If you must change the parent from within a perl script, you could try
  4372.     having it write out a shell script or a C-shell script and then using
  4373.     ". script" or "source script" (sh, Csh, respectively)
  4374.  
  4375. --
  4376. Stephen P Potter        spp@vx.com        Varimetrix Corporation
  4377. 2350 Commerce Park Drive, Suite 4                Palm Bay, FL 32905
  4378. (407) 676-3222                           CAD/CAM/CAE/Software
  4379.  
  4380.  
  4381.  
  4382.