home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / perl-faq / part3 < prev    next >
Internet Message Format  |  1996-01-29  |  28KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!paperboy.osf.org!bone.think.com!blanket.mitre.org!agate!cocoa.brown.edu!ride.ri.net!news3.near.net!paperboy.wellfleet.com!news-feed-1.peachnet.edu!gatech!newsfeed.internetmci.com!in2.uu.net!news1.radix.net!news1.radix.net!spp
  2. From: spp@psa.pencom.com
  3. Newsgroups: comp.lang.perl.announce,comp.lang.perl.misc,comp.answers,news.answers
  4. Subject: comp.lang.perl.* FAQ 3/5 - Programming Aids
  5. Followup-To: poster
  6. Date: 27 Jan 1996 01:25:01 GMT
  7. Organization: Pencom Systems Administration
  8. Lines: 653
  9. Approved: news-answers-request@MIT.EDU
  10. Distribution: world
  11. Message-ID: <SPP.96Jan26202501@syrinx.hideout.com>
  12. NNTP-Posting-Host: dialin23.annex1.radix.net
  13. Xref: senator-bedfellow.mit.edu comp.lang.perl.announce:240 comp.lang.perl.misc:18695 comp.answers:16705 news.answers:63313
  14.  
  15.  
  16. Archive-name: perl-faq/part3
  17. Version: $Id: part3,v 2.7 1995/05/15 15:44:43 spp Exp spp $
  18. Posting-Frequency: bi-weekly
  19. Last Edited: Thu Jan 11 00:55:42 1996 by spp (Stephen P Potter) on syrinx.psa.com
  20.  
  21. This posting contains answers to general information questions, mostly
  22. about programming aids.  
  23.  
  24.  
  25. 3.1) How can I use Perl interactively?
  26.     
  27.     The easiest way to do this is to run Perl under its debugger.  If you
  28.     have no program to debug, you can invoke the debugger on an `empty'
  29.     program like this: 
  30.  
  31.         perl -de 0
  32.  
  33.     (The more positive hackers prefer "perl -de 1". :-)
  34.  
  35.     Now you can type in any legal Perl code, and it will be immediately
  36.     evaluated.  You can also examine the symbol table, get stack
  37.     backtraces, check variable values, and if you want to, set breakpoints
  38.     and do the other things you can do in a symbolic debugger. 
  39.  
  40.  
  41. 3.2) Is there a Perl profiler?
  42.  
  43.     While there isn't one included with the perl source distribution (yet) 
  44.     various folks have written packages that allow you to do at least some 
  45.     sort of profiling.  The strategy usually includes modifying the perl 
  46.     debugger to handle profiling.  Authors of these packages include 
  47.  
  48.     Wayne Thompson         <me@anywhere.EBay.Sun.COM>
  49.     Ray Lischner         <lisch@sysserver1.mentor.com>
  50.     Kresten Krab Thorup     <krab@iesd.auc.dk>  
  51.  
  52.     The original articles by these folks containing their profilers are
  53.     available at ftp://convex.com/pub/perl/info/profiling.shar.
  54.  
  55.     Recently, Dean Roerich* has written a profiler for version 5 that
  56.     likely will be distributed with the standard release.  For now, it
  57.     should be available through any of the extension archives as
  58.     DProf.tar.gz.
  59.  
  60.  
  61. 3.3) Is there a yacc for Perl?
  62.  
  63.     Yes!! It's a version of Berkeley yacc that outputs Perl code instead
  64.     of C code!  You can get this from
  65.     ftp://ftp.sterling.com/local/perl-byacc1.8.2.tar.Z, or send the author
  66.     mail for details. 
  67.  
  68.  
  69. 3.4) Is there a pretty-printer (similar to indent(1)) for Perl?
  70.  
  71.     That depends on what you mean.  If you want something that works like
  72.     vgrind on Perl programs, then the answer is "yes, nearly".  Here's a
  73.     vgrind entry for perl: 
  74.  
  75.     PERL|perl|Perl:\
  76.         :pb=^\d?(sub|package)\d\p\d:\
  77.         :bb={:be=}:cb=#:ce=$:sb=":se=\e":lb=':\
  78.         :le=\e':tl:\
  79.         :id=_:\
  80.         :kw=\
  81.         if for foreach unless until while continue else elsif \
  82.         do eval require \
  83.         die exit \
  84.         defined delete reset \
  85.         goto last redo next dump \
  86.         local undef return  \
  87.         write format  \
  88.         sub package
  89.  
  90.     It doesn't actually do everything right; in particular, 
  91.     things like $#, $', s#/foo##, and $foo'bar all confuse it.
  92.  
  93.     David Levine uses this:
  94.  
  95.     # perl 4.x                    David Levine <levine@ics.uci.edu> 05 apr 1993
  96.     # Derived from Tom Christiansen's perl vgrindef.  I'd like to treat all  of
  97.     # perl's built-ins as  keywords,  but vgrind   fields are  limited  to 1024
  98.     # characters  and the built-ins overflow that (surprise  :-).  So, I didn't
  99.     # include the dbm*, end*, get*, msg*, sem*, set*,  and  shm* functions.   I
  100.     # couldn't come up with an easy way to  distinguish beginnings  of literals
  101.     # ('...') from package prefixes, so literals are not marked.
  102.     # Be sure to:
  103.     # 1) include whitespace between a subprogram name and its opening {
  104.     # 2) include whitespace before a comment (so that $# doesn't get
  105.     # interpreted as one).
  106.     perl4:\
  107.         :pb=^\d?(sub|package)\d\p\d:\
  108.         :id=$%@_:\
  109.         :bb=\e{:be=\e}:cb=\d\e#:ce=$:sb=\e":se=\e":\
  110.         :kw=accept alarm atan2 bind binmode caller chdir chmod chop \
  111.     chown chroot close closedir connect continue cos crypt defined delete \
  112.     die do dump each else elsif eof eval exec exit exp fcntl fileno flock \
  113.     for foreach fork format getc gmtime goto grep hex if include index int \
  114.     ioctl join keys kill last length link listen local localtime log lstat \
  115.     m mkdir next oct open opendir ord pack package pipe pop print printf \
  116.     push q qq qx rand read readdir readlink recv redo rename require reset \
  117.     return reverse rewinddir rindex rmdir s scalar seek seekdir select send \
  118.     shift shutdown sin sleep socket socketpair sort splice split sprintf \
  119.     sqrt srand stat study sub substr symlink syscall sysread system \
  120.     syswrite tell telldir time times tr truncate umask undef unless unlink \
  121.     unpack unshift until utime values vec wait waitpid wantarray warn while \
  122.     write y: 
  123.  
  124.     If what you mean is whether there is a program that will reformat the
  125.     program much as indent(1) will do for C, then the answer is no.  The
  126.     complex feedback between the scanner and the parser (as in the things
  127.     that confuse vgrind) make it challenging at best to write a stand-alone
  128.     Perl parser. 
  129.  
  130.     Of course, if you follow the guidelines in perlstyle(1), you shouldn't
  131.     need to reformat.
  132.  
  133.  
  134. 3.5) How can I convert my perl scripts directly to C or compile them into
  135.      binary form?
  136.  
  137.     The short answer is: "No, you can't compile perl into C.  Period."
  138.  
  139.     However, having said that, it is believed that it would be possible to
  140.     write a perl to C translator, although it is a PhD thesis waiting to
  141.     happen.  Anyone need a good challenging thesis?
  142.  
  143.     In the way of further, detailed explication, it seems that the reasons
  144.     people want to do this usaully break down into one or more of the
  145.     following:
  146.  
  147.     A) speed
  148.     B) secrecy
  149.     C) maintainability
  150.  
  151.     SPEED:
  152.  
  153.     1)    You can't turn perl source code or perl intermediary code into
  154.     native machine code to make it run faster, and saving the perl
  155.     intermediary code doesn't really buy you as much as you'd like.
  156.     If you really must, check out the undump and unexec alternatives.
  157.     If your motivations are speed, then this may or may not help you
  158.     much.  
  159.  
  160.     You might also look into autoloading functions on the fly, which
  161.     can greatly reduce start-up time.
  162.  
  163.     If you have a few routines that are bogging you down, you just
  164.     possibly might wish to hand-translate just them into C, then
  165.     dynamically load these in.  See perlapi(1) for details.  Most
  166.     of the time, however, reorganizing your perl algorithm is the best
  167.     way to address this.
  168.  
  169.  
  170.     SOURCE-CODE SECRECY:
  171.  
  172.     2)  If you're trying to stop people from seeing what you're doing,
  173.     you can shroud it, i.e. turn all the idents into silly stuff, 
  174.     rearrange strings, and remove redundant white space.  There's
  175.     a program out there called ShroudIt! that works on a number of
  176.         languages, including Perl.  Note that it is a commercial product
  177.         though.  Contact David Webber (webber@lnk.com) for more
  178.         information.
  179.         
  180.     3)  You might also look into the cryptswitch() stuff in the perl
  181.         source, which would allow you to ship something in a form they
  182.         can't read.  This isn't particulary well-documented.
  183.  
  184.     4)  If you're worried about them using your software without licence,
  185.     you put some huge disclaimer at the top that says something like
  186.     the following.  This is actually the best solution, because only a
  187.     legal solution will really work if legality is what you're worried
  188.     about: trying to solve legal problems with technical solutions
  189.     is not worth the effort, and too easily circumvented.
  190.  
  191.         This is UNPUBLISHED PROPRIETARY SOURCE CODE of XYZZY, Inc.; the
  192.         contents of this file may not be disclosed to third parties,
  193.         copied or duplicated in any form, in whole or in part, without
  194.         the prior written permission of XYZZY, Inc.
  195.  
  196.         Permission is hereby granted soley to the licencee for use of
  197.         this source code in its unaltered state.  This source code may
  198.         not be modified by licencee except under direction of XYZZY
  199.         Inc.  Neither may this source code be given under any
  200.         circumstances to non-licensees in any form, including source
  201.         or binary.  Modification of this source constitutes breach of
  202.         contract, which voids any potential pending support
  203.         responsibilities by XYZZY Inc.  Divulging the exact or
  204.         paraphrased contents of this source code to unlicensed parties
  205.         either directly or indirectly constitutes violation of federal
  206.         and international copyright and trade secret laws, and will be
  207.         duly prosecuted to the fullest extent permitted under law.
  208.  
  209.         This software is provided by XYZZY Inc. ``as is'' and any
  210.         express or implied warranties, including, but not limited to,
  211.         the implied warranties of merchantability and fitness for a
  212.         particular purpose are disclaimed.  In no event shall the
  213.         regents or contributors be liable for any direct, indirect,
  214.         incidental, special, exemplary, or consequential damages
  215.         (including, but not limited to, procurement of substitute
  216.         goods or services; loss of use, data, or profits; or business
  217.         interruption) however caused and on any theory of liability,
  218.         whether in contract, strict liability, or tort (including
  219.         negligence or otherwise) arising in any way out of the use of
  220.         this software, even if advised of the possibility of such
  221.         damage.
  222.  
  223.     MAINTAINABILITY:
  224.  
  225.     5)  If you just want to stop people from changing it because you're
  226.     concerned about support issues, you can put in a big disclaimer at
  227.     the top that says that if they touch the file they void the
  228.     warranty, and then make them give you a size, checksum, and
  229.     version of the file before answering any questions about it.
  230.  
  231.     If you maintain a central site that distributes software to
  232.     internal client machines, use rdist(1) to send around a proper
  233.     version periodically, perhaps using the -y option on the install
  234.     to flag destinations younger than the source.
  235.  
  236.     Let it be noted than in the many, many years that Perl's author
  237.     has been releasing and supporting freely redistributable software,
  238.     he has NEVER ONCE been bitten by a bogus bug report generated by
  239.     someone breaking his code because they had access to it.  Rather,
  240.     he and many other open software provided (where open software
  241.     means that for which the source is provided, the only truly open
  242.     software) have saved themselves countless hours of labor thousands
  243.     of times over because they've allowed people to inspect the source
  244.     for themselves.  Proprietary source-code hoarding is its own
  245.         headache. 
  246.  
  247.     Thus, obscurity for the sake of maintainability would seem to be a
  248.     red herring.
  249.  
  250.     6)  If you can't count on perl being installed at the destination
  251.         customer, then by all means, merely ship it with your program.
  252.         This is no hardship, since software providers are accustomed to
  253.         shipping software in machine-specific binary form.  The basic idea
  254.         is as simple as: 
  255.  
  256.         shar /usr/local/{lib,bin,man}/perl myprog
  257.  
  258.     Just don't overwrite their own Perl installation if they have one!
  259.  
  260.  
  261. 3.6) Where can I get a perl-mode for emacs?
  262.  
  263.     Since Emacs version 19 patchlevel 22 or so, there has been both a
  264.     perl-mode.el and support for the perl debugger built in.  These should
  265.     come with the standard Emacs 19 distribution.
  266.  
  267.     In the perl source directory, you'll find a directory called
  268.     "emacs", which contains several files that should help you.
  269.  
  270.     Note that the perl-mode of emacs will have fits with "main'foo" (single
  271.     quote), and mess up the indentation and hilighting.  However, note that
  272.     in perl5, you should be using "main::foo".  By the way, did we mention
  273.     that you should upgrade?
  274.  
  275.  
  276. 3.7) Is there a Perl shell? 
  277.     
  278.     Not really.  Perl is a programming language, not a command
  279.     interpreter.  There is a very simple one called "perlsh"
  280.     included in the Perl source distribution.  It just does this:
  281.  
  282.     $/ = '';        # set paragraph mode
  283.     $SHlinesep = "\n";
  284.     while ($SHcmd = <>) {
  285.         $/ = $SHlinesep;
  286.         eval $SHcmd; print $@ || "\n";
  287.         $SHlinesep = $/; $/ = '';
  288.     }
  289.  
  290.     Not very interesting, eh?  
  291.  
  292.     Daniel Smith <dls@best.com> is working on an interactive Perl shell
  293.     called SoftList.  It's currently at version 3.0b7a (beta).  SoftList 
  294.     3.0b7a has tcsh-like command line editing, can let you define a file of
  295.     aliases so that you can run chunks of perl or UNIX commands, and so
  296.     on.  You can pick up a copy at ftp.best.com in
  297.     /pub/dls/SoftList-3.0b7a.gz. 
  298.  
  299.  
  300. 3.8) How can I use curses with perl?
  301.  
  302.     In release 4 of perl, the only way to do this was was to build a
  303.     curseperl binary by linking in your C curses library as described in
  304.     the usub subdirectory of the perl sources.  This requires a modicum of
  305.     work, but it will be reasonably fast since it's all in C (assuming you
  306.     consider curses reasonably fast. :-) Programs written using this
  307.     method require the modified curseperl, not vanilla perl, to run.
  308.     While this is something of a disadvantage, experience indicates that
  309.     it's better to use curseperl than to try to roll your own using
  310.     termcap directly.
  311.  
  312.     Fortunately, in version 5, Curses is a dynamically loaded extension
  313.     by William Setzer*.  You should be able to pick it up wherever you
  314.     get Perl 5 from, or at least these places (expect that the version may
  315.     change by the time you read this):
  316.  
  317.     ftp://ftp.ncsu.edu/pub/math/wsetzer/cursperl5a6.tar.gz
  318.         ftp://ftp.metronet.com/pub/perlinfo/perl5/cursperl5a6.tar.gz
  319.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/cursperl5a6.tar.gz
  320.  
  321.     For a good example of using curses with Perl, you might want to pick
  322.     up a copy of Steven L. Kunz's* "perl menus" package ("menu.pl") via
  323.     anonymous FTP from "ftp.iastate.edu".  It's in the directory /pub/perl
  324.     as:
  325.  
  326.         menu.pl.v3.1.tar.Z
  327.  
  328.     menu.pl is a complete menu front-end for perl+curses and demonstrates
  329.     a lot of things (plus it is useful to boot if you want full-screen
  330.     menu selection ability).  It provides full-screen menu selection
  331.     ability for three menu styles (single-selection, multiple-selection,
  332.     and "radio-button").  The "perl menus" package also includes routines
  333.     for full-screen data entry.  A "template" concept is implemented to
  334.     create a simple (yet flexible) perl interface for building data-entry
  335.     screens for registration, database, or other record-oriented tasks.
  336.  
  337.     menu.pl is supported on Perl4/curseperl and Perl5/Curses.  Complete
  338.     user documentation is provided along with several demos and "beginner
  339.     applications".  A menu utility module is provided that is a collection
  340.     of useful Perl curses routines (such as "pop-up query boxes) that may
  341.     be called from your applications.
  342.  
  343.     Another possibility is to use Henk Penning's cterm package, a curses
  344.     emulation library written in perl.  cterm is actually a separate
  345.     program with which you communicate via a pipe.  It is available from
  346.     ftp.cs.ruu.nl [131.211.80.17] via anonymous ftp. in the directory
  347.     pub/PERL.  You may also acquire the package via email in compressed,
  348.     uuencoded form by sending a message to mail-server@cs.ruu.nl
  349.     containing these lines:
  350.  
  351.     begin
  352.     send PERL/cterm.shar.Z
  353.     end
  354.  
  355.     See the question on retrieving perl via mail for more information on
  356.     how to retrieve other items of interest from the mail server
  357.     there.
  358.  
  359.  
  360. 3.9) How can I use X or Tk with Perl?
  361.  
  362.     Right now, you have several choices.  If you are still using perl4, use
  363.     the WAFE or STDWIN packages, or try to make your own usub binding.
  364.  
  365.     However, if you've upgraded to version 5, you have several exciting
  366.     possibilities, with more popping up each day.  Right now, Tk and Sx
  367.     are the best known such extensions.
  368.  
  369.     If you like the tk package, you should get the Tk extension kit,
  370.     written by Nick Ing-Simmons*.  The official distribution point is at
  371.  
  372.         ftp://ftp.wpi.edu/perl5/private/Tk-b8.tar.gz
  373.  
  374.     but many of the major archive sites now have it in their /ext{entions}
  375.     directory also.  Depending upon your location, you may be better off
  376.     checking there.  Also, understand that the version number may have
  377.     changed by the time you read this.
  378.  
  379.     This package replaced the tkperl5 project, by Malcolm Beattie*, which
  380.     was based on an older version of Tk, 3.6 as compared to the current
  381.     4.X.  This package was also known as nTk (new Tk) while it was in the
  382.     alpha stages, but has been changed to just Tk now that it is in beta.
  383.     Also, be advised that you need at least perl5.001 (preferably 5.002,
  384.     when it becomes available) and the official unofficial patches.
  385.  
  386.     You may also use the old Sx package, (Athena & Xlib), written by
  387.     originally written by by Dominic Giampaolo*, then and rewritten for Sx
  388.     by Frederic Chauveau*.  It's available from these sites:
  389.  
  390.         ftp://ftp.pasteur.fr/pub/Perl/Sx.tar.gz
  391.         ftp://ftp.khoros.unm.edu/pub/perl/extensions/Sx.tar.gz
  392.         ftp://ftp.metronet.com/pub/perlinfo/perl5/Sx.tar.gz
  393.  
  394.     STDWIN is a library written by Guido van Rossum* (author of the Python
  395.     programming language) that is portable between Mac, Dos and X11.  One
  396.     could write a Perl agent to speak to this STDWIN server.
  397.  
  398.     WAFE is a package that implements a symbolic interface to the Athena
  399.     widgets (X11R5). A typical Wafe application consists in our framework
  400.     of two parts: the front-end (we call it Wafe for Widget[Athena]front
  401.     end) and an application program running typically as a separate
  402.     process.  The application program can be implemented in an arbitrary
  403.     programming language and talks to the front-end via stdio.  Since Wafe
  404.     (the front-end) was developed using the extensible TCL shell (cite John
  405.     Ousterhout), an application program can dynamically submit requests to
  406.     the front-end to build up the graphical user interface; the
  407.     application can even down-load application specific procedures into
  408.     the front-end.  The distribution contains sample application programs
  409.     in Perl, GAWK, Prolog, TCL, and C talking to the same Wafe binary.
  410.     Many of the demo applications are implemented in Perl.  Wafe 0.9 can
  411.     be obtained via anonymous ftp from 
  412.     ftp.wu-wien.ac.at[137.208.3.5]:pub/src/X11/wafe-0.9.tar.Z
  413.  
  414.     Alternatively, you could use wish from tcl. 
  415.  
  416.     #!/usr/local/bin/perl
  417.     #####################################################################
  418.     #  An example of calling wish as a subshell under Perl and
  419.     #  interactively communicating with it through sockets.
  420.     #
  421.     #  The script is directly based on Gustaf Neumann's perlwafe script.
  422.     #
  423.     #  Dov Grobgeld dov@menora.weizmann.ac.il
  424.     #  1993-05-17
  425.     #####################################################################
  426.  
  427.     $wishbin = "/usr/local/bin/wish";
  428.  
  429.     die "socketpair unsuccessful: $!!\n" unless socketpair(W0,WISH,1,1,0);
  430.     if ($pid=fork) {
  431.         select(WISH); $| = 1;
  432.         select(STDOUT);
  433.  
  434.     # Create some TCL procedures
  435.         print WISH 'proc echo {s} {puts stdout $s; flush stdout}',"\n";
  436.  
  437.     # Create the widgets
  438.     print WISH <<TCL;
  439.     # This is a comment "inside" wish
  440.  
  441.     frame .f -relief raised -border 1 -bg green
  442.     pack append . .f {top fill expand}
  443.  
  444.     button .f.button-pressme -text "Press me" -command {
  445.         echo "That's nice."
  446.     }
  447.     button .f.button-quit -text quit -command {
  448.         echo "quit"
  449.     }
  450.     pack append .f .f.button-pressme {top fill expand} \\
  451.                .f.button-quit {top expand}
  452.  
  453. TCL
  454.     ;
  455.     # Here is the main loop which receives and sends commands
  456.     # to wish.
  457.     while (<WISH>) {
  458.         chop;
  459.         print "Wish sais: <$_>\n";
  460.         if (/^quit/) { print WISH "destroy .\n"; last; }
  461.     }
  462.         wait;
  463.     } elsif (defined $pid) {
  464.     open(STDOUT, ">&W0");
  465.     open(STDIN, ">&W0");
  466.     close(W0);
  467.     select(STDOUT); $| = 1;
  468.     exec "$wishbin --";
  469.     } else {
  470.     die "fork error: $!\n";
  471.     }
  472.  
  473.  
  474. 3.10) Can I dynamically load C user routines?
  475.  
  476.     Yes -- dynamic loading comes with the distribution.  That means that
  477.     you no longer need 18 different versions of fooperl floating around.
  478.     In fact, all of perl can be stuck into a libperl.so library and
  479.     then your /usr/local/bin/perl binary reduced to just 50k or so.
  480.     See DynLoader(3pm) for details.
  481.  
  482.     In perl4, the answer was kinda.  One package has been released that does
  483.     this, by Roberto Salama*.  He writes:
  484.  
  485.     Here is a version of dylperl, dynamic linker for perl. The code here is
  486.     based on Oliver Sharp's May 1993 article in Dr. Dobbs Journal (Dynamic
  487.     Linking under Berkeley UNIX). 
  488.  
  489.           dyl.h 
  490.           dyl.c - code extracted from Oliver Sharp's article
  491.  
  492.           hash.h
  493.           hash.c - Berkeley's hash functions, should use perl's but
  494.                could not be bothered
  495.  
  496.        dylperl.c - perl usersubs
  497.           user.c - userinit function
  498.  
  499.         sample.c - sample code to be dyl'ed
  500.        sample2.c -          "
  501.          test.pl - sample perl script that dyl's sample*.o
  502.  
  503.     The Makefile assumes that uperl.o is in /usr/local/src/perl/... You
  504.     will probably have to change this to reflect your installation. Other
  505.     than that, just type 'make'...
  506.  
  507.     The idea behind being able to dynamically link code into perl is that
  508.     the linked code should become perl functions, i.e. they can be invoked
  509.     as &foo(...).  For this to happen, the incrementally loaded code must
  510.     use the perl stack, look at sample.c to get a better idea.
  511.  
  512.     The few functions that make up this package are outlined below.
  513.  
  514.     &dyl("file.o"): dynamically link file.o. All functions and non-static
  515.            variables become visible from within perl. This
  516.            function returns a pointer to an internal hash table
  517.            corresponding to the symbol table of the newly loaded
  518.            code.
  519.  
  520.            eg: $ht = &dyl("sample.o")
  521.  
  522.        This function can also be called with the -L and -l ld options.
  523.  
  524.            eg: $ht = &dyl(""sample2.o", "-L/usr/lib", "-lm")
  525.                will also pick up the math library if sample.o
  526.                accesses any symbols there.
  527.  
  528.     &dyl_find("func"): find symbol 'func' and return its symbol table entry
  529.  
  530.     &dyl_functions($ht): print the contents of the internal hash table
  531.     &dyl_print_symbols($f): prints the contents of the symbol returned by
  532.     dyl_find() 
  533.  
  534.     There is very little documentation, maybe something to do for a future
  535.     release.  The files sample.o, and sample2.o contain code to be
  536.     incrementally loaded, test.pl is the test perl script.
  537.  
  538.     Comments are welcome. I submit this code for public consumption and,
  539.     basically, am not responsible for it in any way.
  540.  
  541.  
  542. 3.11) What is undump and where can I get it?
  543.  
  544.     The undump program comes from the TeX distribution.  If you have TeX,
  545.     then you may have a working undump.  If you don't, and you can't get
  546.     one, *AND* you have a GNU emacs working on your machine that can clone
  547.     itself, then you might try taking its unexec() function and compiling
  548.     Perl with -DUNEXEC, which will make Perl call unexec() instead of
  549.     abort().  You'll have to add unexec.o to the objects line in the
  550.     Makefile.  If you succeed, post to comp.lang.perl.misc about your
  551.     experience so others can benefit from it. 
  552.  
  553.     If you have a version of undump that works with Perl, please submit
  554.     its anon-FTP whereabouts to the FAQ maintainer.
  555.  
  556.  
  557. 3.12) How can I get '#!perl' to work under MS-DOS?
  558.  
  559.     John Dallman* has written a program "#!perl.exe" which will do this.
  560.     It is available through anonymous ftp from ftp.ee.umanitoba.ca in the
  561.     directory /pub/msdos/perl/hbp_30.zip.  This program works by finding
  562.     the script and perl.exe, building a command line and running perl.exe
  563.     as a child process.  For more information on this, contact John
  564.     directly.
  565.  
  566. 3.13) Can I write useful perl programs on the command line?
  567.  
  568.     Sure, if they're simple enough.  Of course, for most programs,
  569.     you'll enter them in a file and call perl on them from your
  570.     shell.  That way you can go into the hack/execute/debug cycle.
  571.     But there are plenty of useful one-liner: see below.  (Things
  572.     marked perl5 need to be run from v5.000 or better, but the
  573.     rest don't care.)
  574.  
  575.     # what's octal value of random char (":" in this case)?
  576.     perl -e 'printf "%#o\n", ord(shift)' ":"
  577.  
  578.     # sum first and last fields
  579.     perl -lane 'print $F[0] + $F[1]'
  580.  
  581.     # strip high bits
  582.     perl -pe 'tr/\200-\377/\000-\177/'
  583.  
  584.     # find text files
  585.     perl -le 'for(@ARGV) {print if -f && -T}' *
  586.  
  587.     # trim newsrc
  588.     perl5 -i.old -pe 's/!.*?(\d+)$/! 1-$1/' ~/.newsrc
  589.  
  590.     # cat a dbmfile
  591.     perl -e 'dbmopen(%f,shift,undef);while(($k,$v)=each%f){print "$k:\
  592.             $v\n"}' /etc/aliases 
  593.  
  594.     # remove comments from C program
  595.     perl5 -0777 -pe 's{/\*.*?\*/}{}gs' foo.c
  596.  
  597.     # make file a month younger than today, defeating reaper daemons
  598.     perl -e '$X=24*60*60; utime(time(),time() + 30 * $X,@ARGV)' *
  599.  
  600.     # find first unused uid
  601.     perl5 -le '$i++ while getpwuid($i); print $i'
  602.  
  603.     # find first unused uid after 100, even with perl4
  604.     perl -le '$i = 100; $i++ while ($x) = getpwuid($i); print $i'
  605.  
  606.     # detect pathetically insecurable systems
  607.     perl5 -le 'use POSIX; print "INSECURE" unless sysconf(_PC_CHOWN_RESTRICTED)'
  608.  
  609.     # display reasonable manpath
  610.     echo $PATH | perl5 -nl -072 -e '
  611.     s![^/+]*$!man!&&-d&&!$s{$_}++&&push@m,$_;END{print"@m"}'
  612.  
  613.     Ok, the last one was actually an obfuscate perl entry. :-) 
  614.  
  615. 3.14) What's a "closure"?
  616.  
  617.     (Larry wrote) This is a notion out of the Lisp world that says if you
  618.     define an anonymous function in a particular lexical context, it
  619.     pretends to run in that context even when it's called outside of the
  620.     context.
  621.  
  622.     In human terms, it's a funny way of passing arguments to a subroutine
  623.     when you define it as well as when you call it.  It's useful for
  624.     setting up little bits of code to run later, such as callbacks.  You
  625.     can even do object-oriented stuff with it, though Perl provides a
  626.     different mechanism to do that already.
  627.  
  628.     You can also think of it as a way to write a subroutine template without
  629.     using eval.
  630.  
  631.     Here's a small example of how this works:
  632.  
  633.     sub newprint {
  634.         my $x = shift;
  635.         return sub { my $y = shift; print "$x, $y!\n"; };
  636.     }
  637.     $h = newprint("Howdy");
  638.     $g = newprint("Greetings");
  639.  
  640.     # Time passes...
  641.  
  642.     &$h("world");
  643.     &$g("earthlings");
  644.  
  645.     This prints:
  646.  
  647.     Howdy, world!
  648.     Greetings, earthlings!
  649.  
  650.     Note particularly that $x continues to refer to the value passed into
  651.     newprint() *despite* the fact that the "my $x" has seemingly gone out
  652.     of scope by the time the anonymous subroutine runs.  That's what
  653.     closure is all about.
  654.  
  655.     This only applies to lexical variables, by the way.  Dynamic variables
  656.     continue to work as they have always worked.  Closure is not something
  657.     that most Perl programmers need trouble themselves about to begin with.
  658.  
  659. --
  660. Stephen P Potter        Pencom Systems Administration              Beaching It 
  661. spp@psa.pencom.com    Pager: 1-800-759-8888, 547-9561     Work: 703-860-2222
  662.   Cthulhu for President in '96: When You're Tired of the Lesser of Two Evils
  663. --
  664. Stephen P Potter        Pencom Systems Administration              Beaching It 
  665. spp@psa.pencom.com    Pager: 1-800-759-8888, 547-9561     Work: 703-860-2222
  666. "I don't care whether people actually like Perl, just so long as they *think*
  667.     they like it...  ;-)"    -Larry Wall
  668.