Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Tied hash: Differentiating between assignment of single value andentire hash

Reply
Thread Tools

Tied hash: Differentiating between assignment of single value andentire hash

 
 
bernd
Guest
Posts: n/a
 
      04-24-2012
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". :->>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Differentiating between two browser clients for JSP application Exceedingly Java 2 02-21-2006 10:40 PM
differentiating between requests Pietro ASP .Net 8 09-01-2005 02:28 PM
copying values from a hash into CGI.pm via tied hash reference ioneabu@yahoo.com Perl Misc 14 01-10-2005 01:22 PM
sharing a (tied) hash between processes Thomas Reat Perl Misc 4 01-03-2004 09:42 PM



Advertisments