Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: In-place file manipulation

by halley (Prior)
on Apr 30, 2003 at 15:53 UTC ( [id://254348]=note: print w/replies, xml ) Need Help??


in reply to In-place file manipulation

The (-i) switch is so easy for adjusting TEXT files. I use this so often, that in my login script, I set an alias for pie=perl -p -i~ -e. Easy as pie.

    pie s/2002/2003/g *.html

However, -i really doesn't edit "in place," but it does a good simulation of it. It creates a new file and moves the old file to a backup extension. You may have problems with access control lists (ACLs) on some network file system implementations. Experiment.

The '+' modes for the open() function are designed for fixed-record DATA updating. Note both examples in (perldoc -f open) refer to database files. Not that you couldn't use the functions for text files, but it would be a horrible and unnecessary tangle of seek(), tell() and caching to avoid overwriting parts of the next line before it is read. Not to mention further complications for newline conversion entanglement.

I wish the docs were more clear about this, to avoid this recurring question.

--
[ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://254348]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-25 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found