Posted by Matthias Carlsson on 1999-03-30
>
> Hi. How to write a program that can read/write data file in Perl ?
>
> Regards,
> Kelly
>
To open and read a file in Perl you can use the following code ('m not
sure if it's the best way to do it, but this is how I do it anyway):
open(FILE,"/path/to/file");
@lines = <FILE>;
close(FILE);
Every line is now stored in @lines. If you want to open a file for
writing, you just add a ">" like this:
open(FILE,">/path/to/anotherfile");
foreach $line (@lines) {
print FILE "$line\n";
}
close(FILE);
Note that the above code will replace everything in the file. If you
rather want to add something at the bottom, use ">>" instead of ">".
Hope this helps.
// Matt
Previous post | Next post | Timeline | Home