Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

One Liners

by lakeTrout (Scribe)
on Jan 11, 2005 at 05:45 UTC ( #421195=perlquestion: print w/ replies, xml ) Need Help??
lakeTrout has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,
I'm new here and finally have a question that I couldn't find an answer to (a first). Can anyone point me to some resources/documentation/tutorials etc on perl one-liners? I have picked up some things here and there, but would like better explanations on what the options like  -pie, pack and alike do and what they are for.

Thanks,

lakeTrout

Comment on One Liners
Download Code
Re: One Liners
by prasadbabu (Prior) on Jan 11, 2005 at 06:00 UTC

    lakeTrout,

    what the options like -pie, pack and alike do and what they are for

    In the perl manpage, you can see pack,unpack functions. There they have explanations for pack.

    Prasad

Re: One Liners
by Thilosophy (Curate) on Jan 11, 2005 at 06:15 UTC
    The manpage for command line options is perldoc perlrun and there was recently a good tutorial on perl.com .
Re: One Liners
by davido (Archbishop) on Jan 11, 2005 at 06:43 UTC

    There are just a few things to grasp at first, and then it becomes easy.

    First, you probably already know this, but with the -e option on the command line, you can follow it with a string of Perl code to be executed. Some operating systems prefer the code to be enclosed in single quotes, and some in double. An example could be:

    perl -e "print qq/Hello world!\n/;"

    Notice I used the qq// quotes instead of "" (double quotes). This was done because the entire string of code is wrapped in double-quotes, and it would confuse the command shell if you start embedding other similar quotes within the one liner. Some OS's allow for escaping, but it's easier just to avoid that issue altogether by using alternate quotes.

    The next thing to understand is the -n option. -n opens each file listed on the command line, and iterates over each line in the file. On each iteration, $_ is set to that line of data. Here's an example:

    perl -n -e "print qq/$.:\t$_\n/;" textfile.txt

    The preceeding will iterate over each line in 'textfile.txt', and print that line, along with the line number of the file (that's what the $. special variable contains).

    Consider the -n option to be roughly equivilant to: while( <> ) { ........ }, where you get to specify what goes inside of the { ......... } block.

    Since a lot of one liners need to output the file they're iterating over, we can save typing by using the -p switch, instead of -n. -p does the same as -n, but has an implicit print $_ on each iteration of the loop. Think of -p as this:

    while ( <> ) { .......... # Your code goes here. print $_; }

    This is often put to use by specifying some action that acts upon $_ in some way. If you modify $_ on each iteration, its modified content will be output for each line. Here's an example:

    perl -pe "s/\bperl\b/Perl/g;" textfile.txt

    This will iterate over "textfile.txt", and substitute all occurrences of 'perl' with 'Perl'. The result is dumped to STDOUT, unless you redirect it. ...let's redirect it to a tempfile:

    perl -pe "s/\bperl\b/Perl/g;" textfile.txt >temptext.txt

    Easy huh? Ok, now it gets better. Many times you just want to do what's known as an in-place edit of your input file. The -i option does that. With the -i option, you can specify just -i, and leave it at that, or you can specify -i.bak to tell Perl to output a backup file before acting upon the original file. '.bak' was chosen arbitrarily. It could also be -i.hello -i.old, or whatever other extension you want to give the backup file. Here's how you put inplace editing to work:

    perl -pi.bak -e "s/\bperl\b/Perl/g" infile.txt

    This will iterate over each line of infile.txt, changing each occurrence of 'perl' to 'Perl', and then saving that change. A backup of the original file will be saved as infile.txt.bak

    Another commandline option is the -M option. -M is the same as "use ...." So if you say, perl -MData::Dumper -e "print Dumper \%::", it's the same as including the line "use Data::Dumper" at the top of your script.

    There you go.... all the ammo you need for basic one-liners. You can get a lot more complex with it than that, and I have only hit on a few of the command-line options. See perlrun for a list of all commandline options, as well as a description of how to use most of them.

    Have fun!

    Your question also mentioned pack. This function, and its counterpart 'unpack' are discussed in Perl's perlfunc under pack and unpack. Perl also comes with a great pack/unpack tutorial called perlpacktut. The tutorial ought to really give you more than you ever wanted to know about pack and unpack.... HTH.

    Update: Fixed missing -e per Aristotle's sharp eyes.


    Dave

      wow -- that was exactly what I was looking for davido. Thank you all for the replies, as I said, I'm new here and appreciate all of your help and patience. This place is great! Thanks again!
      perl -p "s/\bperl\b/Perl/g;" textfile.txt

      You're missing an -e in there (and in the following example.)

      Makeshifts last the longest.

        Thanks... Fixed.


        Dave

      perl -e "print qq/Hello world!\n/;" Notice I used the qq// quotes instead of "" (double quotes). This was done because the entire string of code is wrapped in double-quotes, and it would confuse the command shell if you start embedding other similar quotes within the one liner. Some OS's allow for escaping, but it's easier just to avoid that issue altogether by using alternate quotes.

      And, as I recently learned, any character can be used as the quotes if you leave a whitespace after the qq. My favorite now is to use qq qq... which makes for nice obfu (especially if you are quoting NUL and type it just as I did here). Just don't use q if q is part of the quoted text. Likewise, don't use / if / is part of the quoted text... (remembering to escape can be a real pain)


      --
      Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0
      perl -e 'print(map(chr,(0x4a,0x41,0x50,0x48,0xa)))'
Re: One Liners
by eyepopslikeamosquito (Canon) on Jan 11, 2005 at 06:44 UTC
      i looked at this thread, and *knew* i wrote something on one-liners a while back. ( another benefit of one-liners ). you beat me to pointing it out.
Re: One Liners
by blazar (Canon) on Jan 11, 2005 at 11:07 UTC
    I'm new here and finally have a question that I couldn't find and answer to (a first). Can anyone point me to some resources/documentation/tutorials etc on perl one-liners?
    Well, a few other monks already gave you some answers addressing exactly your question. As an example of a one-liner I routinely use I'll give:
    perl -lpi.bak -e "" <file(s)>
    under Windows to convert from *NIX-style \n's to Redmondish ones (yes, I know there are tons other WTDI both in Perl and with other tools - but this way I don't even have to remember which is which).

    The -i.bak is there because under Windows (at least 98 or earlier, that is, and if things have not changed recently) you can't have -i without backups...

    I have picked up some things here and there, but would like better explanations on what the options like -pie, pack and alike do and what they are for.
    The answer to all of these question is out there, in the docs: please check
    perldoc pelrun
    perldoc -f pack
    perldoc perlpacktut
    

    Please note that in certain environments the kind of answer such a question would get could be at best an educated RTFM.

    However I would like to point out that while indeed -pie has to do with one-liners, pack() is yet another Perl function, so you can use it in a one-liner, granted; but I don't see how it can be strictly related to your question (however I hope the tutorial above will help you).

    In any case -pie is just the same as -p -i -e: -p adds a loop around your program, that is given on the cmd line itself by means of -e and finally -i does inline editing. You can check by yourself what -p adds to your code:

    $ perl -MO=Deparse, -pe '' LINE: while (defined($_ = <ARGV>)) { (); } continue { print $_; } -e syntax OK

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (17)
As of 2014-07-28 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (200 votes), past polls