Perl + Objects = Winning combination

M

Mumia W.

After writing a previous version of this program

http://home.earthlink.net/~mumia.w.18.spam/games_fever/index.html

I received feedback that the lightest of the green squares were
difficult to distinguish from the gray background. Fortunately, I'd
written the program in Perl-Tk using object oriented features.

I decided, for reasons of personal aesthetics, that I would make only
minimal changes to the color scheme. Instead of changing the colors, I
wanted to put a border around the "selected" squares--which are really
just Tk-labels.

The problem with borders is that they take up space. Any label that I
put a border around would be slightly larger than the labels around it
(if those others didn't also have borders), so I needed to put borders
around /all/ labels. My initial plan was to make all borders the same
color as the background (e.g. invisible border) and highlight labels by
making the border black; however, alas, Tk does not seem to allow a
Tk::Label to have a border of one color and font text in another color.
I came to the conclusion that Tk::Label wasn't going to do it.

This had the potential to stop my new feature right out of the starting
gate because all of my GUI code assumed that it was dealing with
Tk::Labels. I didn't want to rewrite all that code, so I needed to
subclass Tk::Label.

And it was the easiest thing in the world. I created a new object type,
YOYO::Label, that inherited from Tk::Label. That object consists of a
label within a frame; the frame has one color, and the label has
another. I now have perfect labels with black text and gray borders.

After adding to my program one 54-line module (YOYO::Label) and two
lines in the main program, I have the desired feature. And it took me
all of twenty minutes to add it, and ten of that was spent reading the
POD for Tk::Options.

Perl-Tk is *better* than TCL/TK. TCL is not object-oriented. I would've
written the first version of the program with TCL/TK Label widgets, and
when the change to a new type of label was needed, I'd have to do reams
of modifications in the main portion of the GUI code since there is no
subclassing in the procedural language TCL.

This is how programming is supposed to be done. Small changes should
require a small amount of work, and when you do OO right, that what you get.

Perl plus objects and Tk is a winning combination. Yahoo!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top