Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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:

  1. Am I missing some major reason not to use libraries to hide my widget declarations?
  2. What namespace should I put all this junk into?
  3. 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

In reply to Name spaces and Perl::Tk coding style by TGI

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2015-11-25 03:46 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (670 votes), past polls