how to write text to a file

Discussion in 'Perl Misc' started by Mark Tarver, Dec 17, 2006.

  1. Mark Tarver

    Mark Tarver Guest

    How to you append text to a file in Perl?

    Mark
     
    Mark Tarver, Dec 17, 2006
    #1
    1. Advertising

  2. Mark Tarver

    Paul Lalli Guest

    Mark Tarver wrote:
    > 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
     
    Paul Lalli, Dec 17, 2006
    #2
    1. Advertising

  3. Mark Tarver

    Uri Guttman Guest

    >>>>> "PL" == Paul Lalli <> writes:

    PL> Mark Tarver wrote:
    >> How to you append text to a file in Perl?


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 17, 2006
    #3
  4. Mark Tarver

    Mark Tarver Guest

    Mark Tarver wrote:
    > How to you append text to a file in Perl?
    >
    > Mark


    Thanks for all this. I've got a working version now.

    Mark
     
    Mark Tarver, Dec 17, 2006
    #4
  5. Mark Tarver

    Marc Espie Guest

    In article <em486a$8v$-halle.de>,
    Mirco Wahab <> wrote:
    > 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.
     
    Marc Espie, Dec 17, 2006
    #5
  6. "Uri Guttman" <> wrote in message
    news:...
    >>>>>> "MT" == Mark Tarver <> writes:

    >
    > MT> Mark Tarver wrote:
    > >> How to you append text to a file in Perl?
    > >>
    > >> Mark

    >
    > 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
     
    Arved Sandstrom, Dec 18, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flamesrock
    Replies:
    5
    Views:
    579
    Philippe C. Martin
    May 4, 2005
  2. Replies:
    7
    Views:
    389
    Mick White
    Feb 2, 2005
  3. acord
    Replies:
    3
    Views:
    118
    Evertjan.
    Mar 24, 2006
  4. Iulian Ilea
    Replies:
    1
    Views:
    326
    pcx99
    Dec 21, 2006
  5. Tim Chase
    Replies:
    0
    Views:
    102
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page