TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

10 tips for writing perl one-liners

64 pointsby nelhageabout 15 years ago

8 comments

jrockwayabout 15 years ago
Perl is dead. One-liners are unmaintainable. Everyone should write their one-off command invocations with test driven development, separation of interface and implementation, javadoc, and aspect-oriented programming.
评论 #1384231 未加载
评论 #1384240 未加载
评论 #1385031 未加载
pkruminsabout 15 years ago
Also see my article on "Perl One-Liners Explained":<p><a href="http://www.catonmat.net/blog/perl-one-liners-explained-part-one/" rel="nofollow">http://www.catonmat.net/blog/perl-one-liners-explained-part-...</a>
评论 #1384617 未加载
devinjabout 15 years ago
I think this article has convinced me that I should learn Perl. Looks bloody useful for shell scripts, indeed ("a better awk" is the phrasing I've heard).
评论 #1384235 未加载
评论 #1384953 未加载
评论 #1384411 未加载
d0mineabout 15 years ago
`END{ }` could be replace by eskimo operator `}{`:<p><pre><code> perl -F, -lane '$t += $F[1]; }{ print $t' </code></pre> <a href="http://www.perlfoundation.org/perl5/index.cgi?eskimo_operator" rel="nofollow">http://www.perlfoundation.org/perl5/index.cgi?eskimo_operato...</a><p>`..` is called flip-flop in the context describe in the post.<p>Goatse operator `=()=`:<p><pre><code> my $count_vowels =()= /[aeiou]/g; </code></pre> Here's another usage for `\K`:<p><pre><code> echo '/a/b/c/def/gh' | perl -lnE'$d{$`}++while/\w\K\b/g }{$,=" ";say keys%d' </code></pre> Output:<p><pre><code> /a/b/c/def/gh /a/b/c /a/b/c/def /a/b /a </code></pre> <a href="http://stackoverflow.com/questions/2892126/file-fix-it-codegolf-gcj-2010-1b-a/2894170#2894170" rel="nofollow">http://stackoverflow.com/questions/2892126/file-fix-it-codeg...</a><p>Other features: <a href="http://stackoverflow.com/questions/161872/hidden-features-of-perl/" rel="nofollow">http://stackoverflow.com/questions/161872/hidden-features-of...</a>
vtailabout 15 years ago
Tricks 1-7 are also supported by Ruby - it accepts the same command-line options with the same functionality.
telemachosabout 15 years ago
A good collection of articles, tutorials and slide-shows on Perl one-liners, all in one place:<p><a href="http://bufferfly.members.winisp.net/Perl_oneLiners.html" rel="nofollow">http://bufferfly.members.winisp.net/Perl_oneLiners.html</a><p>(I remember especially liking the one from Sial.org - a lot of good Perl content on that site, the two from Cultured Perl and the FMTYEWTK on mass edits.)
chrismealyabout 15 years ago
I didn't know about \K and -MRegexp::Common. I liked the explanation of '..' too. Thanks.
absconditusabout 15 years ago
See also:<p><a href="http://petdance.com/perl/command-line-options.pdf" rel="nofollow">http://petdance.com/perl/command-line-options.pdf</a>