TGI has asked for the wisdom of the Perl Monks concerning the following question:
I have a rather complex application that uses Perl::Tk to manage its GUI. I find myself quite frustrated by how cluttered and ugly the code is--all the setup and declaration of all the widgets gets rather unweildy. It occurred to me recently that I could use modules to separate out chunks of my app into different files and namespaces. For any variables that I need to share between modules, I can always use the exporter or set package globals via an intialization subroutine.
The thing is, I've looked at a lot of example code that's available online, and it all uses one big file with lots of declarations approach. No one seems to be using modules in the way I describe.
This leaves me with some burning questions:
- Am I missing some major reason not to use libraries to hide my widget declarations?
- What namespace should I put all this junk into?
- I am mostly familiar with OO style modules, should I stick with OO style or should I go for a classic-exporter using style? Or should I stick with what everyone else seems to be doing?
Here's an example of what I'm talking about. I don't actually set up lots of variables to store settings and define callbacks for interaction, so real code gets much more wordy.
#!/usr/bin/perl # Normal code use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->title("A phone list"); my $top = $mw->Frame()->pack( -fill => 'both', -expand => 'x', -ipadx => 5, -ipady => 5, ); my $list = $mw->Frame()->pack( -fill => 'both', -expand => 'both', -ipadx => 5, -ipady => 5, -anchor => 'n', ); $top->Label( -text => 'Enter some phone numbers.', )->pack( -side => 'top', ); $top->Label( -text => 'Check numbers to activate them.', )->pack( -side => 'top', ); $list->Label( -text => 'Enabled', )->grid( -column => 0, -row => 0, ); $list->Label( -text => 'Phone number', )->grid( -column => 1, -row => 0, ); $list->Label( -text => 'Description', )->grid( -column => 2, -row => 0, ); for ( 1..5 ) { $list->Checkbutton( -textvariable => \$nList->[$_]{enable}, -offvalue => '', -onvalue => 'ACTIVE', )->grid( -column => 0, -row => $_, ); $list->Entry( -textvariable => \$nList->[$_]{number} )->grid( -column => 1, -row => $_, ); $list->Entry( -textvariable => \$nList->[$_]{description} )->grid( -column => 2, -row => $_, ); }
Here I broke almost all the widget setup into another package. The goal here is to keep like stuff together, not really reuse.
use strict; use warnings; use PhoneList; my $mw = MainWindow->new; $mw->title("A phone List"); my $phoneNumbers = [ { number => '123-4321', enable => 'ACTIvE', description => 'Bob', }, { number => '321-1234', enable => '', description => 'Not Bob', }, ]; PhoneList::setup($mw, -numbers => $phoneNumbers;); MainLoop; ### A Different file package PhoneList; sub setup { my $parent = shift; my %args = @_; my $top = $parent->Frame()->pack( -fill => 'both', -expand => 'x', -ipadx => 5, -ipady => 5, ); my $list = $parent->Frame()->pack( -fill => 'both', -expand => 'both', -ipadx => 5, -ipady => 5, -anchor => 'n', ); $top->Label( -text => 'Enter some phone numbers.', )->pack( -side => 'top', ); $top->Label( -text => 'Check numbers to activate them.', )->pack( -side => 'top', ); $list->Label( -text => 'Enabled', )->grid( -column => 0, -row => 0, ); $list->Label( -text => 'Phone number', )->grid( -column => 1, -row => 0, ); $list->Label( -text => 'Description', )->grid( -column => 2, -row => 0, ); for ( 1..5 ) { $list->Checkbutton( -textvariable => \$args{-numbers}->[$_]{enable}, -offvalue => '', -onvalue => 'ACTIVE', )->grid( -column => 0, -row => $_, ); $list->Entry( -textvariable => \$args{-numbers}->[$_]{number} )->grid( -column => 1, -row => $_, ); $list->Entry( -textvariable => \$args{-numbers}->[$_]{description} )->grid( -column => 2, -row => $_, ); } } 1;
Update: Added readmore tags.
TGI says moo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Name spaces and Perl::Tk coding style
by ides (Deacon) on Oct 05, 2005 at 18:21 UTC | |
Re: Name spaces and Perl::Tk coding style
by chester (Hermit) on Oct 05, 2005 at 18:55 UTC | |
by TGI (Parson) on Oct 05, 2005 at 20:05 UTC | |
Re: Name spaces and Perl::Tk coding style
by liverpole (Monsignor) on Oct 06, 2005 at 03:06 UTC | |
Re: Name spaces and Perl::Tk coding style
by zentara (Archbishop) on Oct 05, 2005 at 18:50 UTC | |
Re: Name spaces and Perl::Tk coding style
by strat (Canon) on Oct 06, 2005 at 07:35 UTC | |
Re: Name spaces and Perl::Tk coding style
by pg (Canon) on Oct 05, 2005 at 18:54 UTC | |
Re: Name spaces and Perl::Tk coding style
by elwarren (Priest) on Oct 05, 2005 at 21:13 UTC | |
by TGI (Parson) on Oct 05, 2005 at 22:12 UTC | |
Re: Name spaces and Perl::Tk coding style
by duelafn (Parson) on Oct 10, 2005 at 15:04 UTC |