Click to See Complete Forum and Search --> : How To Save Data to a File using Perl??


Winglys
03-16-2003, 04:47 AM
hi evvery experts, I'm new in perl programming and I don't know how I can save a data to a file. Do I need to create a file before I save data to it or the code will do it for me?

jeffmott
03-16-2003, 11:42 AM
You will have to create the file yourself (if it doesn't exist already) before you can write to it. You should lock it as well to avoid possible race conditions.use Fcntl qw{:flock};

open FH, '>file' or die $!;
flock FH, LOCK_EX or die $!;
print FH 'you data here' or die $!;
close FH or die $!;See open (http://www.perldoc.com/perl5.8.0/pod/func/open.html) for more detailed information.

Nedals
03-16-2003, 12:34 PM
jeff, I'm not trying to be picky, promise :) But when I read though this post and, based on Winglys comments, I just wanted to clarify something.

open FH, '>file' or die $!;

This line creates (or deletes the contents if it already exists) the file. You don't have to create it seperately.