[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] How to modify a text file



At 10.09 -0400 1999.06.08, Ingo Bitsch wrote:
>Another newbie-question:
>
>What is the standard way of modifying a text file with Perl? I
>experimented a bit with all the modifiers in front of the filename in the
>"open"-function, but I always ended up opening the file TWICE:
>
>open (IN, "file.txt");
>@all_lines=<IN>;
>$_ = join "",@all_lines;
>
>#(Do some operations on $_ here)
>
>open (OUT, ">file.txt");
>print OUT;
>
>I can't believe that there is no other way of doing it.

There are, but this is the way you normally should do it.  It is safe and
easy, compared to other methods.  Just do a close(IN) nefore you open the
same file again.


>Another thing
>that makes me nervous about my approach is that there is a moment in the
>script (after the second "open") when the contents of my file is alrady
>erased and lives only in the $_-Variable...

There's no way around that in any event.  If you are worried about safety,
make a backup first.

  use File::Copy;
  copy('file.txt', 'file.txt.bak') or die $!;

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org