home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Tk / Tcl-perl.pod < prev    next >
Encoding:
Text File  |  1997-08-10  |  5.3 KB  |  210 lines

  1. =head1 NAME
  2.  
  3. Tcl vs perl - very old suspect documentation on porting.
  4.  
  5. =head1 DESCRIPTION
  6.  
  7. This isn't really a .pod yet, nor is it Tcl vs perl
  8. it is a copy of John's comparison of Malcolm's original perl/Tk
  9. port with the current one. It is also out-of-date in places.
  10.  
  11.  
  12.  
  13.   From: john@WPI.EDU (John Stoffel )
  14.   
  15.   Here are some thoughts on the new Tk extension and how I think the
  16.   organization of the commands looks.  Mostly, I'm happy with it, it
  17.   makes some things more organized and more consistent with tcl/tk, but
  18.   since the overlying language is so different, I don't think we need to
  19.   follow exactly the tcl/tk model for how to call the language.
  20.   
  21.   The basic structure of the Tk program is:
  22.   
  23.   
  24.       require Tk;
  25.   
  26.       $top = MainWindow->new();
  27.   
  28.       #
  29.       # create widgets
  30.       #
  31.   
  32.       Tk::MainLoop;
  33.   
  34.       sub method1 {
  35.       }
  36.   
  37.       sub methodN {
  38.       }
  39.   
  40.   
  41.   This is pretty much the same as tkperl5a5, with some cosmetic naming
  42.   changes, and some more useful command name and usage changes.  A quick
  43.   comparison in no particular order follows:
  44.   
  45.   
  46.   tkperl5a5                Tk
  47.   -------------------------------    -----------------------------------
  48.   $top=tkinit(name,display,sync);    $top=MainWindow->new();
  49.   
  50.   tkpack $w, ... ;        $w->pack(...)
  51.   
  52.   $w = Class::new($top, ...);    $w = $top->Class(...);
  53.   
  54.   tkmainloop;            Tk::MainLoop;
  55.   
  56.   tkbind($w,"<key>",sub);        $w->bind("<key>",sub);
  57.   
  58.   tkdelete($w, ...);        $w->delete(...);
  59.   
  60.   $w->scanmark(...);        $w->scan("mark", ...);
  61.   
  62.   $w->scandragto(...);        $w->scan("dragto", ...);
  63.   
  64.   $w->tkselect();            $w->Select();
  65.   
  66.   $w->selectadjust(...);        $w->selection("adjust", ...);
  67.   
  68.   $w->selectto(...);        $w->selection("to", ...);
  69.   
  70.   $w->selectfrom(...);        $w->selection("from", ...);
  71.   
  72.   $w->tkindex(...);        $w->index(...);
  73.   
  74.   tclcmd("xxx",...);              &Tk::xxx(...)    # all Tk commands, but no Tcl at all
  75.   
  76.   tclcmd("winfo", xxx, $w, ...);  $w->xxx(...);
  77.   
  78.                   $w->mark(...);
  79.   
  80.                   $w->tag(...);
  81.   
  82.   $w->grabstatus();        $w->grab("status");
  83.   
  84.   $w->grabrelease(...);        $w->grab("release", ...);
  85.   
  86.   focus($w);            $w->focus;
  87.   
  88.   update();            Tk->update();
  89.   
  90.   idletasks();            Tk->update("idletasks");
  91.   
  92.   wm("cmd",$w, ...);        $w->cmd(...);
  93.                   
  94.   destroy($w);            $w->destroy();
  95.   
  96.                   Tk::option(...);
  97.                                   $w->OptionGet(name,Class)
  98.   
  99.                   $w->place(...)
  100.   
  101.                   Tk::property(...);
  102.   
  103.   
  104.   $w = Entry::new($parent,...)
  105.   
  106.   is now
  107.   
  108.   $w = $parent->Entry(...)
  109.   
  110.   As this allows new to be inherited from a Window class.
  111.   
  112.     -method=>x,-slave=>y   
  113.   
  114.    is now
  115.   
  116.     -command => [x,y]
  117.   
  118.   1st element of list is treated as "method" if y is an object reference.
  119.   (You can have -command => [a,b,c,d,e] too; b..e get passed as args).
  120.   
  121.   Object references are now hashes rather than scalars and there
  122.   is only ever one such per window.  The Tcl_CmdInfo and PathName
  123.   are entries in the hash.
  124.   
  125.   (This allows derived classes to 
  126.   re-bless the hash and keep their on stuff in it too.)
  127.   
  128.   Tk's "Tcl_Interp" is in fact a ref to "." window. 
  129.   You can find all the Tk windows descended from it as their object 
  130.   references get added (by PathName) into this hash. 
  131.   $w->MainWindow returns this hash from any window.
  132.   
  133.   I think that it should extend to multiple tkinits / Tk->news  
  134.   with different Display's - if Tk code does.
  135.   
  136.   Finally "bind" passes window as "extra" (or only)
  137.   argument. Thus
  138.   
  139.   Tk::Button->bind(<Any-Enter>,"Enter");
  140.   
  141.   Binds Enter events to Tk::Button::Enter by default
  142.   but gets called as $w->Enter so derived class of Button can just 
  143.   define its own Enter method. &EvWref and associated globals and race
  144.   conditions are no longer needed.
  145.   
  146.   
  147.   One thing to beware of : commands bound to events with $widget->bind
  148.   follow same pattern, but get passed extra args :
  149.   
  150.   $widget->bind(<Any-1>,[sub {print shift}, $one, $two ]);
  151.   
  152.   When sub gets called it has :
  153.   
  154.      $widget $one $two 
  155.   
  156.   passed.
  157.   
  158.   1st extra arg is reference to the per-widget hash that serves as the 
  159.   perl object for the widget. 
  160.   
  161.   Every time an XEvent a reference to a special class is placed
  162.   in the widget hash. It can be retrieved by $w->XEvent method.
  163.   
  164.   The methods of the XEvent class are the 
  165.   Tcl/Tk % special characters.
  166.   
  167.   Thus:
  168.   
  169.   $widget->bind(<Any-KeyPress>,
  170.                 sub {
  171.                  my $w = shift;
  172.                  my $e = $w->XEvent;
  173.                  print $w->PathName," ",$e->A," pressed ,$e->xy,"\n");
  174.                 });
  175.   
  176.   XEvent->xy is a special case which returns "@" . $e->x . "," . $e->y
  177.   which is common in Text package.
  178.   
  179.   Because of passing a blessed widget hash to "bound" subs they can be 
  180.   bound to (possibly inherited) methods of the widget's class:
  181.   
  182.   Class->bind(<Any-Down>,Down);
  183.   
  184.   sub Class::Down
  185.   {
  186.    my $w = shift;
  187.    # handle down arrow 
  188.   } 
  189.   
  190.   
  191.   Also:
  192.   
  193.   -command and friends can take a list the 1st element can be a ref to 
  194.   as sub or a method name. Remaining elements are passed as args to the 
  195.   sub at "invoke" time. Thus :
  196.   
  197.   $b= $w->Button(blah blah, '-command' => [sub{print shift} , $fred ]);
  198.   
  199.   Should do the trick, provided $fred is defined at time of button creation.
  200.   
  201.   Thus 1st element of list is equivalent to Malcolm's -method and second 
  202.   would be his -slave.  Any further elements are a bonus and avoid
  203.   having to pass ref to an array/hash as a slave.
  204.   
  205.   
  206.   
  207.   
  208.   
  209.   
  210.