in reply to how watch a variable

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?

Replies are listed 'Best First'.
Re^2: how watch a variable
by karlgoethebier (Abbot) on Dec 10, 2023 at 20:00 UTC
    ”…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»

Re^2: how watch a variable
by Anonymous Monk on Dec 10, 2023 at 16:14 UTC

    > Mostly because it's supposed to be transparent

    Or completely opaque.