Search My Techie Guy

Thursday, December 6, 2012

Working with Perl & Tk to create GUIs - Setting up the environment

Today I set out on a mission to spice up my Perl scripting skills by adding GUIs in my scripts and the best way to archive this, is by using perl’s Tk module. Here is how my story unfolded before i finally got it to work.

Operating System: Ubuntu 10.10 - the Maverick Meerkat

Checking if perl is in my environment PATH: 

root@marc# which perl

The above output tells me that i have perl running on my machine otherwise it would return something like “command not found” or an empty string!

Checking the version of perl am running:

root@marc# perl -v

This is perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi
(with 40 registered patches, see perl -V for more detail)

Then I tried a test script to check if my default perl installation already has a Tk module but no luck! which means I had to install the perl Tk module, there are two ways:

1. Download the archive and manually install perl-Tk:

   Download Link: CPAN

Perl scripting with Tk to add Graphical User Interfaces
change to the dir where you saved your download.    Extract the archive: #gunzip Tk-804.028.tar.gz
                # tar -xvf Tk-804.028.tar
change to the Tk dir: #cd Tk-804.028

   #perl Makefile.PL
   #make test
   #make install

2. Use cpan to install Tk (this is the easier and recommended option)
   Make sure you are root otherwise the installation script will have permission issues!

#sudo su

// if you are on a direct internet connection, cpan will automatically download the required modules and the dependencies from CPAN using ftp.
//if you computer is behind a proxy that requires authentication, then you have to modify the cpan config. this is how you add the proxy configuration in cpan.

cpan[1]> o conf init /proxy/
Your ftp_proxy? []
//cpan tries to automatically detect the proxy as show above, enter
Your http_proxy? []
Your no_proxy? []

//for IPs that you want excluded from the proxy config, you can leave this blank and enter

If your proxy is an authenticating proxy, you can store your username
permanently. If you do not want that, just press RETURN. You will then
be asked for your username in every future session.

Your proxy user id? [joshua]

Your password for the authenticating proxy can also be stored
permanently on disk. If this violates your security policy, just press
RETURN. You will then be asked for the password in every future

CPAN: Term::ReadKey loaded ok (v2.30)
Your proxy password?


Please remember to call 'o conf commit' to make the config permanent!

cpan[2]>install Tk

//cpan will download and install TK
//during the installation test phase, you will see a lot of pop ups, that means that your Tk library is being built and it’s an exciting moment :-)

Test your installation, try to run a script that calls the Tk module (
use Tk; ). if the script runs without errors then you are good to go. Your environment it set to run perl with Tk.

In the next posts, i will be sharing my experience with perl + Tk GUIs, this was basically about setting up the development environment and i hope you did the same.

for windows, you don’t have to go through this hustle, just go to ActiveState and download perl for your Windows OS, it comes with all the libraries including Tk.

Download Link: ActiveState
Active State Perl
Post a Comment