xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Is there a module to watch an variable and print change when something change. I mean not just value stored in it. See Below:

my $c = '123'; say OK if $c >120; #here perl changes $c, from PV to PVIV.
Since perl does a lot of things for me, sometimes I'd like to know when and how then I could decide if I should prevent him to do. Now I have to print Dump variable using Devel::Peek It really annoyed. and I take a look Tie::Watch, it seems also not for me. Any suggestions? Thanks.





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: how watch a variable
by SankoR (Prior) on Dec 10, 2023 at 11:38 UTC

      Can you please clarify? I have quickly concocted this:

      use strict; use warnings; use feature 'say'; use Variable::Magic qw( wizard cast ); use B::Flags; my $x = '1'; cast $x, wizard( # get => sub { say 'getting...' }, ); say B::svref_2object( \$x )-> flagspv; say 1 if $x > 2.2; say B::svref_2object( \$x )-> flagspv; say 1 if $x > 2; say B::svref_2object( \$x )-> flagspv;

      and output is as, I think, expected:

      RMG,POK,OVERLOAD,pPOK,IsCOW RMG,NOK,POK,OVERLOAD,pNOK,pPOK,IsCOW RMG,IOK,NOK,POK,OVERLOAD,pIOK,pNOK,pPOK,IsCOW

      but with getter definition un-commented:

      GMG,POK,OVERLOAD,pPOK,IsCOW getting... GMG,POK,OVERLOAD,pPOK,IsCOW getting... GMG,IOK,POK,OVERLOAD,pIOK,pPOK,IsCOW

      I'm not OP, just fooling around. My idea was:

      use strict; use warnings; use feature 'say'; use Variable::Magic qw( wizard cast ); use B::Flags; package no_int_please { DESTROY { say 'I\'ve been evaluated as integer, HOW DARE YOU?!' if B::svref_2object( @_ )-> flagspv =~ /IOK/ } } my $x = '1'; cast $x, wizard( get => sub { bless $_[0], 'no_int_please' }, ); say "value is $x, let's check it"; say B::svref_2object( \$x )-> flagspv =~ /IOK/ ? 'i' : 's'; say $x > 2 ? '>' : '<'; say B::svref_2object( \$x )-> flagspv =~ /IOK/ ? 'i' : 's';

      which therefore won't work if comparison is to NV, but why?

        If you want to prevent numification, you can use overload to provide a numifier that dies.

Re: how watch a variable
by choroba (Cardinal) on Dec 10, 2023 at 20:45 UTC
    Devel::Peek::Dump is dumb as it always prints but getting the returned string back is not that easy. It's possible, though - but I'm not sure it's always helpful:
    #! /usr/bin/perl use warnings; use strict; use feature qw{ say }; use experimental qw( signatures ); use Devel::Peek qw{ Dump }; sub _dump { my @failures; open my $olderr, '>&', \*STDERR or push @failures, $!; close *STDERR; open *STDERR, '>', \ my $err or push @failures, $!; eval { Dump($_[0]); 1 } or push @failures, $@; open *STDERR, '>&', $olderr or push @failures, $!; die @failures if @failures; my ($type) = $err =~ /FLAGS = \((.*)\)/; say $type; return $type } { my $c = 123; my $before = _dump($c); die unless $c < 130; say "II:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = 123; my $before = _dump($c); die unless $c lt 130; say "IP:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = 123; my $before = _dump($c); die unless $c < 130.01; say "IN:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = 123.01; my $before = _dump($c); die unless $c < 130; say "NI:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = 123.01; my $before = _dump($c); die unless $c lt '130'; say "NP:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = 123.01; my $before = _dump($c); die unless $c < 130.01; say "NN:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = '123'; my $before = _dump($c); die unless $c lt 130; say "PP:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = '123'; my $before = _dump($c); die unless $c < 130; say "PI:", $before eq _dump($c) ? 'Same' : 'Changed'; } { my $c = '123'; my $before = _dump($c); die unless $c < 130.01; say "PN:", $before eq _dump($c) ? 'Same' : 'Changed'; }

    Output:

    IOK,pIOK IOK,pIOK II:Same IOK,pIOK IOK,POK,pIOK,pPOK IP:Changed IOK,pIOK IOK,NOK,pIOK,pNOK IN:Changed NOK,pNOK NOK,pIOK,pNOK NI:Changed NOK,pNOK NOK,pNOK NP:Same NOK,pNOK NOK,pNOK NN:Same POK,IsCOW,pPOK POK,IsCOW,pPOK PP:Same POK,IsCOW,pPOK IOK,POK,IsCOW,pIOK,pPOK PI:Changed POK,IsCOW,pPOK NOK,POK,IsCOW,pNOK,pPOK PN:Changed

    Note the "NP:Same" lines. Also, changing 130.01 to 130.0 makes a difference. What's even worse, changing the Perl version makes a difference. With 130.0, 5.26.1 gives "NN:Changed", but blead gives "NN:Same" (it doesn't add pIOK to NOK,pNOK).

    I haven't succeeded in hiding the call to Dump in a tied or overloaded object.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Yes it just occurred to me that overload could help watching the result of an operation without XS

      nomethod could be used to check them all.

      > haven't succeeded in hiding the call to Dump in a tied or overloaded object.

      One anomonk in this thread tried to use B::Flags instead of Devel::Peek for this

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery


      PS: sorry for the duplicates, it took about 5 minutes before the monastery confirmed my posting, all prior attempts showed as duplicates.

Re: how watch a variable
by LanX (Saint) on Dec 10, 2023 at 13:42 UTC
    For clarification of the question:

    I had to read multiple times, because my first instinct was using a Tie::Scalar mechanism and you ruled out Tie::Watch .

    But tie has no callback to report changing of the internal representation. Mostly because it's supposed to be transparent.

    The only way I see in pure Perl without XS is using the debugger with a complicated watch expression.

    This will come with a considerable speed penalty

    It might help to know why you need this and which problem you are trying to solve.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    update

    It might be worth checking if $c >120 triggers a tie FETCH, like that you could trace the internal casting and raise an alarm after a change happened.

    Update

    SO: How are scalars stored 'under the hood' in perl?

      ”…why…”

      I could imagine that it is a useful feature in certain situations to avoid "losing the overview" - in the broadest sense of the word. That's probably why - among other things - it's already built into other languages:

      karl> (def gizmo (atom[])) #'karl/gizmo karl> (add-watch gizmo :dog (fn [key gizmo old new] (println key old new))) #<Atom@5f5a940a: []> karl> (swap! gizmo conj true) :dog [] [true] [true] karl> (if (seq? (seq @gizmo)) (swap! gizmo pop)) :dog [true] [] [] karl> (if (seq? (seq @gizmo)) (swap! gizmo pop)) nil

      «The Crux of the Biscuit is the Apostrophe»

      > Mostly because it's supposed to be transparent

      Or completely opaque.

Re: how watch a variable
by Polyglot (Chaplain) on Dec 10, 2023 at 22:54 UTC
    There may be more professional solutions, but if I'm only watching one variable, or just a few variables, and I don't wish to interrupt the process, what I have sometimes done is to print that variable to a file, in append mode. Then I check that file. With Linux, it's quite easy...just use tail -f filename.txt to see a running display of what is being added to the file...just like a log file.

    Don't forget, of course, to output to the file in the appropriate encoding if you are not working with English.

    Basically, I would just do something like this (untested):

    my $watchlog = '/tmp/watchlog.txt'; printLog("Watch file: $watchlog\n"); #Whatever and wherever you wa +nt to print sub printLog { my $info = shift @_; open (LOG, '>>:encoding(utf8)', $watchlog) or die "Cannot open the + logfile. $!\n"; print LOG $info; close LOG; } #END SUB printLog

    Blessings,

    ~Polyglot~