Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Scriptome and Perl One-Liners

by willyyam (Priest)
on Oct 23, 2005 at 00:33 UTC ( #502261=perlmeditation: print w/replies, xml ) Need Help??

I was reading about Scriptome on the other day, and thought that much of the code in the Scriptome would make an excellent tutorial on Perl one-liners.

The format of each script helps it to make more sense than many one-liners - each script expects an input file, and results in an output file - and so reading these sciptlets is eased by this underlying decision. Because there are scripts for both Windows and Unix/Mac OSes, you can also see how those systems differentiate for use in one-liners, which can help you to develop your own scripts across the platforms.

I think that anyone looking to create quick scripts or one-liners would benefit from looking at this collection of code. Noting the use and meaning of flags, or just noting the appoach for creating these tiny snippets is well worth a look.

Replies are listed 'Best First'.
Re: Scriptome and Perl One-Liners
by eyepopslikeamosquito (Chancellor) on Oct 23, 2005 at 05:43 UTC

Some non-PM Perl one-liner tutorials:

      I know there's no shortage of good one-liner tutorials (your comprehensive collection of links is outstanding though - thanks). The problem I've had with most of those tutorials while working through them was that sometimes I just wanted a solution that did what I needed, and then I could spend the time I saved looking at the fundamentals underlying it. This collection of one-liners is handy to me because I can quickly find a near-fit solution, hack on it to fix my problem du jour, and learn by doing, which works better for me than reading through even the best tutorial.

        Curiously, the Scriptome article you referenced was written by reformed golfer Amir Karger ... who was a significant contributor to perhaps the largest and most unreadable collection of Perl one liners ever created: the Terje/mtv pdf book of Perl Golf. ;-) You don't want to go there. No, really you don't ... but if you do, you might also be interested in cog's eskimo greeting and other secret operators. :-)

Re: Scriptome and Perl One-Liners
by rnahi (Curate) on Oct 23, 2005 at 15:01 UTC
    much of the code in the Scriptome would make an excellent tutorial on Perl one-liners

    I would not be so quick about excellence.

    Take for example, this page

    perl -ne 'BEGIN {$col=1}' -e 's/\r?\n//; @F=split /\t/, $_; $sum += $F +[$col]; END {warn "Sum of column $col for $. lines\n"; print "$sum\n"}' file.t +ab

    I see an inefficient replacement of newline, instead of chomp. But since this is a one-liner, the "-l" option will do instead, and it will also save the output newlines.

    Then, the @F=split /\t/, $_; could be easily replaced by the "-a" autosplit option, which will happily take charge of different whitespace types, other than tabs.

    I would say that this one-liner woult be better written as

    perl -e 'BEGIN {$col=1}' -lane '$sum += $F[$col]; END {print "Sum of column $col for $. line: $sum"}'

    Or, I should even dare this:

    perl -lane '$sum += $F[1]; END {print $sum}'

    It's shorter, thus easier to write, and less mistakes to make when copying it.

      Here's the contribution link. Some of these scripts aren't optimally short or beautiful, but the motive seems correct and the process is open.
Re: Scriptome and Perl One-Liners
by dbwiz (Curate) on Oct 23, 2005 at 07:59 UTC

    Apart from the dubious educational value of one-liners, I don't like this collection at all, for two reasons:

    • It encourages cargo cult programming
    • A great deal of these exampels could be made much easier using a database instead of plain text files. Even a minimalist database such as SQLite would greatly simplify tasks such as "Calculate sum of the nth column of tabular data" or "Calculate sum of a set of columns for each row". I mean, if you need to sum up some values, you need to read all the text anyway, so while you are reading it, it makes sense to export the data into a database, and then get the stats in no time.

    Since they are ready-to-use recipes, it would have been more useful to save each case into a well written Perl script, with properly named variables, and using a database when needed.

    The way it is now, it will just spread the rumor of Perl being a write-only language.

      Have you ever heard the expression "If you have a hammer, everything looks like a nail"? Or maybe in your case, "If you have a a dirty great big wrench, every nail, screw, thumb-tack and piece of blu-tac looks like it should be replaced with a dirty great big nut'n'bolt :)

      Do you really import flat files into your DB for one-off column sums? You must have an awful lot of once-used, ever-to-be-forgotten junk lying around in DB(s).

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Scriptome and Perl One-Liners
by tphyahoo (Vicar) on Oct 24, 2005 at 14:14 UTC
    I like it. So what if it's "cargo cult" or it occasionally doesn't have the very best way to do something? Perl is sexy because the learning curve encourages diving in without really knowing what you're doing, then figuring stuff out later and getting "legit". Scriptome encourages this approach. It presents a lot of useful info, in a compact way.

    I think it would be a better site if it allowed users to post improvements or observations at the bottom of "tools" pages, but even without this feedback feature, it looks pretty useful to me.

    So, thanks for posting the link. Even though I have been programming perl for over a year, I will be browsing this site for useful tricks I don't know yet.

Re: Scriptome and Perl One-Liners
by Anonymous Monk on Oct 23, 2005 at 02:50 UTC

    You are the kind of people who is responsible for creating bad image of Perl around the world. Go figure it.

    Considered by planetscape: Please delete anonymous troll.
    Unconsidered by Arunbear: keep (and edit) votes prevented reaping; Keep/Edit/Delete: 5/1/6

      I don't agree with you. Maybe it's not an ideal way to start learning perl, but it's a very good way to learn more about perl, and see how powerful perl can be.

      Well, how is a perl onliner supposed to be used? You type some code into a shell to do something. You usually don't save it (maybe except in shell scripts... but there it may or may not be as ugly as sed, awk or the like). Sure, you can write a whole script instead of this oneliner, but that often is much slower than just using the shell... especially when you know a bit about perl. And you can't rely on the existance of your favorite editor with automatically inserting code for speeding up common tasks on every system you are working. So better protest against perlvar, which contains some real dangers ;-)

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://502261]
Approved by friedo
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-06-23 01:09 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (533 votes). Check out past polls.