Robert's Perl Tutorial
In the interests of efficiency, consider this:
print "Give me a name :"; chop($_=<STDIN>); print "Good name\n" if /Pe(tra|ter|nny)/;
The code above functions correctly. If you were wondering what a good name is, Petra, Peter and Penny qualify. The regex is not as efficient as it could be though. Think about what Perl is doing with the regex, that you are just ignoring. Simply throwing away casually. Without consideration as to the effort that has gone into creating it for you. The resources squandered. The little bytes of memory whose sole function in life is to store this information, which will never be used.
What's happening is that because parens are used, perl is creating
your usage and abusage. While this may not seem important, a fair amount of resources go
$2 and so on. Not so much the memory used to
store them, more the CPU effort involved. So, if you aren't going to use the parens for
capturing purposes, why bother capturing the match?
print "Give me a name :"; chop($_=<STDIN>); print "Good name\n" if /Pe(?:tra|ter|nny)/; print "The match is :$1:\n";
The second print statement demonstrates that nothing is captured this time. You get the benefits of the paren's precedence-changing capabilities, but without the overhead of the capturing. This benefit is especially worthwhile if you are writing CGI programs which use parens in regex -- with CGI, every little of bit efficiency counts.