Why doesn't replacement in the context of a list return the capture list?

As per perlop

in the context of a list, the corresponding m//

regex operator with a capturing group and /g

will return a capturing list:

my $str = "8a9b0c"; my @res = ($str =~ m/(\d)/g); print @res;

# Prints "8 9 0"

      

However, I can not find any perlop

, nor prelre

any mention of why instead of a substitution - even in the context of the list - returns the number up for grabs:

my $str = "8a9b0c"; my @res = ($str =~ s/(\d)//g); print "@res\n"

prints "3"

      

Why and where is this behavior documented?

+3


source to share


2 answers


From perlop

:

Searches for a string for a pattern and, if found, replaces that pattern with replacement text, and returns the number of substitutions made.



No distinction is made between a list and a scalar context. So it returns the number of payments and then assigns that to the list. This will simply convert the number to a list containing the number.

+4


source


From perlop

:

  • s/PATTERN/REPLACEMENT/msixpodualgcer

    Searches for a string for a pattern, and if found, replaces that pattern with replacement text, and returns the number of replacements made . Otherwise, it returns false (in particular, an empty string).



The docs do not offer an alternative specification for list context such as operator m//

.

+2


source







All Articles