Skip to content

Instantly share code, notes, and snippets.

@azumakuniyuki
Created May 8, 2022 05:54
Show Gist options
  • Save azumakuniyuki/db0c1e20e1459327dd83bafc69fda8fa to your computer and use it in GitHub Desktop.
Save azumakuniyuki/db0c1e20e1459327dd83bafc69fda8fa to your computer and use it in GitHub Desktop.
${^MATCH} variable with /p modifier
#!/usr/bin/env perl
# Captured $1 vs. ${^MATCH}
use strict;
use warnings;
use Benchmark ':all';
use Test::More 'no_plan';
my $q = 'neko-nyaan-cat';
sub cv {
my $v = shift; $v =~ /(n[a-z]{4})/;
return $1;
}
sub mv {
my $v = shift; $v =~ /n[a-z]{4}/p;
return ${^MATCH};
}
is cv($q), 'nyaan';
is mv($q), 'nyaan';
printf("Running with Perl %s on %s\n%s\n", $^V, $^O, '-' x 80);
cmpthese(6e6, {
'$1' => sub { cv($q) },
'${^MATCH}' => sub { mv($q) },
}
);
__END__
Running with Perl v5.30.0 on darwin
--------------------------------------------------------------------------------
Rate $1 ${^MATCH}
$1 1916933/s -- -9%
${^MATCH} 2097902/s 9% --
Running with Perl v5.32.0 on darwin
--------------------------------------------------------------------------------
Rate $1 ${^MATCH}
$1 1929260/s -- -6%
${^MATCH} 2054795/s 7% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment