I wonder if you'd care to suggest how we could improve the documentation for s/// to make it more obvious how to solve your problem. Currently the documentation says this:
Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (specifically, the empty string).
Update: s///, not chdir...
| [reply] [d/l] [select] |
>Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (specifically, the empty string).
Thank all.
Actually I read the part about s/// for many times but didn't notice about that......
The reason for that is:
1. most of the time we use $string =~ s/($pattern)/($sth)/;
2. In this case, newbie will ask: How can I get the number of substitution made?
3. And traditional programming language not prefer guys play the tricks of precedence (like Java, VB)
4. But the script guys are (and actually C of geeks)
5. When I becoming a script guys, I start to treat the "$string =~ s/($pattern)/($sth)/;" is the expression with a return value.(we should have the concept of expression can have return value - that is not the case of Java!)
6. Assignment have very low precedence, just little more than the list operator and alphabet and, or
6. Then I get it works:
$s = "Hi Hey Man, I got the enlightenment HERE!";
$cnt = $s =~ s/H/M/g;
print "$cnt\n";
Result:
3
We REALLY need better tutorial, from the point of view of the non-scripting programming, they are really 2 different world.
And
Some part of the perl doc give me the feeling of the conf file of sendmail: If I am professional and remember everything, I don't need documentation at all.
If you are interested in making perl documentation better, documentation not only explaining the function behaviour, it should include some advance usage and example - plain text doesn't explain anything in most of the time. The code can express the idea better. | [reply] [d/l] [select] |
See the perlretut tutorial on regular expressions and search for this (which specifically deals with s///)
This example counts character frequencies in a line:
| [reply] |