Hi,
I have started with Perl just a few days ago.
I want to know 2 things about perl file handlling.
1. How to recognize an empty file in perl ?
2. How to create a new empty file in perl?
Thanks in Advance.
Hi,
I have started with Perl just a few days ago.
I want to know 2 things about perl file handlling.
1. How to recognize an empty file in perl ?
2. How to create a new empty file in perl?
Thanks in Advance.
Please try this:
The file will be created with 0666 permissionCode:open MYfile ">pathname"; # will erase the contents if already exists open MYfile ">>pathname"; # won't erase the contents if already exists
This is what you are looking for:
Code:#! /usr/bin/perl use strict; use warnings; my $r = create_empty_file('inane'); die $r if $r; sub create_empty_file { eval { open my $fh, '>', $_[0] or die "Cannot create $_[0]: $!\n"; close $fh or die "Cannot close $_[0]: $!\n"; }; return $@; }
This is the code to empty the file before updating the file:
ORCode:open HANDLE,">","/path/to/file" or die "Cannot overwrite file: $!"; print HANDLE "Whatever"; close HANDLE;
To open existing file & rewriteCode:perldoc -f truncate
For perl file HandlingCode:open IN, $file; #do what you need to do close IN; open OUT ">$file"; #print out the data close OUT;
Bookmarks