As we learned earlier, it's best to keep private C widgets distinct from the Perl/Tk distribution. But if we were to do a merge, here are some considerations.
First, copy tkSquare.c (with the same hand edits described previously) to pTk/mTk/generic, copy square_demo.t to t, and then fire up your favorite editor and add these lines to the main Perl/Tk MANIFEST:
Square/Makefile.PL Square/Square.xs Square/Square.pm pTk/mTk/generic/tkSquare.c t/square_demo.t
Now make the Square directory and populate it with the preceding files. The files Square.xs and Square.pm are the exact same files we just used, but Makefile.PL looks like this:
use Tk::MMutil; Tk::MMutil::TkExtMakefile( 'dynamic_ptk' => 1 );
Secondly, we must add a single statement to three files so the Perl/Tk core is "aware of" Tk_SquareCmd.
To pTk/mTk/generic/tk.h, add the followingaround line 1880:
EXTERN int Tk_SquareCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, char **argv));
To pTk/mTk/generic/tkWindow.c, add the following around line 152:
{"square", Tk_SquareCmd, NULL, 1},
To pTk/tk.exc,add the following around line 96:
Tk_SquareCmd
It may be advisable to start fresh and begin with:
make distclean perl Makefile.PL
Making and testing the widget code is similar to what we just did, although development is slower because it's a make from the top of the entire Perl/Tk hierarchy, with scores of dependencies and nested makes. You'll quickly find that the make; make test cycle is ridiculously slow.
Testing is sped up if we use a command of the form:
perl -Mblib Square/t/square_demo.t
This uses all the newly made code (both Perl and the loadable object) from blib, the build library.
We can even fake a make install with a script that might look like this (although the exact pathnames vary):
#!/bin/sh make cp blib/arch/auto/Tk/Square/Square.so /usr/local/lib/perl5/site_perl/5.005/i686- linux/auto/Tk/Square/Square.so
Of course, this only "installs" the C loadable, not the .pm file or autosplit subroutines.
There are various techniques for debugging the C code, but perhaps the easiest is the tried-and-true fprintf statement (don't forget the #include <stdio.h>):
fprintf(stderr, "square size=%d\n", squarePtr->size);
Copyright © 2002 O'Reilly & Associates. All rights reserved.