Here is my sample code reworked to use a tied array (not all the necessary features of a tied array are implemented here FETCHSIZE and STORESIZE may not be very useful as written-- I just wanted to try the basics of tie and get this example to work a little less clumsily). Thanks to
ar0n for suggesting tie in the first place, to
ar0n and
broquaint for helping me fix a bug in this code. Note that you can also export the $Concat variable in the package to avoid having to package qualify it in Main, but I think I prefer it how it is.
#!/usr/bin/perl -w
use strict;
use Tk;
my @bit;
tie @bit, 'Bitters';
my $mw = Tk::MainWindow->new();
my $entry =
$mw->Label( -textvariable => \$Bitters::Concat,
)->
pack( -expand => 'y',
);
my $foo_button =
$mw->Button( -text => "Bit One",
-command => sub { flip( $bit[1] );}
)->pack();
my $bar_button =
$mw->Button( -text => "Bit Zero",
-command => sub { flip( $bit[0] );}
)->pack();
print "MainLoop\n";
MainLoop();
sub flip
{
$_[0] = ( $_[0] eq 'x' ) ? 'y' : 'x';
}
{ package Bitters;
use vars qw( $Concat );
sub TIEARRAY {
my $class = shift;
my $self = { ARRAY => ['x','x'] };
bless $self, $class;
$self->update_concatenation();
return $self;
}
sub FETCH
{
my($self,$idx) = @_;
return $self->{ARRAY}[$idx];
}
sub FETCHSIZE
{
my $self = shift;
return $self->{ARRAY}+0;
}
sub STORESIZE
{
print "STORESIZE ignored\n";
}
sub STORE
{
my($self, $idx, $value) = @_;
print "[STORE $value at $idx]\n";
$self->{ARRAY}[$idx] = $value;
$self->update_concatenation();
return $self->{ARRAY}[$idx];
}
sub update_concatenation
{
my $self = shift;
$Concat = $self->{ARRAY}[0] . $self->{ARRAY}[1];
print "$Concat\n";
}
}#end_package Bitters;
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.