Tied hash: Differentiating between assignment of single value andentire hash

B

bernd

Hello folks,

I want to use Perl's tie-mechanism to configure an object
"automatically" by changing the/a hash associated with the object (in
this particular project the object will be a Tk-widget, but this is
not relevant since I am looking for a generic solution to the problem
described).

An example should illustrate the process:

#!/usr/bin/perl
package AssignTest;

use Tie::Hash;

use base qw( Tie::StdHash );

sub TIEHASH { $class = shift; $hash = {}; bless $hash, $class }

sub STORE { $self = shift; ( $key, $value ) = @_; print "$value" }

sub CLEAR { $self = shift; print "CLEAR was called!\n"; %{$_[0]} =
() }

package main;

$myobject = tie %tiedhash, 'AssignTest';

%tiedhash = ( a => "Entire ", b => "hash ", c => "assigned!\n");

print "\n-----------\n";

$tiedhash{a} = "Single ";

$tiedhash{b} = "values ";

$tiedhash{c} = "assigned!\n";


On running this script I get the following output:

CLEAR was called!
Entire hash assigned!

-----------
Single values assigned!

From the code and the output it is clear that, if the assignment takes
place in one step by assigning to the entire hash, first CLEAR is
called and then, for each key-value pair on the RHS, STORE is invoked
to actually insert the data into the hash. When one assigns a single
value to a key, STORE is called for each such an assignment, but
CLEAR, of course, not.

If the key-value-pairs used would be meaningful parameters, I could
easily configure $myobject (and trigger possible subsequent events) by
just assigning to the hash.

In my use case the additional requirement is that the object's
internal code is capable of recognizing whether the assignment was
done by assigning to the entire hash or to a single key (in the Tk-
application mentioned above EACH assignment, regardless which of the
both statement types are used, should trigger the appearance of the
freshly configured widget on the screen).

From the invocation of the CLEAR-method the object "knows" when an
assignment to the entire hash was started, but how can it detect when
this assignment is complete ("complete" in the sense that all calls to
STORE associated with the hash assignment are done)? Because what
follows upon the CLEAR is this sequence of calls to STORE. One or more
single value assignments following the assignment to the entire hash
will trigger STORE as well, and, at least my limited insight into the
process gives me no clue on how one could differentiate between calls
to STORE triggered by the assignment to the entire hash and single
value assignments resp.

If the object's code would be aware of the number of key-value-pairs
on the RHS of the hash assignment, then this job would be easily done
(by letting STORE count how often it was called after a CLEAR, but
AFAIK, the object is not aware of this figure).

Any ideas?

Greetz


Bernd

P.S.: Yes, I know about "use strict". :->>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top