home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / perl-faq / ptk-faq / part4 < prev    next >
Encoding:
Text File  |  1997-06-05  |  34.6 KB  |  981 lines

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!uwm.edu!feeder.chicago.cic.net!chi-news.cic.net!news.maxwell.syr.edu!news.cis.ohio-state.edu!nntp.sei.cmu.edu!bb3.andrew.cmu.edu!goldenapple.srv.cs.cmu.edu!rochester!cornellcs!newsstand.cit.cornell.edu!lnsnews.lns.cornell.edu!lns62.lns.cornell.edu!PVHP
  2. From: pvhp@lns62.lns.cornell.edu (Peter Prymmer)
  3. Newsgroups: comp.lang.perl.tk,comp.lang.perl.announce,comp.answers,news.answers
  4. Subject: comp.lang.perl.tk FAQ part4 of 5
  5. Followup-To: comp.lang.perl.tk
  6. Date: Sun, 01 Jun 1997 06:42:26 GMT
  7. Organization: Wilson Lab, Cornell U., Ithaca, NY, 14853
  8. Lines: 965
  9. Approved: pvhp@lns62.lns.cornell.edu (Peter Prymmer)
  10. Expires: Thu, 31 Jul 1997 06:42:06 GMT
  11. Message-ID: <009B51BD.E77958F0@lns62.lns.cornell.edu>
  12. Reply-To: PVHP@lns62.lns.cornell.edu
  13. NNTP-Posting-Host: lns62.lns.cornell.edu
  14. Xref: senator-bedfellow.mit.edu comp.lang.perl.tk:4684 comp.lang.perl.announce:648 comp.answers:26364 news.answers:104174
  15.  
  16. Summary: comp.lang.perl.tk Frequently Asked Questions.
  17. Archive-name: perl-faq/ptk-faq/part4
  18. Posting-Frequency: monthly
  19. Last-modified: Date: Sat May 31 16:48:37 1997
  20. URL: http://w4.lns.cornell.edu/~pvhp/ptk/ptkFAQ.html
  21. Version: 1.00_07
  22.  
  23. URL (Hypertext-split): http://w4.lns.cornell.edu/~pvhp/ptk/ptkTOC.html
  24. URL (Plaintext): http://w4.lns.cornell.edu/~pvhp/ptk/ptkFAQ.txt
  25. Image-supplement: http://w4.lns.cornell.edu/~pvhp/ptk/ptkIMG.html
  26. ftp-Archive: ftp://ftp.ccd.bnl.gov/pub/ptk/ptkFAQ.txt
  27. ftp-Archive: ftp://rtfm.mit.edu/pub/usenet/perl-faq/ptk-faq/
  28. e-mail-Archive: ptkfaq@pubweb.bnl.gov
  29.  
  30. Perl/Tk FAQ part 4 of 5 - More Perl/Tk     
  31. ************************************** 
  32.  
  33.  
  34.  
  35.  ______________________________________________________________________
  36.  
  37.  
  38.  
  39.  13. What are some of the primary differences between Tcl/Tk and Perl/Tk? 
  40.  
  41.  Considering that both interpreters/(compiler) for Tcl and Perl were written in
  42.  C for original use on Unix computers it is not surprising that there are some
  43.  similarities between the two languages. 
  44.  
  45.  Nevertheless, there are a large number of differences between the Tcl
  46.  language and the Perl language. One thing to keep in mind is that to build,
  47.  install, and use Perl/Tk one does not need to have Tcl/Tk on hand at all.
  48.  Perl/Tk is completely independent of Tcl/Tk. 
  49.  
  50.  Tom Christiansen (a definite perl proponent) has put up a web page that
  51.  elucidates some critical technical differences between Tcl and Perl at: 
  52.  
  53.      http://www.perl.com/perl/versus/tcl-discussion.html
  54.  
  55.  Within each language there is Tk - a widget Toolkit. One must be careful that
  56.  some of the Tcl/Tk widget names and options have been modified slightly in
  57.  the perl/Tk language. 
  58.  
  59.  With Tk-b9.01 (and higher) a great many functions (method calls actually)
  60.  start with an upper case letter and continue with all lower case letters (e.g.
  61.  there is a perl/Tk Entry widget but no entry widget), and many
  62.  configuration options are all lower case (e.g. there is a perl/Tk 
  63.  highlightthickness option but no highlightThickness option). Thus
  64.  if you are having trouble converting a script check your typing. (there is a
  65.  script b9names to help). There is also a tcl2perl script (discussed later). 
  66.  
  67.  The html docs that get created during the build of perl/Tk ought to help clarify
  68.  most any language difference. While the following table does not cover all the
  69.  differences it is hoped that it will prove useful, especially to those people
  70.  coming from a primarily Tcl/Tk programming background. These are some of
  71.  the common Tcl->Perl stumbling points: 
  72.  
  73.  
  74.  what              Tcl/Tk                 Perl/Tk
  75.  variable          set a 123              $a = 123; or $a = '123';
  76.   initialization
  77.  re-assignment     set b $a               $b = $a;
  78.  
  79.  lists/arrays      set a {1 2 fred 7.8}   @a = (1,2,'fred',7.8);
  80.  re-assignment     list set b $a          @b = @a;
  81.  
  82.  associative       set a(Jan) 456.02      %a = ('Jan',456.02,'Feb',534.96);
  83.   arrays           set a(Feb) 534.96
  84.  re-assignment     foreach i \            %b = %a;
  85.                     [array names a] {
  86.                     set b($i) = $a($i) }
  87.  
  88.  Note on the above examples:
  89.  In Tcl the scalar, list, and array variable 'a' will overwrite each 
  90.  previous assignment.
  91.  In Perl $a, @a, %a are all distinct (occupy separate namespaces).
  92.  
  93.  expressions       set a [expr $b+$c]     $a = $b+$c;
  94.  
  95.  increment         incr i                 $i++; or ++$i;
  96.  
  97.  declare           proc plus {a b} {      sub plus { my($a,$b) = @_;
  98.   subroutines       expr $a + $b }         $a+$b; }
  99.  
  100.  variable scope    local default          global default
  101.                    override w/ "global"   override w/ "my" (or "local")
  102.  
  103.  call              plus 1 2               &plus(1,2); #or
  104.   subroutines                             plus(1,2);  #OK after sub plus
  105.  
  106.  statement sep     newline or at ";"      ";" required
  107.  
  108.  statement         "\" - newline          none required
  109.   continuation
  110.  
  111.  verbatim strings  {}                     ''
  112.   e.g.             {a \ lot@ of $stuff}   'a \ lot@ of $stuff'
  113.  
  114.  escaped strings   ""                     ""
  115.   e.g.             "Who\nWhat\nIdunno"    "Who\nWhat\nIdunno"
  116.  
  117.  STDOUT            puts "Hello World!"    print "Hello World!\n"
  118.                    puts stdout "Hello!"   print STDOUT "Hello!\n"
  119.  
  120.  Note also that Tcl/Tk has a built-in abbreviation completion mechanism that
  121.  lets you specify short hand, e.g. 
  122.  
  123.     canvas .frame.canvas -yscrollcommand ".frame.scroll set" ; #Tcl/Tk OK
  124.     canvas .frame.canvas -yscroll ".frame.scroll set" ;        #Tcl/Tk also OK
  125.     $canvas=$main->Canvas(-yscroll => ['set',$scroll]);  #ERROR perl/Tk
  126.     $canvas=$main->Canvas(-yscrollcommand => ['set',$scroll]); #perl/Tk OK
  127.  
  128.  You may get around this with the perl abbrev.pl package in certain
  129.  circumstances. For example: 
  130.  
  131.     require 'abbrev.pl';
  132.     %foo = ();
  133.     &abbrev(*foo,'-yscrollcommand');
  134.   ...
  135.     $canvas=$main->Canvas($foo{'-yscroll'} => ['set',$scroll]); #perl/Tk OK
  136.  
  137.  In Perl you can emulate the Tcl unknown proc (through the perl AUTOLOAD
  138.  mechanism) as follows: 
  139.  
  140.      use Shell;
  141.      print($p = man(-k => bitmap));
  142.  
  143.  Which is equivalent to what you would get if you typed: 
  144.  
  145.      man -k bitmap
  146.  
  147.  >From within tclsh or wish. (Thanks to Ilya Zakharevich 
  148.  <ilya@math.ohio-state.edu> for pointing out this feature. ;-) 
  149.  
  150.  ______________________________________________________________________
  151.  
  152.  
  153.  
  154.  14. How do I install new scripts | modules | extensions? 
  155.  
  156.  (Thanks to Ilya Zakharevich <ilya@math.ohio-state.edu> for pointing out
  157.  that perl code comes in a variety of flavors and some code requires more work
  158.  than others to install. Hence I have expanded this topic and will refer to three
  159.  distinct categories here: Scripts Modules and Extensions:) 
  160.  
  161.  Scripts
  162.  -------
  163.  
  164.  A "self-contained" script needs little modification (in principle!) to run. It is a
  165.  good idea to check the #! line at the very top of the file to reflect your local
  166.  perl setup (e.g. #!/usr/bin/perl -w (change to) 
  167.  #!/usr/gnu/local/perl -w or what have you). There are allegedly "more
  168.  portable" ways to invoke the perl interpretor as well - they are more fully
  169.  documented in the perl FAQ and the perlrun(1) man page, however. 
  170.  
  171.  Other things you do not want to forget when trying to run a perl script include
  172.  giving yourself permission to do so, e.g.: 
  173.  
  174.      chmod u+x newscriptname
  175.  
  176.  You also want to be sure your DISPLAY environment variable is set up
  177.  properly when attempting to run a perl/Tk script. You may also need to look at
  178.  the xhost(1) or the xauth(1) man pages for setting up your X-display
  179.  properly. 
  180.  
  181.  If you are still experiencing difficulty check to be sure that extraneous
  182.  /newsgroup|e-mail|HTML headers|footers|markup//; are not in the file and
  183.  that you have on hand all that is requireed or useed by the script (if not you
  184.  may need to install a module - or even a perl4 style lib.pl file). 
  185.  
  186.  Modules
  187.  -------
  188.  
  189.  Check out the module - make sure it is OK and will run on your system - does
  190.  it require a specific location? For testing purposes (always a good idea) or if
  191.  you do not have root priveleges set the file in a directory that you do have
  192.  write access to and try to include it in a test script. Assuming you have a
  193.  module to test called "Foo.pm" and are simply running the test script in the
  194.  same directory as the module begin by adding to the @INC array like so: 
  195.  
  196.      #!/usr/bin/perl -w
  197.       BEGIN { @INC = ("$ENV{'PWD'}",@INC); }
  198.       use Tk;
  199.       use Foo;
  200.  
  201.  or 
  202.  
  203.      #!/usr/bin/perl -w
  204.       use lib $ENV{PWD};
  205.       use Tk;
  206.       use Foo;
  207.  
  208.  Another approach is to set either the PERLLIB or PERL5LIB environment
  209.  variable from your shell. This method allows invoking the test script from
  210.  within a number of different directories without having to edit a hard coded 
  211.  use lib or push(@INC,".") kind of statement within the script. Yet
  212.  another way to do it is with the -I switch on the command line like so: 
  213.  
  214.      perl -Ipath/to/Foo -e fooscriptname
  215.  
  216.  After a successful test; if you are a system administrator, or have root
  217.  priveleges, or are modifying your own copy of perl; then copy it to the 
  218.  perl5/Tk directory. Depending on how the module was written it should be
  219.  possible to use it either with the use Tk; statement itself or with an explicit 
  220.  use Tk::Foo; (for module perl5/Tk/Foo.pm). 
  221.  
  222.  Extensions (Overgrown Modules)
  223.  ------------------------------
  224.  
  225.  These may come as a multi-file kit (tape archive usually) and may require a C
  226.  compiler for part of the installation (perl/Tk itself falls into this category).
  227.  You know you have an Overgrown Module (Extension) when there is one or
  228.  more files with an .xs extension (perl->C meta code) and a Makefile.PL
  229.  (perl->make meta code). One invokes the perl MakeMaker on the file called 
  230.  Makefile.PL in order to create a Makefile via: 
  231.  
  232.      perl Makefile.PL
  233.  
  234.  You may now run make on the resultant Makefile - but the details of this
  235.  process are module dependent and should be documented in a README or an 
  236.  INSTALL file. A very standard perl extension requires 4 (or 5 if making static)
  237.  standard commands to make and install: 
  238.  
  239.      perl Makefile.PL
  240.      make
  241.      make test
  242.      make install
  243.  
  244.  If you have the appropriate CPAN and FTP modules already installed you can
  245.  retrieve a module from CPAN and carry out all of the above steps with a perl
  246.  one-liner like this: 
  247.  
  248.      perl -MCPAN -e 'install "Foo"'
  249.  
  250.  ______________________________________________________________________
  251.  
  252.  
  253.  
  254.  15. How do I write new modules? 
  255.  
  256.  You might want to start by poking around your perl/Tk build directory. Is there
  257.  something there that already does what you want? Is there something that is
  258.  reasonably close - but only requires minor modification? 
  259.  
  260.  Next go through the various perl documents - including the FAQ as well as
  261.  the various relevant man pages: perlmod(1), perlobj(1), perlbot(1),
  262.  (and please don't forget: perlpod(1)!) 
  263.  
  264.  Post your idea to comp.lang.perl.tk and discuss it with others - there might
  265.  very well be someone working on an approach already. A clear explanation of
  266.  all the stuff that gets put into a module was posted to the mailing list and can
  267.  be found in the archive at: 
  268.  
  269.      http://sun20.ccd.bnl.gov/~ptk/archive/ptk.1995.10/0012.html
  270.  
  271.  Also, be sure to check out a recent version of the official Module List that Tim
  272.  Bunce <Tim.Bunce@ig.co.uk> and Andreas Koenig 
  273.  <a.koenig@franz.ww.TU-Berlin.DE> maintain and post to 
  274.  comp.lang.perl.announce periodically. The list is also available at any CPAN
  275.  ftp site as well as: 
  276.  
  277.      http://www.perl.com/CPAN/modules/00modlist.long.html
  278.      ftp://rtfm.mit.edu/pub/usenet/news.answers/perl-faq/module-list
  279.      ftp://ftp.demon.co.uk/pub/perl/db/mod/module-list.txt
  280.      ftp://ftp.wpi.edu/perl5/Modules/module_list.txt
  281.  
  282.  Finally ready to ship? Small (perl/Tk) modules have been posted directly to 
  283.  comp.lang.perl.tk. Big modules may require ftp distribution (see upload info at
  284.  one of the CPAN sites) then make your announcement to comp.lang.perl.tk
  285.  and possibly to comp.lang.perl.announce. 
  286.  
  287.  ______________________________________________________________________
  288.  
  289.  
  290.  
  291.  16. Composite Widgets. 
  292.  
  293.  Composite widgets combine the functions of two or more widget primitives
  294.  into something that is not quite a stand alone program but is something that
  295.  may prove very useful for inclusion in your own scripts. A variety of
  296.  composite widgets have been written and many are still being worked on.
  297.  Many come bundled with your perl/Tk distribution kit, and some are simply
  298.  posted to comp.lang.perl.tk. It is quite common to have composite widgets
  299.  written in perl modules - usually in terms of the Tk widget primitives.
  300.  Graphical examples of some of the composites discussed here can be seen by
  301.  GUI browsers at: 
  302.  
  303.      http://w4.lns.cornell.edu/~pvhp/ptk/ptkIMG.html
  304.  
  305.  ______________________________________________________________________
  306.  
  307.  
  308.  
  309.  16.1. How do I get a Dialog box? 
  310.  
  311.  For things like a simple "are you sure?" dialog box you might want to take a
  312.  look at perl5/Tk/Dialog.pm. This module may be invoked with require
  313.  Tk::Dialog; etc. - there are much more extensive directions inside the
  314.  comment fields at the top of the Dialog.pm file itself. The module has a lot
  315.  of options and has a tutorial driver script in perl5/Tk/demos/dialog.
  316.  Dialog.pm is also used by the perl5/Tk/demos/widget demo. In particular
  317.  look at perl5/Tk/demos/widget_lib/dialog1.pl and dialog2.pl for
  318.  examples of how one makes use of Tk::Dialog. A snippet of a script that uses
  319.  this module could look like: 
  320.  
  321.      require Tk::Dialog;
  322.  
  323.      my $mw = MainWindow->new;
  324.      my $D = $mw->Dialog(
  325.                   -title => 'Are you sure?',
  326.                   -text  => "You have requested rm \*\nAre you sure?",
  327.                   -default_button => 'No',
  328.                   -buttons        => ['No','yes']
  329.                         );
  330.      my $choice = $D->Show;  # use Show for Tk-b9.01
  331.  # if using Tk-b8:    my $choice = $D->show;
  332.  
  333.      print " you chose $choice \n";
  334.  
  335.  A question concerning configuration of the Subwidgets on the Dialogs came
  336.  up recently: 
  337.  
  338.  <Greg_Cockerham@avanticorp.com> wrote:
  339.  ! I want to reconfigure the colors of the Dialog and
  340.  ! ErrorDialog buttons.  How do I do this?
  341.  ! Thanks in advance.
  342.  
  343.     $dialog_widget->configure(-background => 'purple'); 
  344.  
  345.   Since these two widgets are composites you manage them like any 'ol
  346.   widget. If the default delegate subwidget(s) aren't to your liking you can
  347.   always get to individual component widgets of the composite via the 
  348.   ->Subwidget() method. 
  349.  
  350.   I see these subwidgets: 
  351.  
  352.  Dialog
  353.     'message' is the label subwidget with the dialog text, and 'bitmap' is the
  354.     label subwidget showing the dialog bitmap
  355.  ErrorDialog
  356.     'error_dialog' is Dialog subwidget, 'text' is text subwidget
  357.  
  358.   You can even do things like this: 
  359.  
  360.   $error_dialog->Subwidget('error_dialog')->
  361.                      Subwidget('bitmap')->configure(..);
  362.  
  363.   to "get to" the label widget of the dialog component of the error_dialog
  364.   widget..... 
  365.  
  366.   Be sure to also check out the "dialog" demo. 
  367.  
  368.  ______________________________________________________________________
  369.  
  370.  
  371.  
  372.  16.2. Is there a file selector? 
  373.  
  374.  Yes, there may be several eventually... 
  375.  
  376.  One distributed with the perl/Tk code kit itself is called FileSelect.pm and was
  377.  written by Frederick L. Wagner - (based on an original by Klaus
  378.  Lichtenwalder). 
  379.  
  380.  Another module called SelFile.pm was adapted by Alan Louis Scheinine from
  381.  Wagner's FileSelect.pm. It is available from: 
  382.  
  383.      http://sun20.ccd.bnl.gov/~ptk/archive/ptk.1995.11/0122.html
  384.  or
  385.      http://w4.lns.cornell.edu/~pvhp/ptk/etc/SelFile.pm
  386.  
  387.  A module called FileSave.pm allows one to type in a new (non-existant)
  388.  filename for "Save as..." type operations. It was posted by Mark Elston on 12
  389.  Oct 1995 to the mailing list and is available from: 
  390.  
  391.      http://sun20.ccd.bnl.gov/~ptk/archive/ptk.1995.10/0093.html
  392.  or
  393.      http://w4.lns.cornell.edu/~pvhp/ptk/etc/FileSave.pm
  394.  
  395.  A slightly different behaviour is to be had with Brent B. Powers' 
  396.  FileDialog.pm that was posted to the mailing list on 12 Jan 1996 and
  397.  available from: 
  398.  
  399.      http://sun20.ccd.bnl.gov/~ptk/archive/ptk.1995.12/0201.html
  400.  or
  401.      http://w4.lns.cornell.edu/~pvhp/ptk/etc/FileDialog.pm
  402.  
  403.  Harry Bochner chimed in with SaveAs.pm. It is available from: 
  404.  
  405.      http://w4.lns.cornell.edu/~pvhp/ptk/etc/SaveAs.pm
  406.  
  407.  In general, if there is a feature that you want missing from one of these, or
  408.  some behaviour that you would like to see modified then by all means cp the
  409.  source code to your area and start hacking ;-) 
  410.  
  411.  ______________________________________________________________________
  412.  
  413.  
  414.  
  415.  16.3. Is there a color editor? 
  416.  
  417.  There is. Please see 
  418.  
  419.      perldoc ColorEditor.pm
  420.  
  421.  or run the Tk/demos/color_editor demo script for more information. 
  422.  
  423.  ______________________________________________________________________
  424.  
  425.  
  426.  
  427.  16.4. Is there a round Scale? 
  428.  
  429.  It is not quite a "round Scale" but Roy Johnson has written "Dial.pm" for
  430.  round dial (or speedometer) -like settable widgets. It is available from: 
  431.  
  432.      http://sun20.ccd.bnl.gov/~ptk/archive/ptk.1995.08/0431.html
  433.  or
  434.      http://w4.lns.cornell.edu/~pvhp/ptk/etc/Dial.pm
  435.  
  436.  As well as from the Contrib/ sub-directory of your perl/Tk build directory. 
  437.  
  438.  ______________________________________________________________________
  439.  
  440.  
  441.  
  442.  16.5. Is there something equivalent to tkerror? 
  443.  
  444.  Yes there is. Please see the Tk/ErrorDialog.pm module for further
  445.  information. 
  446.  
  447.  ______________________________________________________________________
  448.  
  449.  
  450.  
  451.  16.6. Are there Tables? 
  452.  
  453.  There are least two: 
  454.  
  455.  Nick's Table
  456.  ------------
  457.  
  458.  Nick Ing-Simmons has distributed his own Table widget package with
  459.  Tk-b9.01 (and higher). It is used through a use TK::Table; and 
  460.  $top->Table(); calls. A rather detailed demo of this widget/geometry
  461.  manager's capabilities can be found in the table_demo script (in your 
  462.  Tk-b9.01/ build directory). There is also pod in the perl5/Tk/Table.pm
  463.  file. You may also browse the perl Tk::Table man page on the web at 
  464.  
  465.      http://w4.lns.cornell.edu/~pvhp/ptk/etc/Table.pm.html
  466.  
  467.  Guy Decoux's BLT_Table
  468.  ----------------------
  469.  
  470.  Guy Decoux <decoux@moulon.inra.fr> has ported the popular BLT_Table
  471.  Tcl/Tk tabular geometry manager to perl/Tk. It was known to work with
  472.  Tk-b8. You may obtain the latest version of it either from 
  473.  
  474.      ftp://moulon.inra.fr/pub/pTk/
  475.  
  476.  or from a CPAN site in the authors/id/GUYDX/ directory. You may also
  477.  browse the perl BLT_Table man page on the web at 
  478.  
  479.      http://w4.lns.cornell.edu/~pvhp/ptk/etc/Table.html
  480.  
  481.  ______________________________________________________________________
  482.  
  483.  
  484.  
  485.  17. Programming/development tools. 
  486.  
  487.  There are a number of tools and methods to help you with your perl/Tk
  488.  scripting and development. It is worthwhile to note here that the -w switch is
  489.  recommended as is the use strict; statement near the top of your
  490.  script/program. If it dies and you still cannot decrypt the error message that
  491.  these generate take a look though man perldiag(1). 
  492.  
  493.  ______________________________________________________________________
  494.  
  495.  
  496.  
  497.  17.1 Is there a Tcl/Tk to perl/Tk translator? 
  498.  
  499.  Nick Ing-Simmons has written a (rather lengthy) tcl2perl script. It is
  500.  distributed with the perl/Tk build kit. Please handle carefully! (translation: do
  501.  not expect it to translate arbitrary tcl code accurately nor even into the most
  502.  efficient perl/Tk equivalent. Do go over the converted script with care - and
  503.  do not forget -w and use strict;.) Thanks Nick :-) 
  504.  
  505.  ______________________________________________________________________
  506.  
  507.  
  508.  
  509.  17.2 Is there something equivalent to wish in perl/Tk? 
  510.  
  511.  The answer is yes. 
  512.  
  513.  The idea of wish is that you read from <STDIN> and evaluate each statement.
  514.  The standard way to do this in perl/Tk is to use the tkpsh script that comes in
  515.  your perl/Tk build directory. Another elegant way to get wish like behavior in
  516.  perl/Tk is to use rmt which you can find in perl5/Tk/demos in your perl/Tk
  517.  distribution. When you run rmt you already have Tk.pm set up for you so you
  518.  can start typing things like $mmm = new MainWindow; etc. at the rmt:
  519.  prompt. (This use belies the power of rmt which is derived from Ousterhout's
  520.  Tcl/Tk version of rmt [see section 27.2 of his book]. rmt is capable of
  521.  "inserting Tk code" into simultaneously running Tk applications.) 
  522.  
  523.  A cruder way to get wish-like behaviour with perl/Tk is to run a "perl shell"
  524.  and type in your usual commands, including use Tk; etc. There is a script
  525.  distributed with perl called perlsh which is written quite simply as: 
  526.  
  527.      #!/usr/bin/perl
  528.       $/ = '';        # set paragraph mode
  529.       $SHlinesep = "\n";
  530.       while ($SHcmd = <>) {
  531.           $/ = $SHlinesep;
  532.           eval $SHcmd; print $@ || "\n";
  533.           $SHlinesep = $/; $/ = ''; 
  534.       }
  535.  
  536.  You can use this during code development to test out little snippets of code. It
  537.  helps to be an accurate typist and the use strict; is optional here :-) 
  538.  
  539.  KOBAYASI Hiroaki has a more sophisticated wish like perl/Tk "shell" that
  540.  is called EVA. It is available from: 
  541.  
  542.      ftp://ftp.sowa.is.uec.ac.jp/pub/Lang/perl5/Tk/eva-*.tar.gz
  543.  
  544.  ______________________________________________________________________
  545.  
  546.  
  547.  
  548.  17.3. Is there a debugger specifically for perl/Tk? 
  549.  
  550.  Not for the latest version - but the -w switch and use strict; are always
  551.  helpful with debugging as they provide informative error messages. 
  552.  
  553.  You can, of course, run under the standard perl debugger using the -d switch
  554.  like so: 
  555.  
  556.      perl -d myscript
  557.  
  558.  But it is recommended that you set you breakpoints carefully since just the
  559.  calls to ManWindow->new require many steps. 
  560.  
  561.  (Older information): Gurusamy Sarathy <gsar@engin.umich.edu> had built a
  562.  PERL5DB file called Tkperldb (which despite the name is for pTk not
  563.  Tk/perl). One must install an early de-bugger then apply a patch to bring the
  564.  debugger up to date. The early debugger kit was available from: 
  565.  
  566.      ftp://ftp.perl.com/pub/perl/ext/TK/Tkperldb-*.tar.gz
  567.  
  568.  And Gurusamy Sarathy notes that the patch to bring the debugger up to date
  569.  is available at: 
  570.  
  571.   You need a post 5.001m perl that has support for debugging closures. 
  572.   Or you can simply apply:
  573.  
  574.     http://www-personal.umich.edu/~gsar/perl5.001m-bugs.patch
  575.  
  576.   to 5.001m. (5.002beta includes all the fixes in the above patch).
  577.  
  578.  Note that a perl debugger may be invoked within your script with a line like: 
  579.  
  580.      $ENV{'PERL5DB'} = 'BEGIN { require Tkperldb }';
  581.  
  582.  See man perldebug(1) for more help. 
  583.  
  584.  Keep in mind that you are programming in perl after all. The perl debug line
  585.  mode is available to you through executing the following from your shell: 
  586.  
  587.      perl -de 0
  588.  
  589.  Whereupon you must enter all the lines of a script including use Tk;.
  590.  (Fancier file reads & evals are possible - but if you are getting that
  591.  sophisticated why not create your own custom PERL5DB file?) When using 
  592.  perl -dwe 0 beware of the emacs like line editing under this debugger, and
  593.  be forewarned that as soon as you type in the MainLoop; statement perl will
  594.  no longer read from <STDIN>. 
  595.  
  596.  Ilya Zakharevich <ilya@math.ohio-state.edu> points out that very recent
  597.  perldb versions will allow for simultaneous X and STDIN reads. He also points
  598.  out: 
  599.  
  600.  Note that you may use 
  601.  
  602.      sub myLoop {
  603.        if (defined &DB::DB) {
  604.          while (1) {             # MainWindow->Count
  605.            Tk::DoOneEvent(0);
  606.          }
  607.        } else {
  608.          MainLoop;
  609.        }
  610.      }
  611.  
  612.  (and I hope the analogous provision will be in MainLoop in 
  613.   tk-b9 - hi, Nick ;-)
  614.  
  615.  ______________________________________________________________________
  616.  
  617.  
  618.  
  619.  17.4. Is there a GUI builder in perl/Tk? 
  620.  
  621.  Work has reputedly (January 1996) started on porting a Tcl/Tk GUI builder
  622.  known as SpecTcl for use with perl/Tk. For the Tcl/Tk SpecTcl kit see: 
  623.  
  624.      ftp://ftp.sunlabs.com/pub/tcl/SpecTcl-*.tar.[gz|Z]
  625.  
  626.  and address questions about SpecTcl to <spectcl@tcl.eng.sun.com>. 
  627.  
  628.  In <news:ANDREAS.96Mar24234521@marvin.berlin.de> Andreas
  629.  Koschinsky <marvin@logware.de> announced a perl script for use with
  630.  SpecTcl that has some interesting capabilies: 
  631.  
  632.   24 Mar 1996 22:45:21 GMT
  633.   ... So i wrote a perl-script that can convert project-file (.ui-files) which
  634.   spectcl writes. The script reads the .ui-file and generates an equivalent
  635.   perl-module. 
  636.  
  637.  The URL for ui2perl should be something like: 
  638.  
  639.      ftp://susan.logware.de/pub/incoming/ui2perl*.tar.gz
  640.  
  641.  Somewhat more removed from SpecTcl there is also SWIG. 
  642.  
  643.  ______________________________________________________________________
  644.  
  645.  
  646.  
  647.  18. Processes & Inter-Process Communication under Perl/Tk. 
  648.  
  649.  Inter-Process Communication (IPC) is the subject of spawning and
  650.  controlling other programs or "processes" from within perl (sometimes using
  651.  sockets to do so). The subject is briefly discussed in the perlipc(1) man
  652.  page, and was addressed towards the end of Chapter 6 of The Camel. The
  653.  subject is also discussed in the perl FAQ and at Tom Christiansen's ftp site
  654.  (in the various perlipc* files) at: 
  655.  
  656.      ftp://ftp.perl.com/perl/info/everything_to_know/    199.45.129.30
  657.  
  658.  as well as the web site at: 
  659.  
  660.      http://www.perl.com/perl/everything_to_know/ipc/index.html
  661.  
  662.  In addition to the usual perl IPC routines Tk allows (at least) three more
  663.  special functions: fileevent (for handling I/O events), send (for
  664.  inter-widget communication), and after (for time control like a sleep
  665.  expressly for widgets). 
  666.  
  667.  Remember:
  668.  
  669.   If a packet hits a pocket on a socket on a port,
  670.   And the bus is interrupted as a very last resort,
  671.   And the address of the memory makes your floppy disk abort,
  672.   Then the socket packet pocket has an error to report!
  673.   -Ken Burchill(?) 
  674.  
  675.  ______________________________________________________________________
  676.  
  677.  
  678.  
  679.  18.1. How does one get Perl/Tk to act on events that are not coming from X? 
  680.  
  681.  On 22 Nov 1995 (Yaniv Bargury) bargury@milcse.cig.mot.com wrote: 
  682.  
  683.   I need to write a GUI monitor, that displays the status and controls a set
  684.   of processes running in the background. The idea is to have the GUI
  685.   application start a few child processes, command the children through
  686.   pipes from the GUI to the children, and display the children status
  687.   coming on pipes from the children to the GUI in real time. 
  688.  
  689.   The GUI must not be busy waiting, because the CPU resources are
  690.   limited. This excludes using the Tk_DoWhenIdle as explained in the
  691.   manual. 
  692.  
  693.   The usual way to do this is to for the GUI process to have one select()
  694.   in its main loop. That select() should wait for X events or input from
  695.   the pipes leading from the children. 
  696.  
  697.   How do you do this? 
  698.  
  699.  To which Nick Ing-Simmons <nik@tiuk.ti.com> replied: 
  700.  
  701.   fileevent - it is the hook into the select() in the mainloop. 
  702.  
  703.  In addition Avi Deitcher <avi@morgan.com> replied with: 
  704.  
  705.   I wrote something similar to effectively do a tail -f on multiple hosts,
  706.   displaying the result on separate text widgets. Do the following: 
  707.  
  708.      parent
  709.       child
  710.       child
  711.       child
  712.       ..
  713.  
  714.   with a one-way pipe from each child to the parent. Set up the following: 
  715.  
  716.      $main->fileevent(FILEHANDLE,status,subroutine);
  717.  
  718.   for each pipe that you have. This will cause pTk to monitor the 
  719.   FILEHANDLE and call 'subroutine' when an event happens on that
  720.   handle. In this case: FILEHANDLE = pipename status =
  721.   'readable' or 'writable' or 'exception' and subroutine = any
  722.   subroutine that you want. 
  723.  
  724.  To provide a concrete example of fileevent usage Stephen O. Lidie wrote a
  725.  wonderful little GUI tail monitor he calls tktail: 
  726.  
  727.      #!/usr/local/bin/perl -w
  728.      #
  729.      # tktail pathname
  730.      
  731.      use English;
  732.      use Tk;
  733.      
  734.      open(H, "tail -f -n 25 $ARGV[0]|") or die "Nope: $OS_ERROR";
  735.      
  736.      $mw = MainWindow->new;
  737.      $t = $mw->Text(-width => 80, -height => 25, -wrap => 'none');
  738.      $t->pack(-expand => 1);
  739.      $mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);
  740.      MainLoop;
  741.      
  742.      sub fill_text_widget {
  743.      
  744.          my($widget) = @ARG;
  745.      
  746.          $ARG = <H>;
  747.          $widget->insert('end', $ARG);
  748.          $widget->yview('end');
  749.      
  750.      } # end fill_text_widget
  751.  
  752.  An example of how one might use such a script would be to create and
  753.  monitor a file foo like so: 
  754.  
  755.      echo Hello from foo! > foo
  756.      tktail foo &
  757.      echo \"A ship then new they built for him/of mithril and of elven glass\" --Bilbo \
  758.       >> foo
  759.  
  760.  ______________________________________________________________________
  761.  
  762.  
  763.  
  764.  18.2. Is there a send and do I need xauth? 
  765.  
  766.  There is a Tk::send, but to use it own must write one's own version of 
  767.  Tk::receive. An example of this may be found in the rmt program
  768.  distributed with perl/Tk. Note that as of Tk-b12 (including the released
  769.  version Tk400.200) the script that receives from a Tk::send must run with
  770.  taint chcecking turned on (i.e. with the -T switch thrown) and it must untaint
  771.  all commands received from the other process. 
  772.  
  773.  The Tk::send <-> Tk::receive process will work under xhost +
  774.  authority. The security this affords comes from the fact that anyone who
  775.  would want to exploit it would have to know how to write a Tk::receive
  776.  custom tailored to your application (in addition to all the other protocol
  777.  hacking). 
  778.  
  779.  Please note that while you may not need xauth authorization it is
  780.  nevertheless always a good idea. 
  781.  
  782.  ______________________________________________________________________
  783.  
  784.  
  785.  
  786.  18.3. How can I do animations using after? 
  787.  
  788.  There is a "toggling button" demo script supplied with Tk called after_demo
  789.  that makes effective use of after(). 
  790.  
  791.  Terry Greenlaw <terry@encompass.is.net> of Encompass Technologies
  792.  posted a character cell animator for the really bored. Here it is in a slightly
  793.  modified form that allows string input from the command line (note too the
  794.  recursive call that doesn't sop up system memory): 
  795.  
  796.  #!/usr/bin/perl
  797.  
  798.  =head1 NAME
  799.  
  800.  From: z50816@mip.lasc.lockheed.com "Terry Greenlaw"  Thu Feb 1 12:02:24 EST 1996
  801.  To: ptk@guest.WPI.EDU
  802.  Subj: A code sample for the REALLY bored
  803.  
  804.  For everyone with a case of Browser envy after using Microsoft's Internet
  805.  Explorer, here's a perl/tk script only slightly more useful than a script
  806.  to do <BLINK>. Don't know why I wrote it. Don't know why you'd run it.
  807.  Maybe if you were writing a ticker tape application. Or had a weird thing
  808.  for Times Square. Anyway....
  809.  
  810.  tog
  811.  Terry Greenlaw (on-site @ Lockheed Martin)      Encompass Technologies
  812.  z50816@mip.lasc.lockheed.com                    terry@encompass.is.net
  813.  
  814.  ##################################################################
  815.  
  816.  =cut
  817.  
  818.      #!/usr/bin/perl
  819.      
  820.      #use strict;
  821.      use Tk;
  822.      
  823.      $message=join(' ',@ARGV,''); 
  824.      if (!$message) {
  825.          $message="THIS IS A VERY LONG SCROLLING MESSAGE...      ";
  826.          $topmssg="This is the top of the screen";
  827.          $botmssg="This is the bottom of the screen";
  828.      }
  829.      else {
  830.          $topmssg=$message;
  831.          $botmssg=$message;
  832.      }
  833.      $top = MainWindow->new;
  834.      $l1 = $top->Label(-fg => 'White', -text => $topmssg);
  835.      $l1->pack(-fill => 'both', -expand => 1 );
  836.      $m1 = $top->Label(-fg=>'Red', -bg=>'black',
  837.                        -textvariable => \$message, 
  838.                        -width => 15 
  839.                        );
  840.      $m1->pack();
  841.      $m2 = $top->Label(-wrap=>1, 
  842.                        -fg=>'Green', -bg=>'black',
  843.                        -textvariable => \$message2, 
  844.                        -width=>1, -height=>8 
  845.                        );
  846.      $m2->pack(-anchor=>'w');
  847.      $l2 = $top->Label(-fg => 'White', -text => $botmssg);
  848.      $l2->pack(-fill => 'both', -expand => 1 );
  849.      
  850.      after(100, \&scroll_it);
  851.      
  852.      $top->MainLoop;
  853.      
  854.      sub scroll_it {
  855.          $message =~ /(.)(.*)/;
  856.          $message="$2$1";
  857.          ($message2 = $message) =~ s/ /  /g;
  858.          after(100, \&scroll_it);
  859.      }
  860.      __END__
  861.  
  862.  (Please note that a script like this is now distributed as "TickerTape" in
  863.  your Tk*/Contrib/ directory.) 
  864.  
  865.  ______________________________________________________________________
  866.  
  867.  
  868.  
  869.  18.4. How do I update widgets while waiting for other processes to complete? 
  870.  
  871.  The short answer is either 
  872.  
  873.      $widget -> update;
  874.  or
  875.      $widget -> DoOneEvent;
  876.  
  877.  Here is a script that makes use of the first of these methods. Note that instead
  878.  of actually doing something useful the "long running process" is simply a call
  879.  to the perl sleep() function for illustrative purposes: 
  880.  
  881.      #!/usr/bin/perl -w
  882.      
  883.      use Tk;
  884.      
  885.      my $m = MainWindow->new();
  886.      my $l = $m -> Listbox();
  887.      $l -> bind('<Double-1>' => sub{sleepy($l)} );
  888.      my @nuts   = qw(Almond Brazil Chestnut Doughnut Elmnut Filbert);
  889.      for (@nuts) { $l -> insert('end',$_); }
  890.      $l -> pack;
  891.      MainLoop;
  892.      
  893.      sub sleepy {
  894.          my $widget = shift;
  895.          print "before 1st sleep \n";
  896.          sleep(10);
  897.          print "after 1st sleep before delete \n";
  898.          $widget -> delete('active');
  899.          $widget -> update;             # try [un]*commenting this
  900.          print "after delete before 2nd sleep \n";
  901.          sleep(10);
  902.          print "after 2nd sleep \n";
  903.      }
  904.      __END__
  905.  
  906.  ______________________________________________________________________
  907.  
  908.  
  909.  
  910.  18.5. How do you fork on System V (HP)? 
  911.  
  912.  Kraegeloh Martin <mkr@dm-server.cv.com> originally asked: 
  913.  
  914.  
  915.  ! Subj: signal handling difference on HP vs. SUN
  916.  !
  917.  ! the following code will fork an xterm with vi in it, and it
  918.  ! will refuse to do so while the first xterm is still running.
  919.  ! works fine on my sun.
  920.  ! On HP however, the second time an xterm is started, NO handler
  921.  ! is called when the child dies.
  922.  !
  923.  ! the code:
  924.  ! ===================== 8< ===============================
  925.  ! $SIG{CHLD}=\&w;
  926.  !
  927.  ! sub w{
  928.  !    $pid=wait;
  929.  !    print STDERR "died: $pid\n";
  930.  !    if ( $have == $pid ) { $have = 0; }
  931.  ! }
  932.  
  933.  To which a part of Nick Ing-Simmons' response was: 
  934.  
  935.   I suspect HPUX is SysV-ish not BSD or POSIX. So every time a signal
  936.   fires, it removes the handler - you need to reset it in the handler: 
  937.  
  938.      sub w{
  939.          $SIG{CHLD}=\&w;
  940.          $pid=wait;
  941.          print STDERR "died: $pid\n";
  942.          if ( $have == $pid ) { $have = 0; }
  943.       }
  944.   
  945.  
  946.   Whether you reset it before/after the wait may be very important ... 
  947.  
  948.  Then Bjarne Steinsbo <bjarne@hsr.no> followed up with: 
  949.  
  950.   That's not the whole story... Another problem is that SIGCLD interrupts
  951.   the read system call on SysV-ish (I like that word! :-) systems. This
  952.   means that you have to test why "" fails, and act accodingly. A program
  953.   that works on both Sun and HP is: 
  954.  
  955.      $SIG{CHLD}=\&w;
  956.      while(1){
  957.         $_ = ;
  958.         $! = 0, next if $! =~ /Interrupted/;
  959.         last if $! or !defined $_;
  960.         if($have){
  961.              print STDERR "child still alive\n";
  962.         }
  963.         else{
  964.              if(($pid=fork()) != 0){
  965.                 $have=$pid;
  966.                 print STDERR "forked $pid\n";
  967.              }
  968.              else {
  969.                 exec("xterm -e vi") 
  970.              }
  971.         }
  972.      }
  973.  
  974.      sub w{
  975.         $pid=wait;
  976.         print STDERR "died: $pid\n";
  977.         if ( $have == $pid ) { $have = 0; }
  978.         $SIG{CHLD}=\&w;
  979.      }
  980.  
  981.