|
| ||||||||||
| Tags: file, perl |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| perl file handling question
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. |
|
#2
| ||||
| ||||
| Re: perl file handling question
Please try this: Code: open MYfile ">pathname"; # will erase the contents if already exists open MYfile ">>pathname"; # won't erase the contents if already exists |
|
#3
| |||
| |||
| Re: perl file handling question
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 $@;
} |
|
#4
| |||
| |||
| Re: perl file handling question
This is the code to empty the file before updating the file: Code: open HANDLE,">","/path/to/file" or die "Cannot overwrite file: $!"; print HANDLE "Whatever"; close HANDLE; Code: perldoc -f truncate Code: open IN, $file; #do what you need to do close IN; open OUT ">$file"; #print out the data close OUT; |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "perl file handling question" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is File Handling in Visual Basic 6.0? | Dėfrim | Software Development | 4 | 27-12-2010 04:54 AM |
| Is File handling in Java Swing possible | Vivan | Software Development | 5 | 20-07-2010 11:51 PM |
| Handling basic text file using Perl | Amie | Software Development | 3 | 06-11-2009 10:37 PM |
| How to Parse CSV file using PERL | Xena | Software Development | 3 | 31-08-2009 10:38 AM |
| File Handling in C# | GaryK | Software Development | 1 | 10-11-2008 04:31 PM |