Reputation: 1821
I need to open a file in append mode in Perl, but I need that before opening file all the data is deleted and fresh data goes in.
I will be entering data line by line, so before entering the first line I need all previous data is deleted.
Please help.
Upvotes: 15
Views: 19703
Reputation: 1342
File handling includes:
<
)>>
)>
)For detailed explanation please visit this link.
Upvotes: 2
Reputation: 67900
I think you are confused about what "append" means in perl. What you are describing is opening a file and truncating it, i.e.:
open my $fh, '>', $file;
This will delete the contents of $file
and open a new file with the same name.
The reason to use open for appending is when you have a file that you do not wish to overwrite. I.e. the difference between >
and >>
is simply that the former truncates the existing file and begins writing at the start of the file, and the latter skips to the end of the existing file and starts writing there.
Documentation here
Upvotes: 18