in reply to two regexes in one function call

Why are you using substitutions in a "count" script?
And then replacing whatever it was with itself?

Best to not modify your input and risk changing things while you're trying to count.
Note: See Perl Idioms Explained - my $count = () = /.../g
use strict; use warnings; sub countIt { my $data = shift; my $lines =()= $data =~ /\n/g; $lines++ unless $data =~ /\n$/; # Last line may not have \n on it my $words =()= $data =~ /\w+/g; my $length = length($data); return ($lines, $words, $length); } print join ',', countIt(" Blah blah blah\nStuff-by:somebody");
#Prints 2,6,34

Replies are listed 'Best First'.
Re^2: two regexes in one function call
by bobr (Monk) on Jul 28, 2009 at 16:23 UTC
    Agree, this is definitely more safe and probably much faster. Thanks for pointing this out, the idioms are very interesing.

    What I was rather surprised was the output of my short-minded solution, where all expressions alone worked, but combined together did not.

    I just recently tried other perl for windows, the strawberry, and it seems to produce very same result. Strange.