You shouldn't be too keen on writing multiple operations into a single statement: it only serves to spoil the readability of your code. But I suggest that you write something like this.
There is no point in using the /s
modifier on your original pattern match as it affects only the .
metacharacter, and there are none of these in your pattern. Also, it makes no sense to use /g
as you are interested only in a single match.
Don't be tempted to join the two statements into one line, as the if
statement modifier would then also apply to the my
declaration which has some very strange effects.
use strict;
use warnings;
use 5.010;
my $text = 'Date: 1994.04.04';
my $result;
$result = $1 =~ tr/./-/r if $text =~ /Date:\s+(\d{4}\.\d\d\.\d\d)/i;
say $text;
say $result;
output
Date: 1994.04.04
1994-04-04