Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: read from a file and write into the same file

by systems (Pilgrim)
on Mar 03, 2008 at 10:01 UTC ( #671623=note: print w/ replies, xml ) Need Help??


in reply to read from a file and write into the same file

Remember you can also use perl to edit files in-place.

> perl -p -i.bak -e "s/(.*)/$1\n****************/" test.txt
The above command will append a new line containing **************** after every line in the document.

Now I have a question for the more experienced Perlers, can I do in-place file substitution from inside a script using just Perl code. Or do I have to execute as a command line? I know I can execute a command line within a Perl script, but what I mean, can I do it otherwise, without going to the command line in any way?


Comment on Re: read from a file and write into the same file
Download Code
Re^2: read from a file and write into the same file
by stiller (Friar) on Mar 03, 2008 at 10:31 UTC
    It's not entierly clear to me what you are asking, but take a look at Tie::File, it might be what you are looking for.
Re^2: read from a file and write into the same file
by olus (Curate) on Mar 03, 2008 at 10:54 UTC

    You can do inplace editing from inside scripts with $^I, which is the equivalent for the -i command line option. It is described in perlvar.

Re^2: read from a file and write into the same file
by almut (Canon) on Mar 03, 2008 at 13:03 UTC
    ... can I do in-place file substitution from inside a script

    If using the feature behind the -i option from within a script is what you mean... yes, you can. You can either put the -i.bak on the shebang line, or set the corresponding $^I special variable (as olus already mentioned). The latter allows a little more flexibility, e.g. you could localise the effects

    #!/usr/bin/perl use strict; use warnings; sub edit_inplace { local @ARGV = @_; local $^I = ".bak"; # local $^I = ""; # no backup file while (<>) { s/(.*)/$1\n****************/; print $_; } } edit_inplace("test.txt");

    BTW, even Perl doesn't do a real in-place edit (i.e. modify the physically-same file). Rather, it opens, then renames (or unlinks, when no backup extension is specified) the original file, then opens a new file with the same name — at least on Unix, where opened files continue to be accessible, even when they're no longer associated with a directory entry  (look at the inode numbers before and after to verify, e.g. with "ls -li").

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (15)
As of 2015-07-02 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (40 votes), past polls