in reply to Substituting white spaces with a tab changes my string into numeric value
s/\s+/\t/g; is equivalent to $_ =~ s/\s+/\t/g;, so $line =~ s/\s+/\t/g; actually means $line = ($_ =~ s/\s+/\t/g); (The parentheses aren't actually necessary, but I wanted to make the precedence completely clear.)
In scalar context, a regex match or substitution (=~) returns the number of matches found/substitutions performed, so, to finally get to where that 2 comes from, $line =~ s/\s+/\t/g; changes all the groups of whitespace in $_ into tabs and sets $line to the number of substitutions which were performed. If you look at $_, you will find that it holds the value you wanted, since $line and $_ held the same value before the substitution was performed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Substituting white spaces with a tab changes my string into numeric value
by PerlPeer (Novice) on Sep 20, 2012 at 09:22 UTC |