Is there a reason to use IO::File instead of open $fh,'>>',$filename;?
use strict; sub test { my $filename = 'test2.txt'; if (-f $filename) { print "File Exists!"; } else { print "File does not exist"; } } sub openFile { open my $fh, '>>', 'test2.txt'; print "file open!\n"; print $fh "The stuff I wanted to add to the end of the file.\n"; } test(); openFile(); test();
Output:
$: perl foo.pl File does not exist file open File Exists! $: cat test2.txt The stuff I wanted to add to the end of the file. $: perl foo.pl File Exists! file open File Exists! $: cat test2.txt The stuff I wanted to add to the end of the file. The stuff I wanted to add to the end of the file. $:
In reply to Re: Check if a file exists and if not create it
by GotToBTru
in thread Check if a file exists and if not create it
by Wroof
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |