- Perl ›
- File operation ›
- here
Create a file
Create a file. Open the file in write mode. Perl doesn't have a function just to create a file. To create the file, use the write mode of open function.
open my $fh, ">", $file;
Example
This is an example to create a file.
use strict;
use warnings;
# Create a file. open my $fh, ">", $file
print "1: Create a file.";
my $file = "example_20080519_ $$. txt"; # $$is the process ID
if (-e $file) {
die "$file already exists.";
}
else {
open my $fh, ">", $file
or die "Unable to open $file in write mode .:$!";
close $fh;
}
Perl ABC