Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Quickly detecting variable writes by Tanktalus
in thread Quickly detecting variable writes by sfink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-18 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found