Results 1 to 4 of 4

Thread: perl file handling question

  1. #1
    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.

  2. #2
    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

  3. #3
    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 $@;
    }

  4. #4
    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

Similar Threads

  1. What is File Handling in Visual Basic 6.0?
    By Dfrim in forum Software Development
    Replies: 4
    Last Post: 27-12-2010, 04:54 AM
  2. Is File handling in Java Swing possible
    By Vivan in forum Software Development
    Replies: 5
    Last Post: 20-07-2010, 11:51 PM
  3. Handling basic text file using Perl
    By Amie in forum Software Development
    Replies: 3
    Last Post: 06-11-2009, 10:37 PM
  4. How to Parse CSV file using PERL
    By Xena in forum Software Development
    Replies: 3
    Last Post: 31-08-2009, 10:38 AM
  5. File Handling in C#
    By GaryK in forum Software Development
    Replies: 1
    Last Post: 10-11-2008, 04:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •