Tied hash: Differentiating between assignment of single value andentire hash

Discussion in 'Perl Misc' started by bernd, Apr 24, 2012.

  1. bernd

    bernd Guest

    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

    An example should illustrate the process:

    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?



    P.S.: Yes, I know about "use strict". :->>
    bernd, Apr 24, 2012
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pietro

    differentiating between requests

    Pietro, Aug 31, 2005, in forum: ASP .Net
    Sep 1, 2005
  2. Exceedingly
    Feb 21, 2006
  3. Thomas Reat

    sharing a (tied) hash between processes

    Thomas Reat, Jan 1, 2004, in forum: Perl Misc
    Jan 3, 2004
  4. Replies:
    Tomi Häsä
    Jan 10, 2005
  5. cybose
    Dec 18, 2007

Share This Page