Perl FAQ 3.9: How can I use X or Tk with Perl?

Perl FAQ 3.9

How can I use X or Tk with Perl?

Right now, you have several choices. If you are still using perl4, use the WAFE or STDWIN packages, or try to make your own usub binding.

However, if you've upgraded to version 5, you have several exciting possibilities, with more popping up each day. Right now, Tk and Sx are the best known such extensions.

If you like the tk package, you should get the Tk extension kit, written by Nick Ing-Simmons*. The official distribution point is at

ftp://ftp.wpi.edu/perl5/private/Tk-b8.tar.gz

but many of the major archive sites now have it in their /ext{entions} directory also. Depending upon your location, you may be better off checking there. Also, understand that the version number may have changed by the time you read this.

This package replaced the tkperl5 project, by Malcolm Beattie*, which was based on an older version of Tk, 3.6 as compared to the current 4.X. This package was also known as nTk (new Tk) while it was in the alpha stages, but has been changed to just Tk now that it is in beta. Also, be advised that you need at least perl5.001 (preferably 5.002, when it becomes available) and the official unofficial patches.

You may also use the old Sx package, (Athena & Xlib), written by originally written by by Dominic Giampaolo*, then and rewritten for Sx by Frédéric Chauveau*. It's available from these sites:

STDWIN is a library written by Guido van Rossum* (author of the Python programming language) that is portable between Mac, Dos and X11. One could write a Perl agent to speak to this STDWIN server.

WAFE is a package that implements a symbolic interface to the Athena widgets (X11R5). A typical Wafe application consists in our framework of two parts: the front-end (we call it Wafe for Widget[Athena]front end) and an application program running typically as a separate process. The application program can be implemented in an arbitrary programming language and talks to the front-end via stdio. Since Wafe (the front-end) was developed using the extensible TCL shell (cite John Ousterhout), an application program can dynamically submit requests to the front-end to build up the graphical user interface; the application can even down-load application specific procedures into the front-end. The distribution contains sample application programs in Perl, GAWK, Prolog, TCL, and C talking to the same Wafe binary. Many of the demo applications are implemented in Perl. Wafe 0.9 can be obtained via anonymous ftp from
ftp.wu-wien.ac.at[137.208.3.5]:pub/src/X11/wafe-0.9.tar.Z

Alternatively, you could use wish from tcl.

#!/usr/local/bin/perl
#####################################################################
#  An example of calling wish as a subshell under Perl and
#  interactively communicating with it through sockets.
#
#  The script is directly based on Gustaf Neumann's perlwafe script.
#
#  Dov Grobgeld dov@menora.weizmann.ac.il
#  1993-05-17
#####################################################################

    $wishbin = "/usr/local/bin/wish";

    die "socketpair unsuccessful: $!!\n" unless socketpair(W0,WISH,1,1,0);
    if ($pid=fork) {
	    select(WISH); $| = 1;
	    select(STDOUT);


	# Create some TCL procedures
	    print WISH 'proc echo {s} {puts stdout $s; flush stdout}',"\n";

	# Create the widgets
	print WISH &;lt;&;lt;TCL;
	# This is a comment "inside" wish

	frame .f -relief raised -border 1 -bg green
	pack append . .f {top fill expand}

	button .f.button-pressme -text "Press me" -command {
	    echo "That's nice."
	}
	button .f.button-quit -text quit -command {
	    echo "quit"
	}
	pack append .f .f.button-pressme {top fill expand} \\
		       .f.button-quit {top expand}

TCL
	;
	# Here is the main loop which receives and sends commands
	# to wish.
	while () {
	    chop;
	    print "Wish sais: <$_>\n";
	    if (/^quit/) { print WISH "destroy .\n"; last; }
	}
	    wait;
    } elsif (defined $pid) {
	open(STDOUT, ">&W0");
	open(STDIN, ">&W0");
	close(W0);
	select(STDOUT); $| = 1;
	exec "$wishbin --";
    } else {
	die "fork error: $!\n";
    }


Other resources at this site: