home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / faqs / faq / perl-faq / part3 < prev    next >
Encoding:
Text File  |  1995-07-25  |  17.1 KB  |  431 lines

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