How to watch particular hash key for changing its value?

perl print hash keys and values
perl hash
perl sort multidimensional hash by value
perl hash keys
perl hash key exists
sort hash perl
perl get all keys from hash
perl hash foreach

I have a hash, e.g. $hash->{'foo'}{'bar'}.

I want to call Carp::cluck in any place where value of bar key changed.

How to do that ? Is there any ready module on CPAN that can do that trick ?


my $hash = { foo => { bar => 1 } };
Internals::SvREADONLY( $hash->{foo}{bar}, 1 );
$hash->{foo}{bar} = 2;

produces

Modification of a read-only value attempted at -e line 4.

But that's a fatal error, and it doesn't include a trace (unless Carp::Always is used).

I would recommend adding set magic to the scalar.

use Carp            qw( cluck );
use Variable::Magic qw( wizard cast );

my $wizard = wizard(
   set => sub {
      cluck("Warning: Modification of a read-only value attempted");
   },
);

my $hash = { foo => { bar => 1 } };
cast( $hash->{foo}{bar}, $wizard );
$hash->{foo}{bar} = 2;

produces

Warning: Modification of a read-only value attempted at -e line 6.
        main::__ANON__(SCALAR(0x4200c90), undef) called at -e line 12
        eval {...} called at -e line 12

The same can be accomplished with tie, but it would be more expensive. (Tied variables are built upon magic.)

Class: Hash (Ruby 2.6.3), Two objects refer to the same hash key when their hash value is identical and the two objects are eql? to each other. A user-defined Creates a new hash populated with the given objects. If not found, returns the default value (see Hash::new for details). Returns nil if no changes were made, otherwise returns the hash. The replace(K key, V value) method of Map interface, implemented by HashMap class is used to replace the value of the specified key only if the key is previously mapped with some value. Syntax: public V replace(K key, V value) Parameters: This method accepts two parameters: key: which is the key of the element whose value has to be replaced. value: which is the new value which has to be mapped with the provided key. Return Value: This method returns the previous value associated with the


Tie::Trace almost gets you there.

use Tie::Trace 'watch';
my $hash = { foo => { bar => "original value" } };
watch $hash->{foo}{bar};
sub f1 { f2() }
sub f2 { f3() }
sub f3 { $hash->{foo}{bar} = "new value" }
f1();

Output:

'new value' at watch.pl line 6

You can make the output produce a full stack trace by importing Carp::Always or by monkey patching the Tie::Trace::_carpit function or with a $SIG{__WARN__} handler like

$SIG{__WARN__} = sub {
    if (caller(0) eq 'Tie::Trace') {
        # warning is from Tie::Trace
        Carp::cluck(@_);
    } else {
        CORE::warn(@_);
    }
};
...

keys, So long as a given hash is unmodified you may rely on keys, values and each to of Perl's hash algorithm and the hash traversal order are subject to change in the internal iterator of the HASH or ARRAY (see each) before yielding the keys. The Value mapped to Key 4 is:DD The Value mapped to Key 5 is:null Note: In the above program the key 5 is not mapped to any value so the get() method returned null, However you must not use this method for checking existence of a Key in HashMap because a return value of null does not necessarily indicate that the map contains no mapping for the


I have done a "simple" function to do this, it doesn't work with hashes which contains arrays:

use v5.28;
use Storable qw(dclone);

my $ori = {
    'hola' => {
        'hola' => 'adios',
        'adios' => 'hola'
    },
    'hey' => 'you'
};
my $copy = dclone($ori);
$ori->{'hola'}{'adios'} = {'good', 'bye', 'hello', 'year'};
compare($ori, $copy, sub { say $_[0]; });

sub compare {
    my $original = shift;
    my $copy = shift;
    my $func = shift;
    for my $el (keys %{$original}) {
        if (ref $original->{$el} eq ref {}) {
                compare($original->{$el}, ref $copy->{$el} 
                        eq ref {} ? $copy->{$el}:{} , $func);
        } else {
            unless ($copy->{$el} eq $original->{$el}) {
                &$func($original->{$el}, {}, $func);
            }
        }
    }
}
~             

Perl Hash, It helps differentiate between keys and values, and makes the code more elegant . When you see the => operator, you� Scenario. HashMap can be used to store key-value pairs. But sometimes you may want to store multiple values for the same key. For example: For Key A, you want to store - Apple, Aeroplane


Hash table, In computing, a hash table (hash map) is a data structure that implements an associative array For the South Park episode, see Rehash (South Park). The idea of hashing is to distribute the entries (key/value pairs) across an array of buckets. Given a key, the algorithm computes an index that suggests where the entry� However, BidiMap imposes a 1:1 relationship between its keys and values. If we try to put a key-value pair for which the value already exists in the Map, it removes the old entry. In other words, it updates the key against the value. Also, it requires a larger amount of memory for keeping the reverse map.


hash function, See .set for further details and how key-value vectors of unequal length are When passes to functions, those functions can change the value of the hash. It basically removes the values for any particular key in the Map. Syntax: Hash_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map. Return Value: The method returns the value that was previously mapped to the specified key if the key exists else the method returns NULL.


HashMap (Java Platform SE 7 ), (The HashMap class is roughly equivalent to Hashtable, except that it is This class makes no guarantees as to the order of the map; in particular, it does not one or more mappings; merely changing the value associated with a key that an instance The iterators returned by all of this class's "collection view methods" are� Using a key, references a value from hash. If the key is not found, returns a default value. 3: hash.[key] = value. Associates the value given by value with the key given by key. 4: hash.clear. Removes all key-value pairs from hash. 5: hash.default(key = nil) Returns the default value for hash, nil if not set by default=.