Announcing perl1line.txt - A collection of handy Perl scripts(catonmat.net)
catonmat.net
Announcing perl1line.txt - A collection of handy Perl scripts
http://www.catonmat.net/blog/announcing-perl1line-txt/
3 comments
Making this for all the different languages would be awesome. I'm thinking PHP, jquery etc. one liners.
Here's a page for Haskell: http://www.haskell.org/haskellwiki/Blow_your_mind
It isn't exactly the same--some examples are two or three lines and it's more about coolness than practicality--but it's in a similar spirit, and, more importantly, also interesting.
It isn't exactly the same--some examples are two or three lines and it's more about coolness than practicality--but it's in a similar spirit, and, more importantly, also interesting.
The first example:
-- split at whitespace -- "hello world" -> ["hello","world"] words
-- split at whitespace -- "hello world" -> ["hello","world"] words
unfoldr (\b -> fmap (const . (second $ drop 1) . break (==' ') $ b) . listToMaybe $ b)
takeWhile (not . null) . evalState (repeatM $ modify (drop 1) >> State (break (== ' '))) . (' ' :)
where repeatM = sequence . repeat
fix (\f l -> if null l then [] else let (s,e) = break (==' ') l in s:f (drop 1 e))
Not really in the same spirit at all.That blows my mind, but certainly not in a good way. Ruby:
$ echo 'hello world' | ruby -e 'p STDIN.read.split'Be aware that those are four separate examples made to showcase features of the language, the simplest of which being just `words`.
split.hs:
main = interact (show . words)
$ echo hello world | runhaskell split.hsOK, thats not really clear from the layout. Your example is clearer, but if you have to put it in a script and pipe stuff to it, its not really a 1-liner.
Do any other languages really lend themselves to the one line command tools like perl does?
The only one off the top of my head would be ruby but I don't have that much ruby knowledge to judge.
This kind of thing however is perl's bread and butter.
The only one off the top of my head would be ruby but I don't have that much ruby knowledge to judge.
This kind of thing however is perl's bread and butter.
Compared to Ruby, one liners are more efficient in Perl because the normally infuriating cryptic variables and defaults save space. For example in Perl you can run a foreach without naming the current iteration value and it defaults to $_. $_ in turn is the default target for many commands, like print.
So Perl's `print foreach @a` becomes Ruby's `a.each{|a| print a}`. Not so bad but it adds up.
So Perl's `print foreach @a` becomes Ruby's `a.each{|a| print a}`. Not so bad but it adds up.
Benoit Hamelin did ruby one liners - http://benoithamelin.tumblr.com/ruby1line
Agreed - one reason why I still love Perl so much, despite its obvious warts.
It's missing one of my favorites:
perl -MData::Dumper\ 999
Quickest way to get the version of a module you're running.Shit like this is why Perl gets under my skin. The best way to do anything in Perl always seems to be some side effect of a clever hack, instead of actually solving the damn problem expressively.
The equivalent in Ruby?
The equivalent in Ruby?
gem list $GEMNAME
It's shorter and more clear. Take that, Perl golfers!Interesting. I would have done it this way previously:
perl -MData::Dumper -e 'print $Data::Dumper::VERSION."\n"'Works for any module:
$ perl -MLWP::UserAgent\ 999
LWP::UserAgent version 999 required--this is only version 5.833.Well, almost any module:
$ perl -MFile::Slurp\ 9999
<hangs forever> mike@server:~$ perl -MLWP::UserAgent -e 'print $LWP::UserAgent::VERSION."\n"'
5.835
mike@server:~$Even shorter, as -l appends \n to all prints:
$ perl -MLWP::UserAgent -le 'print $LWP::UserAgent::VERSION'Even shorter!
$ M=LWP::UserAgent && perl -M$M -le "print \$$M::VERSION"
Even even shorter! $ M=LWP::UserAgent;perl -M$M -Esay\$$M::VERSIONThis reminds me of that joke in which jokes have been reduced to numbers. It's nearly as obscure, too.
Did Gödel come up with that joke?
That works, too!
Doesn't work for File::Slurp, which has version 9999.19. :) Otherwise awesome thingy to query module versions.
I'm a fan of the "V" module.
perl -MV=Data::Dumper
"Quicker", if you don't count install time. cpanm -n VDirect download link: http://www.catonmat.net/download/perl1line.txt