Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #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

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
    [Discipulus]: good morning community!
    [Corion]: Good morning Discipulus!

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2018-06-19 07:12 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (111 votes). Check out past polls.