Does this line of Perl really do anything?
$variable =~ s/^(\d+)\b/$1/sg;
The only thing I can think of is that $1
or $&
might be re-used, but it is immediately followed by.
$variable =~ s/\D//sg;
With these two lines together, is the first line meaningless and removable? It seems like it would be, but I have seen it multiple times in this old program, and wanted to make sure.
Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user.
$1 equals the text " brown ".
The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − s/PATTERN/REPLACEMENT/;
The built in Perl operator =~ is used to determine if a string contains a string, like this. The !~ operator is used to determine if a string does not contains a string, like this. Often, variables are used instead of strings.
$variable =~ s/^(\d+)\b/$1/sg;
^
at the beginning makes the /g
modifier useless..
in the string makes the /s
modifier useless, since it serves to make .
also match newline.\b
and ^
are zero-width assertions, and the only things outside the capture group, this substitution will not change the variable at all.The only thing this regex does is capture the digits into $1
, if they are found.
The subsequent regex
$variable =~ s/\D//sg;
Will remove all non-digits, making the variable just one long number. If one wanted to separate the first part (matched by the first regex), the only way to do so would be by accessing $1
from the first regex.
However, the first regex in that case would be better written simply:
$variable =~ /^(\d+)\b/;
And if the capture is supposed to be used:
my ($num) = $variable =~ /^(\d+)\b/;
Is "taint mode" in use? (Script is invoked with -T option.)
Maybe it's used to sanitize (i.e. untaint) user input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With