-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

* Timothy Kohl <tkohl at it.bu.edu> [2003-08-07 08:17]:
> I am trying to set up an associative array 
> where the values are method calls (specifically 
> widget creation calls) for Tk.

You want a dispatch table, with the keys pointing to subroutine
references.

  %Valid = (
      hostname => sub {
          my $obj = shift;
          $obj->Entry(width         => 30,
                      background    => 'darkgreen',
                      -textvariable => \$curval{'hostname'},
          );
      },
      # and others
  );

You would then invoke them as something like:

  my $sub = $Valid{'hostname'};
  $prop{'hostname'} = $sub->($rightframe)->pack(side => 'right');

This creates references to anonymous subroutines as the values of %Valid
that are expecting a Tk object (a Frame, in this case).  The subrefs
take the object as an argument (that's the $sub->($rightframe) syntax,
which will need to be spelled $&sub($rightframe) if you are using an
pre-5.005 Perl).

In the 'hostname' example, this returns whatever $rightframe->Entry()
returns, which you can then call pack on (presumably).

You could even make it a little more extensible like this:

  %Valid = (
      hostname => sub {
          my $obj = shift;
          $obj->Entry(width         => 30,
                      background    => 'darkgreen',
                      -textvariable => \$curval{'hostname'},
                      @_,
          );
      },
      # and others
  );
  
The addition of @_ in the call to $obj->Entry means you can pass extra
arguments to it:

  $prop{'hostname'} =
    $sub->($rightframe, other => 'arg')->pack(side => 'right');

And other => 'arg' will be passed to Entry as well (sorry about the
useless example, I don't have Tk installed to find a real argument).

(darren)

- -- 
The higher we soar the smaller we appear to those who cannot fly.
    -- Friedrich Nietzsche
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: This message is digitally signed and can be verified for authenticity.

iD8DBQE/Mk2ozsinjrVhZaoRAgC8AJ9ltTbz6yN4kob/Co+2nmDMNmGK+wCfaYIv
kxof0vFBrltFF67PWLxg8zI=
=6U5a
-----END PGP SIGNATURE-----
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to