#!/usr/local/bin/perl
use strict;
use warnings;
use Benchmark qw(:all);
cmpthese(1_000_000, {
"Non-global" => sub { "Perl is cool" =~ m/Perl/ },
"Global" => sub { "Perl is cool" =~ m/Perl/g }
}
);
####
Rate Global Non-global
Global 1351351/s -- -9%
Non-global 1492537/s 10% --
##
##
s/
( # Match and backreference to either
(?:
Perl| # one or
perl| # another way
PERL| # to spell
[Pp][Ee][Rr][Ll] # perl
)| # or
(?:
Java| # one or
java| # another way
JAVA| # to spell
[Jj][Aa][Vv][Aa] # java
)
)
/ # and replace it with
lc($1) eq "perl" ?
"$1 looks like a nice language to me" # this when perl is found
:lc($1) eq "java"?
"I don't know $1 very well" # or this when java is found
:"I do not wish to consider $1" # or this when something else is found.
/ex;