Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Creating files

by Anonymous Monk
on Jan 23, 2000 at 12:05 UTC ( [id://2367]=perlquestion: print w/replies, xml ) Need Help??

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-03-28 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found