Piers Cawley writes: : Hmm... making up some syntax on the fly. I sort of like the idea of : being able to do : : class File; : sub foreach ($file, &block) is Control { : # 'is Control' declares this as a control sub, which, amongst : # other things 'hides' itself from caller. (We can currently : # do something like this already using Hooks::LexWrap type : # tricks.
Maybe, but we'll need more explicit parsing control for other things, so this may fall out of that. : open my $fh, $file or die $!; POST { close $fh } More like: my $fh = open $file or die; : while (<FILE>) { : my @ret = wantarray ?? list &block() :: (scalar &block()); : given $! { : when c::RETURN { return wantarray ?? @ret :: @ret[0] } : } : } That "given $!" would have to be a CATCH, or the code would never be executed on a control exception. : This is, of course, dependent on $! not being set to a RETURN control : 'exception' in the case where we just fall off the end of the block. I'd say that's correct. : It's also dependent on being able to get continuations from caller : (which would be *so* cool Hmm, might not need to go that far. : > allow this: : > : > File.foreach('/usr/dict/words') { print } : : Sounds plausible to me. We're not using Ruby syntax here. Any closure is a real argument with a real formal argument name, and is called via ordinary &block(...) syntax, not yield. : > or would the prototype be (&file, &block)? : : I prefer the ($file, &block) prototype. I don't see why it would ever be &file. It's just a string. : > And would this: : > : > my $caller = caller; : > File.foreach('/usr/dict/words') { : > print $caller eq caller ? "ok" : "not ok" : > } : > : > be ok or not ok? It has to be ok if mywhile is going to emulate a : > while loop. : : In theory there's nothing to stop you writing it so that that is the : case. I'd like it to be as simple as adding an attribute to the : function declaration (and if it isn't that simple out of the box, it : will almost certainly be, if not trivial, at least possible to write : something to *make* it that simple...) Precisely. Larry