Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Modify file without any temp variable

by tulsyan (Beadle)
on Jul 20, 2005 at 07:07 UTC ( #476384=perlquestion: print w/ replies, xml ) Need Help??
tulsyan has asked for the wisdom of the Perl Monks concerning the following question:

MONKS,

I am new this perl group.

I need ur help. That is, Is it possible to modify the file without using any Temporary variable

use strict; open (JE, "< c:\\tuls\\tuls.txt") || die "Can not open the file "; my $ou; while (<JE>) { $_=~s/findtext/changetext/g; $ou.=$_; } close(JE); open (JE, "> c:\\tuls\\tuls.txt") ; print JE $ou; close (JE);

Comment on Modify file without any temp variable
Download Code
Re: Modify file without any temp variable
by gopalr (Priest) on Jul 20, 2005 at 07:15 UTC

    Hi

    use strict; my $ou; open(JE, "+<c:\\tuls\\tuls.txt"); while(<JE>) { $_=~s/findtext/changetext/g; $ou.=$_; } seek(JE, 0, 0); print JE $ou; close JE
      This is bad ! If the new text is shorter than the old one, the file is not truncated, and you happen to have some extra dirt at the end of the file ! (took me quite some time to figure it out)

      P!

      Are the monkeys gone yet?
Re: Modify file without any temp variable
by GrandFather (Cardinal) on Jul 20, 2005 at 07:22 UTC

    Your version works fine. The following is a little more succinct:

    use strict; use warnings; my $fileName = "test.txt"; open (JE, "+< $fileName") || die "Can not open the file "; my $ou = join "", <JE>; $ou=~s/findtext/changetext/gs; seek JE, 0, 0; print JE $ou; close (JE);

    Perl is Huffman encoded by design.

      Can you tell me about the seek

        seek = Sets FILEHANDLE's position

        Go thorough seek

Re: Modify file without any temp variable
by rev_1318 (Chaplain) on Jul 20, 2005 at 07:48 UTC
    You can use the in-place edit option of Perl (see the -i option in perlrun):
    { local $^I = ''; #no backup of original! local @ARGV = qw(c:\\tuls\\tuls.txt); while ( <ARGV> ) { s/find/replace/g; print; } }

    Paul

      I think you will find that that generates a temporary file, although not an explicit one. It is an option OP should look at though.


      Perl is Huffman encoded by design.
Re: Modify file without any temp variable
by Llew_Llaw_Gyffes (Beadle) on Jul 20, 2005 at 20:23 UTC

    Here's another approach:

    my @orig = ($/, $\); undef($/); undef($\); open (JE, $file) || die "Cannot open $file"; (my $j = <JE>) =~ s/orig/new/g;

    At this point you can either:

    close (JE); open (JE, "> $file); print JE $j; close (JE); ($/, $\) = @orig;

    or, more efficiently:

    seek (JE, 0, 0); print JE $j; close (JE); ($/, $\) = @orig;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-11-27 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (181 votes), past polls