Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: One Liners by davido
in thread One Liners by lakeTrout

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (8)
    As of 2017-06-28 17:51 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (641 votes). Check out past polls.