in reply to Doing "it" only once

I agree that this kind of functionality would be way cool. However, it sounds like you want to do on-the-fly rewriting of the optree. I'm not certain that this is possible in the general case, given optimizations that would probably benefit you more than your optimization is going to.

You could do something with overload instead of subrefs. I'm not sure how much faster this will be, but here goes:

package Always::False; use overload 'bool' => sub { return; }; sub new { return bless \(my $foo), shift } package Check::Once; use overload 'bool' => 'boolify'; sub new { my $class = shift; my ($val) = @_; return bless \$val, $class; } sub boolify { if (${$_[0]} eq $_) { $_[0] = Always::False->new; return 1; } else { return; } } package main; my $once_only = Check::Once->new('foo'); while (<FILE>) { next if $once_only; # Other stuff here. }

Yes, there are improvements that could be made, but it's just proof-of-concept.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?