http://qs321.pair.com?node_id=444537

willyyam has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know of a good beginners tutorial on writing Perl one-liners? I constantly find myself looking at a problem that I know is trivial to solve, but I simply haven't written enough Perl (and no one-liners) to be able to take the problem from "theoretically trivial" to actually trivial.

For example, I have a directory of MS Worm (er, Word) files. I want to run them all through antiword to convert them to text. In English I'd say:

for each doc file, run antiword, redirecting output to a file with the same filename, but the txt extension.

And that's where I get stopped - I don't know enough about the syntax to turn my tiny problem into a tiny solution. So, any good tutorials around? There seems to be lots of reference to example programs, and lots of scripting tutorials, but not enough hand-holding for my sad self. Thanks.

Replies are listed 'Best First'.
Re: How-To on Perl one-liners
by tlm (Prior) on Apr 03, 2005 at 21:59 UTC
      Wow... I was just going to mention that, and you beat me to it... :-)
        Cog,
        Great presentation, a lot of things I didn't know. Thanks =).

        Regards Paul.
Re: How-To on Perl one-liners
by Thilosophy (Curate) on Apr 03, 2005 at 22:09 UTC
    There is a nice tutorial on mass-editing of files on perl.com. The complete list of all the useful command line switches is in the perlrun manual.

    What I like to do is combine Perl (to process the files) with the UNIX command line tool "find" (to make the list of files to hand to Perl). If you are on Windows, you can still go with the File::Find module (examples for this are in above tutorial).

    Another thing to watch out for if you are writing one-liners with "perl -e" is that you easily run into quoting problems (totally depends on the shell you are using), see Escaping characters in one-liners.

Re: How-To on Perl one-liners
by petdance (Parson) on Apr 03, 2005 at 22:36 UTC
Re: How-To on Perl one-liners
by ikegami (Patriarch) on Apr 03, 2005 at 22:13 UTC

    Perl's not needed here.

    From prompt:
    for %q in (*.doc) do antiword "%q" > "%~nq.txt"

    From batch file:
    for %%q in (*.doc) do antiword "%%q" > "%%~nq.txt"

      Absolutely true, and thank you for the code. However, I work on several systems, all of which have Perl, but not all of which have a consistent or sane shell. I believe the relevant quote is, "It is easier to port a shell than a shell script." My thinking in asking the question is, if I grok Perl one-liners, I don't need shell scripting nearly as much.
        if I grok Perl one-liners, I don't need shell scripting nearly as much

        Indeed, but with oneliners, you still need to know the shell fairly well. I'm not implying you don't, but I'm just trying to make the point. There are many shell gotchas involved when writing oneliners. Most common are quoting and glob expansion (or expansion in general) woes. Also, it's often more natural to use a perl oneliner as part of the pipeline, rather than the entire thing. This requires knowing the shells you're working with. There's really no [reasonably convenient] way of avoiding it. :-)

Re: How-To on Perl one-liners
by ww (Archbishop) on Apr 03, 2005 at 22:07 UTC
    by inference, the big issue for you is apt to be correctly escaping DOS shell characters such as \, on which answers can be found at MS or perhaps even with a supersearch here.

    Re the rest, you can turn a script-file into a one liner for most tasks on the order of what you pseudo-coded by simply removing the returns. Now, yes, that's a tad oversimplified, but as a mechanism for learning what you ask about, those that don't work are probably as instructive as those that do

    (ww is the monk formerly known as schodckwm)

      I appreciate the advice. It reminds me that I need to be explicit about what platform I'm using. Too much time talking only to people who use one OS limits my thinking. I use Linux, but it is plainly very good to know about shell escapes. Thanks again.
Re: How-To on Perl one-liners
by brian_d_foy (Abbot) on Apr 04, 2005 at 01:20 UTC

    There are many great resources that people have already mentioned, but as an old-timer, I fondly remember Randal's first Unix Review column. :)

    --
    brian d foy <brian@stonehenge.com>
Re: How-To on Perl one-liners
by davido (Cardinal) on Apr 04, 2005 at 04:46 UTC

    For Yet Another Perl One-Liners introduction, have a look at this one (shameless plug, I'm the author): Re: One Liners.


    Dave

Re: How-To on Perl one-liners
by mhearse (Chaplain) on Apr 04, 2005 at 02:52 UTC
    There was a good article on this in last month's issue of SysAdmin magazine, also by merlyn.
Re: How-To on Perl one-liners
by strat (Canon) on Apr 04, 2005 at 07:33 UTC

    On my homepage www.fabiani.net -> Vortraege -> "Perl in der Kommandozeile" there is a powerpoint presentation (afraid it is in german) of a talk I had some years ago

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

Re: How-To on Perl one-liners
by chanio (Priest) on Apr 05, 2005 at 06:29 UTC