my $x = "foo"; my $y = "foo foo foo bar"; my $c = () = $y =~ /$x/g; # $c is now 3
您还可以捕获到数组并计数数组。相同的原理,不同的技术:
my @c = $y =~ /$x/g; my $count = @c;