how to write text to a file

P

Paul Lalli

Mark said:
How to you append text to a file in Perl?

open my $append_fh, '>>', 'file.txt' or die "Cannot open file.txt for
append: $!";
print $append_fh "This text appears at the bottom of file.txt\n";
close $append_fh;

See also:
perldoc -f open
perldo perlopentut

Paul Lalli
 
U

Uri Guttman

PL> open my $append_fh, '>>', 'file.txt' or die "Cannot open file.txt for
PL> append: $!";
PL> print $append_fh "This text appears at the bottom of file.txt\n";
PL> close $append_fh;

PL> See also:
PL> perldoc -f open
PL> perldo perlopentut

and also File::Slurp has a append_file() sub which is perfect for this.

uri
 
M

Marc Espie

use IO::File;

which some people do consider "more contemporary" ...

Only some... IO::File is heavy, and incurs some real penalty for light-weight
scripts (the overhead of loading File::IO is anything but trivial)

When auto-vivification of file handles became available, I ditched IO::File
almost completely. I very often do not actually need to have any of the OO
properties of IO::File (Plus, the print $fh syntax matches the second way
to call object methods anyways).

Some people may see it as slightly more readable and less funky, and as far
as I know, perl6 will have a clearer syntax than open(my $f, '>', something);
but apart from that, it's just fine and *fast* in 99% of the cases.
 
A

Arved Sandstrom

Uri Guttman said:
MT> Thanks for all this. I've got a working version now.

thanks for your own question? why would you followup to that and not
some answer? kinda silly IMO.

Seems pretty logical if all or most of the answers were helpful. By
selecting a specific answer you're making another statement, that that
answer was especially helpful. And maybe you don't want to leave that
impression either.

AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top