1)
$ cat i.pl
for(<>) {
print uc;
}
This is a simple tr(1) implementation.
These two lines and a closing brace do what tr does to
translate from lower case to upper case.
You run like this
$ perl i.pl
foo just trying
<ctrl-d>
You will get upper case results.
2)
$ cat i.pl
for(<>) {
print if /peter/;
}
Use same technique for running. You will only get lines that contain peter.
Now the / / operator is actually a regex match pattern in perl.
You can match for literal words like above or actually regex classes
like digits like this:
3)
$ cat i.pl
for(<>) {
print if /\d/;
}
Now you will get lines that contain digits 0 to 9. This also will work.
print if /[0-9/;
Another interesting sample.
4)
$ cat i.pl
for(<>) {
print $& if /\d+/;
}
This not only prints the match but also matches multiple digits.
That is what \d+ does.
regex is a complex topic and all perl code you find will go through a
file line by line and look for matches.
So remember the diamond operator <> for reading a file line by line and
the regex match / / operator.
You can easily do an if else condition match and execute code in perl using
regexes.
This finds plenty of practical use.
I will come up with some practical instances tomorrow.
-Girish
--
Gayatri Hitech
http://gayatri-hitech.com
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc