in reply to RFC: Regexp::AllMatches

In Perl 6 that's the :ex or :exhaustive modifier, maybe you could consider calling your module Regexp::Exhaustive or Regexp::Match::Exhaustive or something.

The API looks nice, I just wonder why you use the parens in while (my ($match) = $matcher->next) - does ->next() return a list? if yes, why?

Update: while rereading S05 I also found the :overlap modifier - please check if you are implementing :exhaustive or :overlap - I think it's :exhaustive, but I'm not sure.

Replies are listed 'Best First'.
Re^2: RFC: Regexp::AllMatches
by lodin (Hermit) on Aug 06, 2007 at 17:30 UTC

    I'll definately consider ::Exhaustive. Thanks!

    does ->next() return a list? if yes, why?

    There are different opinions and tastes regarding this. I prefer to design iterators so that the next method returns undef in scalar context and the empty list in list context when it's exhausted. A successful match may be "" or "0", and that would force me to write

    while (defined(my $match = $matcher->next)) { ... }
    to not leave the loop prematurely.

    lodin

      There is another way to handle the return value in a loop. Instead of returning a string, return an object that overloads the boolean, numerical, and string operators.

      An example of this in practice is the IO::Prompt module on cpan. Here is an excerpt of the source:
      package IO::Prompt::ReturnVal; use overload q{bool} => sub { $_ = $_[0]{value} if $_[0]{set_val}; $_[0]{handled} = 1; $_[0]{success}; }, q{""} => sub { $_[0]{handled} = 1; "$_[0]{value}"; }, q{0+} => sub { $_[0]{handled} = 1; 0 + $_[0]{value}; }, fallback => 1, ; sub DESTROY { $_ = $_[0]{value} unless $_[0]{handled}; }
      As you can see, it also provides mechanisms for setting $_.

      Anyway, just wanted to throw out another option. Please note that the IO::Prompt module only runs on unix based systems currently AFAIK.

      - Miller