Learning Perl, 4th Edition Notes 0x8 by
DaNmarner
* The operator "s///" substitutes the part of variable that matches with the pattern between the first two slashes with the stuff between the last two slashes, and returns a boolean value to tell whether the substitution is successful.
* Putting a "g" after "s///" makes it operate on all the parts that can be matched in the variable, instead of just one.
* Like "m//", the delimiter in "s///" is variable from different characters, either paired or nonpaired. Same thing happens to the other matching modifiers like /i, /x and /s. So is to the binding operator (=~).
* Case shifting works on the replacement word:
\U forces whatever following it to be uppercase, while for \L, lowercase.
The effects of the two guys above could be turned off by \E.
\u and \l work on only one letter that follows them.
* Case shiftint works not only on part of the "s///", but also in all the double quoted strings.
* Yielding a list, the split operator seperates a scalar of string by its given seperator:
@list = split /seperator/, $string;
* Yielding a scalar, the join operator glues a list of strings with the given glue string between the pieces:
$scalar = join $glue_string, @list;
* Used in a list context, the "m//" operator returns a list of matching variables created with the parentheses withing the pattern.
* To make the quantifiers non-greedy, put a qustion mark (?) after them.
* The modifer "m" changes the "m//" and "s///" operator into operating each line within a variable, instead of the whole variable.
* By default, the value of $^I is undef. But when it is assigned with any string, that string will become the suffix of the backup of the files that are read in via the diamond operator and modified. When this happens, the default output is changed into those files from the standard output.
* The code operating on a serial of files
#!/usr/bin/perl # Part 1
$^I = ".bak"; # Part 2
while( <> ) { # Part 3
s/Randal\ the\ grey/"Randal the white"/g; # Part 4
print; # Part 3
} #Part 3
is equivalent to the following in command line:
$ perl -p -i.bak -e 's/Randal\ the\ grey/"Randal the white"/g' The_serial_of_files
As the comments show, the arguments in the command line contributes to differnt parts in the code. To clarify, part 1-4 are each contributed by "perl", "-i.bak", "-p", "-e ...". This is hard to put shortly, but in addtion, using -n instead of -p will take the "print" line out of the code and leave other things the same.
* There's a way to group things in Regular Expression without triggering the memory variables. To achieve this, lead whatever is grouped with "?:".
No comments:
Post a Comment