PeiYu Zeng wrote:
> Hello,
Hello,
> Can I modify the contents of a file, without creating a new one?
Yes, but only if you do not change the length of the data (i.e. fixed length
records.)
> Now, the method that I modify the contents of a file is:
> open( READHANDLE , "sourceFile" );
> open( WRITEHANDLE, ">destiFile" );
You should *always* verify that the file is opened correctly.
open READHANDLE , '<', 'sourceFile' or die "Cannot open 'sourceFile' $!";
open WRITEHANDLE, '>', 'destiFile' or die "Cannot open 'destiFile' $!";
> foreach my $line (<READHANDLE>) {
foreach reads the entire file into a list in memory. You should probably use
a while loop instead.
> if it accords with my rules {
> modify $line;
> print(WRITEHANDLE $line);
> }
> }
>
> close(...);
>
> Is there a method that I could not create a new file to store the modified
> informations?
If the modifications you make do not change the length of the data, and you
are on a system that allows it (Unix/Linux), you can open the same file twice,
once to read and again to write.
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/