s///o and qr// are pretty much the opposite of each other. If you make a regexp with the o argument you are basicly tellin the compiler that you will not change any of the variables associated with this regexp. Because of this perl will only ever compile this regexp once and will never change it, even if you do change the variables.
qr// is used to compare a string for use in a regexp. Consider the following:
$tofind = "some string";
while(<DATA>) {
push @list, $_;
}
# Make a compiled list with qr
@clist = map qr/$_/, @list;
# Now run the two lists
foreach (@list) {
print "Found $tofind\n", last if($tofind =~ /$_/);
}
foreach (@clist) {
print "Found $tofind\n", last if($tofind =~ /$_/);
}
Warning: This code was written by a sleepy, coffee deprived programmer first thing in the morning. It has not been tested and may not run, explode and/or set fire to your computer.
The second loop, with @clist, will run faster as a lot of work has been done for the compiler already. Another possible use is when you have a list of patterns to match and you want to run some against certain rules (say case insensitivity) and some against others. You could either write a complicated set of subroutines or add extra data to flag which pattern had which requirement... or just precompile them all with qr!
$japh->{'Caillte'} = $me; |