4

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 ?

Paul Serikov
  • 2,550
  • 2
  • 21
  • 36

3 Answers3

6
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.)

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Another reason the set magic may be preferred in this case is that restricted hashes (what you get when you set a hash readonly) are a little more than just read-only; it also errors if you access a nonexistent key, even as an rvalue. (See [Const::Fast caveats](https://metacpan.org/pod/Const::Fast#CAVEATS) for example) – Grinnz Feb 13 '19 at 17:40
3

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(@_);
    }
};
...
mob
  • 117,087
  • 18
  • 149
  • 283
0

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);
            }
        }
    }
}
~             
sergiotarxz
  • 520
  • 2
  • 14