Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Scriptome and Perl One-Liners

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

I was reading about Scriptome on perl.com 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 (Archbishop) 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 perl.com 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"}' file.tab

    Or, I should even dare this:

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

    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.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://502261]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found