#!/usr/bin/perl use strict; use warnings; use 5.012; # 901284 my $count=0; my $foo = "X x y y x y Z"; say "First, with a simple substitution '/g'"; my $simplecount = $foo =~ s/x/o/g; say " \$foo: $foo; \n \$simplecount: $simplecount (i.e., 'x's changed)"; $foo =~ s/o/x/g; say "\$foo restored: $foo \n"; say 'Now, with given/when and =~ /x/g'; given( $foo ) { when ( $foo =~ /x/g ) { say "\$foo contains an x"; $count++; say "\$foo: $foo; \n \$count: $count;"; continue } when ( /y/ ) { say "\n \$foo contains a y"; } default { say "$foo does not contain a y"; } } say "\n Final \$count of 'x' in given/when matching 'x': $count"; say "\n $foo";