Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Empty File Contnts before writing

by gautamparimoo (Beadle)
on Feb 19, 2013 at 07:10 UTC ( #1019467=perlquestion: print w/ replies, xml ) Need Help??
gautamparimoo has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I want empty the file contents before writing into it again.The following is my code:

open(FILE1 , ">" ,"testing1.txt"); print FILE1 "stat"; #Do the required actions print FILE1 "stat-updated"; close(FILE1);

The output file contents will be:

stat stat-updated

But I want the output file to contain just :stat-updated.How to do it?? Do I need to close the filehandle or call truncate or something else??

Comment on Empty File Contnts before writing
Download Code
Re: Empty File Contents before writing
by Athanasius (Monsignor) on Feb 19, 2013 at 07:44 UTC

    Yes, closing the filehandle and re-opening for writing with ">" will truncate the file. See open and perlopentut.

    Note that you should test open’s return value. Either

    use autodie;

    or write

    open(my $file1, ">", "testing1.txt") or die "Cannot open file 'testing +1.txt' for writing: $!";

    Also note that a lexical variable (as shown) is preferable to a bareword for the filehandle.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Empty File Contnts before writing
by tobyink (Abbot) on Feb 19, 2013 at 07:46 UTC

    The function you seek is... seek

    use strict; use warnings; open my $fh, ">", "testing1.txt"; print $fh "stat"; seek $fh, 0, 0; print $fh "stat-updated"; close $fh; system cat => "testing1.txt";
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Yes, seek works in this case because the final output is longer than the initial output. But a word of caution is in order. Consider:

      #! perl use strict; use warnings; my $filename = 'test.txt'; open(my $fh, '>', $filename) or die "Cannot open file '$filename' for +writing: $!"; print $fh "The quick brown fox jumped over the unfortunate dog."; seek $fh, 0, 0; print $fh "The final output."; close($fh) or die "Cannot close file '$filename': $!";

      This results in file “test.txt” containing:

      The final output.ox jumped over the unfortunate dog.

      as seeking to the beginning of the file does not erase its existing contents.

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        So I will have to open and close the file agin in order to completely truncate watever is written in previous write attempts?

        Yeah, gotta watch out for those output-oxen. ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-12-22 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls