Subject: | Matching a zero with String::Substitution |
Date: | Thu, 23 Apr 2015 21:03:20 +0200 |
To: | bug-string-substitution [...] rt.cpan.org |
From: | Håkon Hægland <hakon.hagland [...] gmail.com> |
I am trying to match a digit with String::Substitution; it works fine if
the digit is not zero. If the digit is zero it substitutes the empty string
instead of the digit. For example:
use strict;
use warnings;
use Data::Dump;
use String::Substitution;
my @data = qw(0 1);
for (@data) {
my $str = $_;
my $regex = qr/(\d)/;
my $replace = '$1';
my $result_str = String::Substitution::gsub_copy($str, $regex,
$replace);
my @m = $str =~ /$regex/g;
dd $result_str;
dd @m;
}
The output is:
""
0
1
1
expected output would be:
0
0
1
1
I believe there is a bug on line #95 in the source code
https://metacpan.org/source/RWSTAUNER/String-Substitution-1.001/lib/String/Substitution.pm
map { ($$_) || '' } ( 1 .. $#- )
it should be
map { ($$_) // '' } ( 1 .. $#- )
Note: defined or // not regular or || as in the source..
See also
http://stackoverflow.com/questions/29831317/matching-a-zero-with-stringsubstitution
Best regards,
Håkon Hægland