Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey I'm new to perl and I need to know how to create a file. All the books I have tell me how to open, close, read, and write to files. But I need to know how to create one.

Replies are listed 'Best First'.
Re: Creating files
by chromatic (Archbishop) on Jan 26, 2000 at 09:17 UTC
    Don't forget to check the return value of your open call. To wit:
    open FILE, ">bob.txt" or die "Could not open file: $!\n";
    $! is a special variable that returns the error. It'll tell you why you couldn't open the file.
Re: Creating files
by Crulx (Monk) on Jan 23, 2000 at 13:20 UTC
    Opening a file for writing that does not exits creates the file. *grin* so
    open FILE, ">bob.txt";
    Crulx
Re: Creating files
by setantae (Scribe) on Jan 28, 2000 at 02:48 UTC
    If you want to check whether the file already exists before you clobber it, then use the -e test thusly:
    $name = "filename"; if (-e $name) { print "File $name already exists.\n"; } else { do stuff; }