Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

perl's command line snippet which I read from a perl article ( perl tutrous )

by jesuashok (Curate)
on Sep 06, 2006 at 07:16 UTC ( #571385=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

fellow monks,

$ ls -l foo* -rw-rw-r-- 1 metrica metrica 0 Sep 6 08:02 foo $ perl -pi~ -e '' foo $ ls -l foo* -rw-rw-r-- 1 metrica metrica 0 Sep 6 08:02 foo -rw-rw-r-- 1 metrica metrica 0 Sep 6 08:02 foo~
I am using perl version 5.
$ perl -pi~ -e '' foo
what did the above line do ?
why it has created a file called 'foo~' ?
am I making any mistakes there ?

"Keep pouring your ideas"

Comment on perl's command line snippet which I read from a perl article ( perl tutrous )
Select or Download Code
Re: perl's command line snippet which I read from a perl article ( perl tutrous )
by Corion (Pope) on Sep 06, 2006 at 07:18 UTC

    Read perlrun or perldoc perlrun, especially about the -i switch.

Re: perl's command line snippet which I read from a perl article ( perl tutrous )
by Hue-Bond (Priest) on Sep 06, 2006 at 07:23 UTC

    -i tells Perl that the files read with the diamond operator (<>) are going to be edited in place, and that a backup of the original file will be kept, using the extension specified. So, -i~ will create a copy of the file with a tilde appended to its name.

    But, where's the <> coming from? It's from -p, which causes the program to be enclosed into a loop that does a while(<>).

    Whether you committed a mistake or not, depends on what you intended to do ;^).

    --
    David Serrano

Re: perl's command line snippet which I read from a perl article ( perl tutrous )
by davido (Archbishop) on Sep 06, 2006 at 07:27 UTC

    Ok, this isn't that tricky if you disect the command line switches one at a time:

    -e

    This means essentially "execute the following code". As you can see, in this particular case, the following code is nothing at all; zilch.

    -p

    This means wrap the -e code in the following loop:

    while( <> ) { # -e code goes here (nothing, in this case) print $_; }

    Now for the -i: That means do an in-place edit. Whatever characters immediately follow -i will be appended to the input file's name for the purpose of creating a backup file. Often you'll see -i.bak which means take file foo and create a backup named foo.bak. Well, in your case, instead of .bak you're using ~, so it creates a backup named foo~.

    Now put it all together:

    1. -i~ Do an in-place edit, but first create a backup file named "inputfile~" (or in your case, foo~).
    2. -p loop over the input file (foo), and after executing the -e code, print the line to the output file (which is the same filename as the original input file, because you're doing in-place editing thanks to -i
    3. -e Execute the following code within the while loop: '' (in other words, do nothing inside the loop except the -p loop's default; print.

    I hope this helps, but if it doesn't, see perlrun for more detail. Also (shameless plug), I wrote a node awhile back that went into greater detail on the subject of learning how to compose Perl one liners. The node is here: Re: One Liners. I think you'll find it to be a pretty easy introduction to Perl's command line switches and Perl one-liners. Good luck!


    Dave

Re: perl's command line snippet which I read from a perl article ( perl tutrous )
by cog (Parson) on Sep 06, 2006 at 07:28 UTC
Re: perl's command line snippet which I read from a perl article ( perl tutrous )
by davorg (Chancellor) on Sep 06, 2006 at 08:43 UTC
Re: perl's command line snippet which I read from a perl article ( perl tutrous )
by merlyn (Sage) on Sep 06, 2006 at 14:47 UTC
    I have been known to use  perl -pi-DIST -e0 file1 file2 file3.... as a "poor man's change control", to save the "distribution" versions of a Makefile or config file as "Makefile-DIST" and so on. The nice thing about the in-place "non-edit" is that permissions and ownership are retained automatically.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-09-18 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (120 votes), past polls