Hacker News new | past | comments | ask | show | jobs | submit login
Announcing perl1line.txt - A collection of handy Perl scripts (catonmat.net)
74 points by pkrumins on Nov 14, 2011 | hide | past | favorite | 26 comments



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?

    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::VERSION


This 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 V



Making this for all the different languages would be awesome. I'm thinking PHP, jquery etc. one liners.


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.


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.


Ruby:

   print a.join
Perl:

   print foreach @a
Ruby wins!


I think you mean:

    print @a;


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.


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.


The first example:

-- 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.hs


OK, 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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: