Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Quickly detecting variable writes

by Tanktalus (Canon)
on Jan 05, 2007 at 04:24 UTC ( [id://593066]=note: print w/replies, xml ) Need Help??


in reply to Quickly detecting variable writes

Your example may be a bit more abstract than you intended, but, in case it is not, why not just test the array?

foreach (@kabluther) { #... } if (@kabluther) { # the "..." above was executed # i.e., your $x was modified $_->change_in_x() for @listeners; # sounds like this is something li +ke what you want to do. }
Note that this test is executed once, and only once, each time through this function. The test should be O(1) - that is, independent of the actual size of the list. Even if perl didn't keep track of the actual size of the list separate from the list itself (i.e., not a calculation), which it does, perl would be able to optimise this by noting we're in boolean context and knowing to just check if the list is empty or not, and thereby only count the first element in the list.

Note that this does not (quite) help if you're doing the $x modification inside an "if", e.g.:

foreach (@kabluther) { $x += $_->getSkookiness() if $_; # ignore undef arguments # or if ref $_; # only count ref's # or if ref $_ and $_->can('getSkookiness'); # only count objects t +hat have this method }
But, even then, based on your original question ('usually empty'), and your fuzzy operator ('can report true when there is no actual change, but cannot report false when there is a change'), I think this may be close enough.

For more accuracy, I would just create a lexical variable to track changes.

sub onTick { our $x; my $changed; # Initialize and associate $x with some external value foreach (@kabluther) { do { # these two lines must be a block - do together, or not at +all. $x += $_->getSkookiness(); $changed++; } # if clause can go here I think, if needed } if ($changed) { $_->ticked() for @listeners; } return $x; }
A bit more overhead when doing something, but about the same overhead when doing nothing, and will get it right all the time, and no funky XS. That said, it requires a change to the loop. Probably not a huge change, but a change nonetheless.

Replies are listed 'Best First'.
Re^2: Quickly detecting variable writes
by sfink (Deacon) on Jan 05, 2007 at 04:42 UTC
    The purpose of this is to provide a nice interface to programmers so that they don't have to track whether the watchers need to be notified or not. Here's some actual code in the current version of my system:
    onTime { our $active; our $pos : Attr('position'); if ($active) { $pos += [ 1, 1 ]; } }
    (I know there's a missing 'sub' before 'onTime', but that's intentional to match our actual setup. Just pretend there is a 'sub' there.)

    I would like the developers to be able to write exactly the above code, but only have the 'position' watchers notified if $pos was written to. I can do whatever setup I want in either the attribute handler or code that is inserted before the entire body, and whatever cleanup or change notification is needed at the end of the body or after the subroutine returns.

    In the current system, it just unconditionally assumes that any variable that could change, did. That means there is a cost for using the Attr(...) syntax, and I want it to at least feel "free" to the developers.

    Sorry if the question was unclear. It's hard to figure out how much to explain so that people can understand the question (and maybe detect if I'm asking the wrong question), and how much to leave out so people don't have to wade through irrelevant detail.

      it looks like the += operator is already overloaded. can you hook into that, and an overloaded = operator, and etc?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://593066]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-19 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found