Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 chilling in the Monastery: (3)
As of 2014-08-31 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls