Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



perl file handling question

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 18-06-2009
Member
 
Join Date: May 2008
Posts: 56
perl file handling question
  

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.

Reply With Quote
  #2  
Old 18-06-2009
Member
 
Join Date: May 2008
Posts: 30
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
The file will be created with 0666 permission
Reply With Quote
  #3  
Old 18-06-2009
Member
 
Join Date: Jan 2009
Posts: 40
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 $@;
}
Reply With Quote
  #4  
Old 18-06-2009
Member
 
Join Date: May 2008
Posts: 38
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;
OR
Code:
perldoc -f truncate
To open existing file & rewrite
Code:
open IN, $file;
#do what you need to do
close IN;

open OUT ">$file";
#print out the data
close OUT;
For perl file Handling
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 02:59 PM.