> Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm > Date: Tue, 10 Dec 2002 21:07:34 -0500 > From: Adam Turoff <[EMAIL PROTECTED]> > Cc: [EMAIL PROTECTED] > Content-Disposition: inline > X-SMTPD: qpsmtpd/0.20, http://develooper.com/code/qpsmtpd/ > > On Tue, Dec 10, 2002 at 03:38:58PM -0800, Rich Morin wrote: > > On occasion, I have found it useful to cobble up a "little language" > > that allows me to generate a list of items, using a wild-card or some > > other syntax, as: > > > > foo[0-9][0-9] yields foo00, foo01, ... > > > > I'm wondering whether Perl should have a similar capability, using REs. > > Dominus has an example of an 'odometer' written using closures. If > you want to specify a simple sequence like this using a regex, then > you need to parse the regex (but in reverse). > > Alternatively, you could write a generic odometer generator generator > that takes a series of scalars and lists: > > my $generator = make_generator('foo', [0..9], [0..9]); > > while ($_ = $generator->()) { > ## progressively generate foo00, foo01, ... > } > > Z.
It only differentiates between arrays an non-, but it would be trivial to add other semantics, if you figure out what they actually are. sub make_generator(*@pats) { return unless @pats; given shift @pats -> $pattern { when Array { for @$pattern -> $item { my $generator = make_generator(@pats); yield $item ~ $_ for <$generator>; } } otherwise { my $generator = make_generator(@pats); yield $pattern ~ $_ for <$generator>; } } } You'd use it just like I did in the sub, as an iterator. This would have been dastardly complex without corouties. :-) Luke