Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I like your idea of using libraries to hide widget definitions.  I would even suggest going a step further, and creating wrappers to simplify some of your widget subroutines, since many of them use the same parameters.  Here's how I would be inclined to simplify the library module (note btw, that in the call to pack(), you should set the value of -expand to either 0 or nonzero; it's -fill which takes one of "none", "x", or "y", or "both"):
### A Different file package PhoneList; use Tk; sub placement($$) { my ($w, $placement) = @_; if ($placement =~ s/^p,(\w+),(\w+),(\w+)//) { $w->pack(-side => $1, -fill => $2, -expand => $3); } elsif ($placement =~ s/^g,(\d+),(\d+)//) { $w->grid(-column => $1, -row => $2); } else { die "Illegal placement format '$placement'\n"; } return $w; } sub myFrame($$$$) { my ($w, $ix, $iy, $placement) = @_; my $fr = $w->Frame(); return &placement($fr, $placement); } sub myLabel($$$) { my ($w, $text, $placement) = @_; my $lbl = $w->Label(-text => $text); return &placement($lbl, $placement); } sub myEntry($$$) { my ($w, $tvar, $placement) = @_; my $e = $w->Entry(-textvariable => $tvar); return &placement($e, $placement); } sub myCheckbutton($$$$$) { my ($w, $v, $b0, $b1, $placement) = @_; my $c = $w->Checkbutton(-textvar => $v, -offvalue => $b0, -onvalue + => $b1); return &placement($c, $placement); } sub setup { my $parent = shift; my %args = @_; my $top = myFrame $parent, 5, 5, 'p,top,x,1'; my $list = myFrame $parent, 5, 5, 'p,top,x,1'; myLabel $top, 'Enter some phone numbers.', 'p,top,none,0'; myLabel $top, 'Check numbers to activate them.', 'p,top,none,0'; myLabel $list, 'Enabled', 'g,0,0'; myLabel $list, 'Phone number', 'g,1,0'; myLabel $list, 'Description', 'g,2,0'; for ( 1..5 ) { my $textvar = \$args{-numbers}->[$_]{enable}; myCheckbutton $list, $textvar, '', 'ACTIVE', "g,0,$_"; myEntry $list, \$args{-numbers}->[$_]{number}, "g,1,$_"; myEntry $list, \$args{-numbers}->[$_]{description}, "g,2,$_"; } MainLoop } 1;
And here's the main program (with a fix to the line "PhoneList::setup($mw, -numbers => $phoneNumbers;);", which had an extraneous ';' before the final parenthesis):
#!/usr/bin/perl -w use strict; use warnings; use lib "."; use PhoneList; my $mw = MainWindow->new; $mw->configure(-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);

In reply to Re: Name spaces and Perl::Tk coding style by liverpole
in thread Name spaces and Perl::Tk coding style by TGI

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found