home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Information / Languages / Perl / perl-faq⁄part3 < prev    next >
Encoding:
Internet Message Format  |  1994-12-03  |  17.6 KB  |  [TEXT/R*ch]

  1. Path: bloom-beacon.mit.edu!senator-bedfellow.mit.edu!faqserv
  2. From: spp@vx.com
  3. Newsgroups: comp.lang.perl,comp.answers,news.answers
  4. Subject: comp.lang.perl FAQ 3/5 - Programming Aids
  5. Supersedes: <perl-faq/part3_784894001@rtfm.mit.edu>
  6. Followup-To: poster
  7. Date: 30 Nov 1994 09:40:38 GMT
  8. Organization: none
  9. Lines: 424
  10. Approved: news-answers-request@MIT.EDU
  11. Distribution: world
  12. Message-ID: <perl-faq/part3_786188328@rtfm.mit.edu>
  13. References: <perl-faq/part0_786188328@rtfm.mit.edu>
  14. NNTP-Posting-Host: bloom-picayune.mit.edu
  15. X-Last-Updated: 1994/11/14
  16. Originator: faqserv@bloom-picayune.MIT.EDU
  17. Xref: bloom-beacon.mit.edu comp.lang.perl:27003 comp.answers:8610 news.answers:30224
  18.  
  19. Archive-name: perl-faq/part3
  20. Version: $Id: part3,v 2.1 1994/10/25 13:56:19 spp Exp spp $
  21. Posting-Frequency: bi-weekly
  22.  
  23. This posting contains answers to general information questions, mostly
  24. about programming aids.  
  25.  
  26.  
  27. 3.1) How can I use Perl interactively?
  28.     
  29.     The easiest way to do this is to run Perl under its debugger.  If you
  30.     have no program to debug, you can invoke the debugger on an `empty'
  31.     program like this: 
  32.  
  33.         perl -de 0
  34.  
  35.     (The more positive hackers prefer "perl -de 1". :-)
  36.  
  37.     Now you can type in any legal Perl code, and it will be immediately
  38.     evaluated.  You can also examine the symbol table, get stack
  39.     backtraces, check variable values, and if you want to, set breakpoints
  40.     and do the other things you can do in a symbolic debugger. 
  41.  
  42.  
  43. 3.2) Is there a Perl profiler?
  44.  
  45.     While there isn't one included with the perl source distribution (yet) 
  46.     various folks have written packages that allow you to do at least some 
  47.     sort of profiling.  The strategy usually includes modifying the perl 
  48.     debugger to handle profiling.  Authors of these packages include 
  49.  
  50.     Wayne Thompson         <me@anywhere.EBay.Sun.COM>
  51.     Ray Lischner         <lisch@sysserver1.mentor.com>
  52.     Kresten Krab Thorup     <krab@iesd.auc.dk>  
  53.  
  54.     The original articles by these folks containing their profilers are
  55.     available on convex.com in /pub/perl/information/profiling.shar via
  56.     anon ftp. 
  57.  
  58.  
  59. 3.3) Is there a yacc for Perl?
  60.  
  61.     Yes!! It's a version of Berkeley yacc that outputs Perl code instead
  62.     of C code!  You can get this from ftp.sterling.com [192.124.9.1] in
  63.     /local/perl-byacc1.8.1.tar.Z, or send the author mail for details.
  64.  
  65.  
  66. 3.4) Is there a pretty-printer for Perl?
  67.  
  68.     That depends on what you mean.  If you want something that works like
  69.     vgrind on Perl programs, then the answer is "yes, nearly".  Here's a
  70.     vgrind entry for perl: 
  71.  
  72.     PERL|perl|Perl:\
  73.         :pb=^\d?(sub|package)\d\p\d:\
  74.         :bb={:be=}:cb=#:ce=$:sb=":se=\e":lb=':\
  75.         :le=\e':tl:\
  76.         :id=_:\
  77.         :kw=\
  78.         if for foreach unless until while continue else elsif \
  79.         do eval require \
  80.         die exit \
  81.         defined delete reset \
  82.         goto last redo next dump \
  83.         local undef return  \
  84.         write format  \
  85.         sub package
  86.  
  87.     It doesn't actually do everything right; in particular, 
  88.     things like $#, $', s#/foo##, and $foo'bar all confuse it.
  89.  
  90.     David Levine uses this:
  91.  
  92.     # perl 4.x                    David Levine <levine@ics.uci.edu> 05 apr 1993
  93.     # Derived from Tom Christiansen's perl vgrindef.  I'd like to treat all  of
  94.     # perl's built-ins as  keywords,  but vgrind   fields are  limited  to 1024
  95.     # characters  and the built-ins overflow that (surprise  :-).  So, I didn't
  96.     # include the dbm*, end*, get*, msg*, sem*, set*,  and  shm* functions.   I
  97.     # couldn't come up with an easy way to  distinguish beginnings  of literals
  98.     # ('...') from package prefixes, so literals are not marked.
  99.     # Be sure to:
  100.     # 1) include whitespace between a subprogram name and its opening {
  101.     # 2) include whitespace before a comment (so that $# doesn't get
  102.     # interpreted as one).
  103.     perl4:\
  104.         :pb=^\d?(sub|package)\d\p\d:\
  105.         :id=$%@_:\
  106.         :bb=\e{:be=\e}:cb=\d\e#:ce=$:sb=\e":se=\e":\
  107.         :kw=accept alarm atan2 bind binmode caller chdir chmod chop \
  108.     chown chroot close closedir connect continue cos crypt defined delete \
  109.     die do dump each else elsif eof eval exec exit exp fcntl fileno flock \
  110.     for foreach fork format getc gmtime goto grep hex if include index int \
  111.     ioctl join keys kill last length link listen local localtime log lstat \
  112.     m mkdir next oct open opendir ord pack package pipe pop print printf \
  113.     push q qq qx rand read readdir readlink recv redo rename require reset \
  114.     return reverse rewinddir rindex rmdir s scalar seek seekdir select send \
  115.     shift shutdown sin sleep socket socketpair sort splice split sprintf \
  116.     sqrt srand stat study sub substr symlink syscall sysread system \
  117.     syswrite tell telldir time times tr truncate umask undef unless unlink \
  118.     unpack unshift until utime values vec wait waitpid wantarray warn while \
  119.     write y: 
  120.  
  121.     If what you mean is whether there is a program that will reformat the
  122.     program much as indent(1) will do for C, then the answer is no.  The
  123.     complex feedback between the scanner and the parser (as in the things
  124.     that confuse vgrind) make it challenging at best to write a stand-alone
  125.     Perl parser. 
  126.  
  127.  
  128. 3.5) There's an a2p and an s2p; why isn't there a p2c (perl-to-C)?
  129.  
  130.     Because the Pascal people would be upset that we stole their name. :-)
  131.  
  132.     The dynamic nature of Perl's do and eval operators (and remember that
  133.     constructs like s/$mac_donald/$mac_gregor/eieio count as an eval) would
  134.     make this very difficult.  To fully support them, you would have to put
  135.     the whole Perl interpreter into each compiled version for those scripts
  136.     using them.  This is what undump does right now, if your machine has it.
  137.     If what you're doing will be faster in C than in Perl, maybe it should
  138.     have been written in C in the first place.  For things that ought to be
  139.     written in Perl, the interpreter will be just about as fast, because the
  140.     pattern matching routines won't work any faster linked into a C program.
  141.     Even in the case of simple Perl programs that don't do any fancy evals, the
  142.     major gain would be in compiling the control flow tests, with the rest
  143.     still being a maze of twisty, turny subroutine calls.  Since these are not
  144.     usually the major bottleneck in the program, there's not as much to be
  145.     gained via compilation as one might think.
  146.  
  147.     However, we're still looking for someone to generate byte-compiled
  148.     code for Perl, or eventually even C code out of it.  These are
  149.     probably masters and PhD thesis topics respectively, and no one
  150.     has begun work on it yet.
  151.  
  152.  
  153.  
  154. 3.6) Where can I get a perl-mode for emacs?
  155.  
  156.     Since Emacs version 19 patchlevel 22 or so, there has been both a
  157.     perl-mode.el and support for the perl debugger built in.  These should
  158.     come with the standard Emacs 19 distribution.
  159.  
  160.     In the perl source directory, you'll find a directory called
  161.     "emacs", which contains several files that should help you.
  162.  
  163.  
  164. 3.7) Is there a Perl shell? 
  165.     
  166.     Not really.  Perl is a programming language, not a command
  167.     interpreter.  There is a very simple one called "perlsh"
  168.     included in the Perl source distribution.  It just does this:
  169.  
  170.     $/ = '';        # set paragraph mode
  171.     $SHlinesep = "\n";
  172.     while ($SHcmd = <>) {
  173.         $/ = $SHlinesep;
  174.         eval $SHcmd; print $@ || "\n";
  175.         $SHlinesep = $/; $/ = '';
  176.     }
  177.  
  178.     Not very interesting, eh?  
  179.  
  180.     Daniel Smith <dansmith@autodesk.com> is working on an interactive Perl
  181.     shell called SoftList.  It's currently at version 3.0beta.  SoftList
  182.     3.0 has tcsh-like command line editing, can let you define a file of
  183.     aliases so that you can run chunks of perl or UNIX commands, and so
  184.     on.  You can send mail to him for further information and availability.
  185.  
  186. 3.8) How can I use curses with perl?
  187.  
  188.     In release 4 of perl, the only way to do this was was to build a
  189.     curseperl binary by linking in your C curses library as described in
  190.     the usub subdirectory of the perl sources.  This requires a modicum of
  191.     work, but it will be reasonably fast since it's all in C (assuming you
  192.     consider curses reasonably fast. :-) Programs written using this
  193.     method require the modified curseperl, not vanilla perl, to run.
  194.     While this is something of a disadvantage, experience indicates that
  195.     it's better to use curseperl than to try to roll your own using
  196.     termcap directly.
  197.  
  198.     Fortunately, in version 5, Curses is a dynamically loaded extension
  199.     by William Setzer*.  You should be able to pick it up wherever you
  200.     get Perl 5 from, or at least these places:
  201.  
  202.     ftp://ftp.ncsu.edu/pub/math/wsetzer/cursperl5a6.tar.gz
  203.         ftp://ftp.metronet.com/pub/perlinfo/perl5/cursperl5a6.tar.gz
  204.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/cursperl5a6.tar.gz
  205.  
  206.     For a good example of using (v4) curseperl, you might want to pick up a
  207.     copy of  Steven L Kunz's* "perl menus" package ("menu.pl") via
  208.     anonymous FTP from "ftp.iastate.edu".  It's in the /pub/perl as
  209.  
  210.     menu.pl.v2.3.shr1
  211.     menu.pl.v2.3.shr2
  212.     menu.pl.v2.3.tar.Z
  213.  
  214.     menus.pl is a complete menu front-end for curseperl and demonstates
  215.     a lot of things (plus it is useful to boot if you want full-screen
  216.     menu selection ability).
  217.  
  218.     Another possibility is to use Henk Penning's cterm package, a curses
  219.     emulation library written in perl.  cterm is actually a separate
  220.     program with which you communicate via a pipe.  It is available from
  221.     ftp.cs.ruu.nl [131.211.80.17] via anonymous ftp. in the directory
  222.     pub/PERL.  You may also acquire the package via email in compressed,
  223.     uuencoded form by sending a message to mail-server@cs.ruu.nl
  224.     containing these lines:
  225.  
  226.     begin
  227.     send PERL/cterm.shar.Z
  228.     end
  229.  
  230.     See the question on retrieving perl via mail for more information on
  231.     how to retrieve other items of interest from the mail server
  232.     there.
  233.  
  234.  
  235. 3.9) How can I use X with Perl?
  236.  
  237.     Right now, you have several choices.  If you are still using perl4, use
  238.     the WAFE or STDWIN packages, or try to make your own usub binding.
  239.  
  240.     However, if you've upgraded to version 5, you have several exciting
  241.     possibilities, with more popping up each day.  Right now, Tk and Sx
  242.     are the best known such extensions.
  243.  
  244.     If you like the tk package, you should get the Tk extension kit,
  245.     written by Malcolm Beattie*.  Here are some places to get it:
  246.  
  247.      Tk (as in tcl/tk, but sans tcl)
  248.  
  249.     ftp://ftp.cis.ufl.edu/pub/perl/src/tkperl/tkperl5a4.tar.gz
  250.         ftp://ftp.khoros.unm.edu/pub/perl/extensions/tkperl5a4.tar.gz
  251.         ftp://ftp.metronet.com/pub/perlinfo/perl5/tkperl/tkperl5a4.tar.gz
  252.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/tkperl5a4.tar.gz
  253.         ftp://black.ox.ac.uk/src/ALPHA/tkperl5a4.tar.gz
  254.  
  255.         (try 5a5 everywhere after 2pm UST Thu 20 Oct 1994, as in)
  256.  
  257.         ftp://sable.ox.ac.uk/pub/perl/tkperl5a5.tar.gz
  258.  
  259.     You may also use the old Sx package, (Athena & Xlib), written by
  260.     originally written by by Dominic Giampaolo*, then and rewritten for Sx
  261.     by Frederic Chauveau*.fr>.  It's available from these sites:
  262.  
  263.         ftp://ftp.pasteur.fr/pub/Perl/Sx.tar.gz
  264.         ftp://ftp.khoros.unm.edu/pub/perl/extensions/Sx.tar.gz
  265.         ftp://ftp.metronet.com/pub/perlinfo/perl5/Sx.tar.gz
  266.         ftp://ftp.cs.ruu.nl/pub/PERL/perl5.0/PerlDoc.ps.gz
  267.  
  268.     STDWIN is a library written by Guido van Rossum* (author of the Python
  269.     programming language) that is portable between Mac, Dos and X11.  One
  270.     could write a Perl agent to speak to this STDWIN server.
  271.  
  272.     WAFE is a package that implements a symbolic interface to the Athena
  273.     widgets (X11R5). A typical Wafe application consists in our framework
  274.     of two parts: the front-end (we call it Wafe for Widget[Athena]front
  275.     end) and an application program running typically as a separate
  276.     process.  The application program can be implemented in an arbitrary
  277.     programming language and talks to the front-end via stdio.  Since Wafe
  278.     (the front-end) was developed using the extensible TCL shell (cite John
  279.     Ousterhout), an application program can dynamically submit requests to
  280.     the front-end to build up the graphical user interface; the
  281.     application can even down-load application specific procedures into
  282.     the front-end.  The distribution contains sample application programs
  283.     in Perl, GAWK, Prolog, TCL, and C talking to the same Wafe binary.
  284.     Many of the demo applications are implemented in Perl.  Wafe 0.9 can
  285.     be obtained via anonymous ftp from 
  286.     ftp.wu-wien.ac.at[137.208.3.5]:pub/src/X11/wafe-0.9.tar.Z
  287.  
  288.     Alternatively, you could use wish from tcl. 
  289.  
  290.     #!/usr/local/bin/perl
  291.     #####################################################################
  292.     #  An example of calling wish as a subshell under Perl and
  293.     #  interactively communicating with it through sockets.
  294.     #
  295.     #  The script is directly based on Gustaf Neumann's perlwafe script.
  296.     #
  297.     #  Dov Grobgeld dov@menora.weizmann.ac.il
  298.     #  1993-05-17
  299.     #####################################################################
  300.  
  301.     $wishbin = "/usr/local/bin/wish";
  302.  
  303.     die "socketpair unsuccessful: $!!\n" unless socketpair(W0,WISH,1,1,0);
  304.     if ($pid=fork) {
  305.         select(WISH); $| = 1;
  306.         select(STDOUT);
  307.  
  308.     # Create some TCL procedures
  309.         print WISH 'proc echo {s} {puts stdout $s; flush stdout}',"\n";
  310.  
  311.     # Create the widgets
  312.     print WISH <<TCL;
  313.     # This is a comment "inside" wish
  314.  
  315.     frame .f -relief raised -border 1 -bg green
  316.     pack append . .f {top fill expand}
  317.  
  318.     button .f.button-pressme -text "Press me" -command {
  319.         echo "That's nice."
  320.     }
  321.     button .f.button-quit -text quit -command {
  322.         echo "quit"
  323.     }
  324.     pack append .f .f.button-pressme {top fill expand} \\
  325.                .f.button-quit {top expand}
  326.  
  327. TCL
  328.     ;
  329.     # Here is the main loop which receives and sends commands
  330.     # to wish.
  331.     while (<WISH>) {
  332.         chop;
  333.         print "Wish sais: <$_>\n";
  334.         if (/^quit/) { print WISH "destroy .\n"; last; }
  335.     }
  336.         wait;
  337.     } elsif (defined $pid) {
  338.     open(STDOUT, ">&W0");
  339.     open(STDIN, ">&W0");
  340.     close(W0);
  341.     select(STDOUT); $| = 1;
  342.     exec "$wishbin --";
  343.     } else {
  344.     die "fork error: $!\n";
  345.     }
  346.  
  347.  
  348. 3.10) Can I dynamically load C user routines?
  349.  
  350.     Yes -- dynamic loading comes with the distribution.  That means that
  351.     you no longer need 18 different versions of fooperl floating around.
  352.     In fact, all of perl can be stuck into a libperl.so library and
  353.     then your /usr/local/bin/perl binary reduced to just 50k or so.
  354.     See DynLoader(3pm) for details.
  355.  
  356.     In perl4, the answer is kinda.  One package has been released that does
  357.     this, by Roberto Salama*.  He writes:
  358.  
  359.     Here is a version of dylperl, dynamic linker for perl. The code here is
  360.     based on Oliver Sharp's May 1993 article in Dr. Dobbs Journal (Dynamic
  361.     Linking under Berkeley UNIX). 
  362.  
  363.           dyl.h 
  364.           dyl.c - code extracted from Oliver Sharp's article
  365.  
  366.           hash.h
  367.           hash.c - Berkeley's hash functions, should use perl's but
  368.                could not be bothered
  369.  
  370.        dylperl.c - perl usersubs
  371.           user.c - userinit function
  372.  
  373.         sample.c - sample code to be dyl'ed
  374.        sample2.c -          "
  375.          test.pl - sample perl script that dyl's sample*.o
  376.  
  377.     The Makefile assumes that uperl.o is in /usr/local/src/perl/... You
  378.     will probable have to change this to reflect your installation. Other
  379.     than that, just type 'make'...
  380.  
  381.     The idea behind being able to dynamically link code into perl is that
  382.     the linked code should become perl functions, i.e. they can be invoked
  383.     as &foo(...).  For this to happen, the incrementally loaded code must
  384.     use the perl stack, look at sample.c to get a better idea.
  385.  
  386.     The few functions that make up this package are outlined below.
  387.  
  388.     &dyl("file.o"): dynamically link file.o. All functions and non-static
  389.            variables become visible from within perl. This
  390.            function returns a pointer to an internal hash table
  391.            corresponding to the symbol table of the newly loaded
  392.            code.
  393.  
  394.            eg: $ht = &dyl("sample.o")
  395.  
  396.        This function can also be called with the -L and -l ld options.
  397.  
  398.            eg: $ht = &dyl(""sample2.o", "-L/usr/lib", "-lm")
  399.                will also pick up the math library if sample.o
  400.                accesses any symbols there.
  401.  
  402.     &dyl_find("func"): find symbol 'func' and return its symbol table entry
  403.  
  404.     &dyl_functions($ht): print the contents of the internal hash table
  405.     &dyl_print_symbols($f): prints the contents of the symbol returned by
  406.     dyl_find() 
  407.  
  408.     There is very little documentation, maybe something to do for a future
  409.     release.  The files sample.o, and sample2.o contain code to be
  410.     incrementally loaded, test.pl is the test perl script.
  411.  
  412.     Comments are welcome. I submit this code for public consumption and,
  413.     basically, am not responsible for it in any way.
  414.  
  415.  
  416. 3.11) What is undump and where can I get it?
  417.  
  418.     The undump program comes from the TeX distribution.  If you have TeX,
  419.     then you may have a working undump.  If you don't, and you can't get
  420.     one, *AND* you have a GNU emacs working on your machine that can clone
  421.     itself, then you might try taking its unexec() function and compiling
  422.     Perl with -DUNEXEC, which will make Perl call unexec() instead of
  423.     abort().  You'll have to add unexec.o to the objects line in the
  424.     Makefile.  If you succeed, post to comp.lang.perl about your experience
  425.     so others can benefit from it. 
  426.  
  427.     If you have a version of undump that works with Perl, please submit
  428.     its anon-FTP whereabouts to the FAQ maintainer.
  429.  
  430.  
  431. 3.12) How can I get '#!perl' to work under MS-DOS?
  432.  
  433.     John Dallman* has written a program "#!perl.exe" which will do this.
  434.     It is available through anonymous ftp from ftp.ee.umanitoba.ca in the
  435.     directory /pub/msdos/perl/hbp_20.zip.  This program works by finding
  436.     the script and perl.exe, building a command line and running perl.exe
  437.     as a child process.  For more information on this, contact John
  438.     directly.
  439. Stephen P Potter        spp@vx.com        Varimetrix Corporation
  440. 2350 Commerce Park Drive, Suite 4                Palm Bay, FL 32905
  441. (407) 676-3222                           CAD/CAM/CAE/Software
  442.  
  443.