Win8 Strawberry 5.8.9.5 (32) Fri 11/06/2020 18:48:09
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings
use Data::Dump qw(pp);
my $linkheader = 'axbyczSelfaxbycz';
printf "A: '$linkheader' (\$1 is %s) \n", pp $1;
(my $link1 = $linkheader) =~ s/ .*? (Self)/$1/gx;
printf "B: '$link1' (\$1 is %s) \n", pp $1;
(my $link2 = $link1) =~ s/a/$1/g;
printf "C: '$link2' (\$1 is %s) \n", pp $1;
^Z
A: 'axbyczSelfaxbycz' ($1 is undef)
B: 'Selfaxbycz' ($1 is "Self")
Use of uninitialized value in substitution iterator at - line 9.
C: 'Selfxbycz' ($1 is undef)
####
$string =~ m{ ... (pattern) ... }xms;
my $you_can_trust_this = $1;
...
... m{ ... }xms;
...
... s{ ... (foo) ... }{$1...$you_can_trust_this}xms;
####
my ($you_can_trust_this) = $string =~ m{ ... (pattern) ... }xms;
####
Win8 Strawberry 5.8.9.5 (32) Fri 11/06/2020 19:28:21
C:\@Work\Perl\monks
>perl
use strict;
use warnings;
my $string = 'foo + bar == baz';
my %xlate = (
'foo' => 'wibble',
'bar' => 'wobble',
'baz' => 'boff',
'+' => 'plus',
'==' => 'equals',
);
my ($rx_token) =
map qr{ $_ }xms,
join ' | ',
map quotemeta,
reverse sort
keys %xlate
;
print "\$rx_token $rx_token \n"; # for debug
print "before: '$string' \n";
$string =~ s{ ($rx_token) }{$xlate{$1}}xmsg;
print "after: '$string' \n";
^Z
$rx_token (?msx-i: foo | baz | bar | \=\= | \+ )
before: 'foo + bar == baz'
after: 'wibble plus wobble equals boff'