Perl-Sensitive Sunglasses | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
gam3 demonstrates a great method. ++. Perl Best Practices advocates avoiding using $1, $2, etc. whenever you possibly can, and that is a great way to do it. IMO it's sort of a "poor man's" named capture (one of the few regex features that Perl "lacks"), and you never have to refer to the numbered variables themselves except in some special cases like in the replacement clause of the substitution operator. I am also particularly fond of a one-step syntax to get the results of a substitution on a variable without affecting the original variable. It's hard to explain but I'm sure you've either done it or wanted to do it at some point. Here's an example (which I apologize is less than ideal; I wasn't sure if any spaces would turn up in the match from the split): I think of both of those tricks as falling in the same category, though I use the first more often. It came in handy in the very first lines of a CGI script I wrote: These examples are probably not the best but it's from code I could put my hands on in short order. While we're on the topic, I should note that it's a very good idea to use non-capturing parentheses(?:) whenever you want or need to use parentheses but don't need to remember the stuff that matched in there. I like computer programming because it's like Legos for the mind. In reply to Re: $1, etc. are not just strings
by OfficeLinebacker
|
|