Grygonos has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I come seeking your opinions on this subroutine, it takes a reference to a scalar holding the name of the client, and a reference to a hash of db login properties. It then constructs the gui by creating a hash of arrays to hold corresponding elements.... lemme know what you think.. is it overly verbose.. or does it add to maintainability.

sub guiStart { my ($client,$login) = @_; my $mw = MainWindow->new(-borderwidth=>30, -title=>"Duplicates: ".$$client); my %layout = ('frame' => [], 'button' => [], 'label' => [], 'callback'=> []); #Construct labels $layout{'label'}->[0] = 'List Available Data'; $layout{'label'}->[1] = 'Remove Data'; $layout{'label'}->[2] = 'Add Data'; $layout{'label'}->[3] = 'Identify Duplicates'; $layout{'label'}->[4] = 'Change Client'; $layout{'label'}->[5] = 'Exit Application'; #Set callbacks $layout{'callback'}->[0] = \&listData; $layout{'callback'}->[1] = \&removeData; $layout{'callback'}->[2] = [\&addData,,$client,$login,$mw]; $layout{'callback'}->[3] = \&identifyDuplicates; $layout{'callback'}->[4] = \&changeClient; $layout{'callback'}->[5] = \&exitApp; for (0..5) { #Create the frames $layout{'frame'}->[$_] = $mw->Frame(); #Create the buttons $layout{'button'}->[$_] = $layout{'frame'}->[$_]->Button(-text + =>$layout{'label'}->[$_], -widt +h =>20, -comm +and=>$layout{'callback'}->[$_])->pack(); $layout{'frame'}->[$_]->pack(); } MainLoop; return; }

Grygonos

Replies are listed 'Best First'.
Re: GUI Setup with Tk, overly verbose, or maintainable?
by keszler (Priest) on Jul 02, 2004 at 17:38 UTC
    I'd go with "overly verbose", unless you need references to the majority of the widgets for cgets or changes.

    If that is the case, then I'd suggest adding

    use constant { LIST => 0, DEL => 1, ADD => 2, DUP => 3, CHG => 4, EXIT => 5, };
    so that you can refer to e.g. $layout{'button'}->[ADD] to modify the "Add Data" text.

      Looking back on it I don't need the refs to Button's to be stored... just the labels, callbacks and frames(so I can create a button on that frame via $frame->Button(..); Would that help you think?

Re: GUI Setup with Tk, overly verbose, or maintainable?
by saberworks (Curate) on Jul 02, 2004 at 21:00 UTC
    Since you're asking a style question, I would typically do something like this:
    my $labels = { 0 => 'List Available Data', 1 => 'Remove Data', 2 => 'Add Data', ... }; my $callbacks = { 0 => \&listData, 1 => \&removeData, ... } $layout{label} = $labels; $layout{callback} = $callbacks;
    It eliminates a whole lot of typing and seems to be just as clear.
Re: GUI Setup with Tk, overly verbose, or maintainable?
by toma (Vicar) on Jul 03, 2004 at 23:07 UTC
    I think that your style of code is good for maintainability.

    I like the use constant approach that keszler mentioned. There is a module that can do the counting for you; it is called enum::fields. You also have the opportunity to remove some redundancy from your array initializations.

    sub guiStart { my ($client,$login) = @_; my $mw = MainWindow->new(-borderwidth => 30, -title => "Duplicates: ".$client); use enum::fields qw{ LIST DEL ADD DUP CHG EXIT }; my %layout; $layout{label} = [ 'List Available Data', 'Remove Data', 'Add Data', 'Identify Duplicates', 'Change Client', 'Exit Application', ]; $layout{callback} = [ \&listData, \&removeData, [\&addData,,$client,$login,$mw], \&identifyDuplicates, \&changeClient, \&exitApp, ]; for my $btn (LIST..EXIT) { $layout{frame}->[$btn] = $mw->Frame(); $layout{button}->[$btn] = $layout{frame}->[$btn] ->Button(-text => $layout{label}->[$btn], -width => 20, -command => $layout{callback}->[$btn]) ->pack(); $layout{frame}->[$btn]->pack(); } MainLoop; return; }
    It should work perfectly the first time! - toma

      Thanks, toma that's a neat module. Is there very much overhead involved with it? Is it fairly lightweight?

        You can read the source or invent benchmarks to decide how lightweight it is. I haven't looked at it much. The CAVEATS section of the enum::fields POD explains the module limitations. The author suggests that Class::Delegate may be a better module, but I don't understand the suggestion.

        It should work perfectly the first time! - toma