Hi, Ted Zlatanov wrote: > I have a Perl 5 module called Every which provides, simply, > > every(5); # true on invocation 5, 10, etc. > every(seconds => 5); # true at 5, 10, etc. seconds > > It's really nice in endless loops, logging, etc. I'd like to translate > it to Perl 6 (using Rakudo specifically). > > Its current implementation requires Devel::Opcode because it needs to > know what location is getting called again and again. > > Generally for Perl 6, is there a simple way to make Every obsolete by > using built-ins?
State variables will probably make that a lot easier. Something like (do { state $x; $x++}) %% 5 > I'd love that but haven't seen anything in the > language that would help. I want to avoid closures, they are expensive > (the current implementation uses one hash key per location). Maybe I > could use a continuation (I don't know the syntax for that)? > > Specifically for Rakudo, if I have to implement it, can I avoid > Devel::Opcode? Are continuations expensive? Currently there is no Devel::Opcode in Rakudo (and I doubt that it will work when being called through Blizkost), and everything is expensive :/ Once macros and 'state' are implemented, I think it will be easy to turn every(5) into (do { state $x; $x++}) %% 5 Until then the best you can do for now is probably to use callframe(1).file and .line to fingerprint the source location. You'll only get line-level resolution, but that should be enough for a start. Cheers, Moritz