Perl One-Liners Explained, Part VII: Handy Regular Expressions
catonmat.net
catonmat.net
I share your sentiment. The article is certainly useful for learning regexps. But IMHO it should also point to the correct way of doing things - often, the correct way is using a module and thus the resulting code is not much longer than the code in the article. For email address validation:
use Email::Valid;
print (Email::Valid->address('john@example.com') ? 'valid' : 'invalid');
as a oneliner: perl -MEmail::Valid -E"say (Email::Valid->address('john@example.com') ? 'valid' : 'invalid');"
Other than installing the Email::Valid module with a cpanm Email::Valid
it is not much longer than the example in the article.`Notice that I say "looks like". It doesn't guarantee it is an email address.`
And yeah, you can find the full regular expression in the back of one of O'Reilly's Perl books (the regular expression handbook I believe).
It's nice to see perl code from time to time, even if it's just one line :)
But the more important point is that an article that sounds so authoritative should present much higher quality.
That said, perl one-liners are certainly useful so thanks to the OP for putting this together. I just think it would add a lot of value to include examples of where one is likely to go wrong.
Mail::RFC822::Address is a Perl module to validate email addresses according to the RFC 822 grammar. (...) Implementing validation with regular expressions somewhat pushes the limits of what it is sensible to do with regular expressions, although Perl copes well:
(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(
?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[
\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0
** SNIP: 76 lines **
?:\r\n)?[ \t])*))*)?;\s*) The second regex matches <em>hello</em> because.If you need a book to explain one-liners, it will be challenge to comprehend a program with 20 such one-liners.
As others have mentioned (and as the articles themselves repeatedly mention), most of the time, if you have a common problem, you should find and use a CPAN module already built to solve it.
I have never used a code snippet from a one-liner compilation in one of my programs, but I have learned a lot of new constructs by reading them.
EDIT: Or, what tikhonj (http://news.ycombinator.com/item?id=3219843) said.
Good Perl is basically 72% of Ruby. (Less syntactic sugar. Less-structured reflection. And mildly crufty sigils - not that you'll notice those after your second week, though.)