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

Resource for command line "one-liner" interpretation

by Hagbone (Monk)
on May 14, 2003 at 23:23 UTC ( #258280=perlquestion: print w/ replies, xml ) Need Help??
Hagbone has asked for the wisdom of the Perl Monks concerning the following question:

I have a question that I believe is perl enough oriented to justify posting (and I'm not looking for an answer, but more for advice).

Up until recently, I've had to lead my perl/web/programming life in an environment that offered what many would consider "crippled shell access". Now that I have full/typical access, my administrative life has changed ...

For years, I watched and noted as others talked about "one-liners", knowing that I was unable to take advantage of this aspect of box management. Now that the one-liner option is available to me, I'm looking for advice on resources that will bring me up to speed on this new tool.

Many times, I've looked at posts that offer one-liners, and my jaw drops at the amount of work that is performed from just one line of a command. But just about every time my jaw drops, I also realize that I have no clue how to interpret the one-liner. Resources that help me understand these types of one-liners is what I'm looking for.

I figure that since system calls like:

system "(nice /usr/local/bin/zip -r -q $storage_path $copy_path)";
are part of how I'd use the one-liners, my question qualifies as an appropriate perl question for the Monks.

Any and all suggestions are appreciated.

Comment on Resource for command line "one-liner" interpretation
Download Code
Re: Resource for command line "one-liner" interpretation
by MrYoya (Monk) on May 14, 2003 at 23:33 UTC
    Here's a website that has a lot of one-liners: Perl Limericks

    Also man perlrun has a description of what all those switches do

Re: Resource for command line "one-liner" interpretation
by leriksen (Curate) on May 15, 2003 at 00:42 UTC
    have a look at The Perl Journal. The May issue has an article 'Data Manipulation and Perl Command-Line Options' - and if it was useful then subscribe to TPJ
Re: Resource for command line "one-liner" interpretation
by graff (Chancellor) on May 15, 2003 at 01:31 UTC
    For me, one of the essential features of command-line facility is the pipe. Notice what sorts of operations you need to use often on command lines (sorting, grepping, extracting one or more columns from a list of lines, doing string substitutions, etc); usually you'll find a unix utility that does just one of these things quite well, with a lot of flexibility in how you can do that one thing.

    If you have a particular need that is not easy to do with an existing tool, write an simple tool in perl to make that operation easy on the command line, and put it in your PATH. A typical situation is: I want to locate all files whose names contain "x", and whose size is greater than 10240 bytes, and determine the total space consumed by these files. (This could certainly be done completely (and fairly easily) in perl, but using standard command line tools in combination with perl makes it even easier (a lot less typing) and offers a lot of flexibility, esp. if your command-line-interface is like bash, and makes it possible to recall, edit and re-execute earlier commands:

    find . -name '*x*' -type f -printf '%s\n' | perl -ne 'chomp;$s+=$_ if( +$_>10240);END{print "$s\n"}'

    I found myself needing to sum columns like this quite often, in many different situations, so I wrote a perl script to do just this (with options for flexibility), reducing the above example to:

    find . -name '*x*' -type f -printf '%s\n' | sumcol -min 10240
    In general, if I'm writing "perl -e '...'" or "perl -ne '...'" etc on a command line, I'm not using a system call within that perl script -- it's quicker (less typing) to use the command line for running unix tools, and to pipe their output to a perl script when necessary to accomplish other things.
Re: Resource for command line "one-liner" interpretation
by hv (Parson) on May 15, 2003 at 02:02 UTC

    The main thing to look at is perlrun, which will describe the command-line options to perl - understanding what options are available, and how they interact with each other, is crucial to writing and understanding one-liners.

    Beyond that, it is just perl. One-liners tend to take shortcuts that make the perl less obvious to someone more used to application code - no strictures, heavy use of special variables (perlvar) and nasty tricks that would be more trouble than they're worth in a production/maintenance environment.

    Another important aspect to grok is the Unix philosophy beyond perl - lots of tools that do (mostly) simple things and work well together. For example, this is an invocation I use remarkably often:

    find . -type f | xargs perl -wle '... some code ...'
    The power of perl knowledge is that you can add to this set - look out for things that you do regularly, and try to write tools to add to the set to reduce the amount of typing and, more importantly, thinking required to do your day to day tasks. And when you write such tools, treat them as applications rather than one-liners - perl is all about having the kitchen sink as well.

    Taking it out of context, this article includes some short examples of the sort of utilities I'm talking about. But there's no reason your utilities need to be short - one of my most often used self-written utilities is 818 lines (including POD) and supports 19 distinct command-line options in around 25920 combinations.

    Hugo
Re: Resource for command line "one-liner" interpretation
by Util (Priest) on May 15, 2003 at 21:36 UTC

    B::Deparse can translate a one-liner into a (almost) regular program. To use it, add -MO=Deparse to the beginning of perl's command line.

    Using the Perl section of graff's example:

    perl -MO=Deparse -ne 'chomp;$s+=$_ if($_>10240);END{print "$s\n"}'
    This gives us:
    LINE: while (defined($_ = <ARGV>)) { chomp $_; $s += $_ if $_ > 10240; sub END { print "$s\n"; } ; } -e syntax OK
    which we can tighten up to be:
    while (<>) { chomp; $s += $_ if $_ > 10240; } print "$s\n";

Re: Resource for command line "one-liner" interpretation
by Enlil (Parson) on May 16, 2003 at 21:00 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://258280]
Approved by graff
Front-paged by Tomte
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-08-30 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls