Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: RFC: Regexp::AllMatches

by lodin (Hermit)
on Aug 06, 2007 at 17:30 UTC ( [id://630862]=note: print w/replies, xml ) Need Help??


in reply to Re: RFC: Regexp::AllMatches
in thread RFC: Regexp::AllMatches

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

Replies are listed 'Best First'.
Re^3: RFC: Regexp::AllMatches
by wind (Priest) on Aug 07, 2007 at 00:25 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://630862]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found