Perl
  Home arrow Perl arrow Page 4 - Perl: Appending and Writing to Files
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl: Appending and Writing to Files
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-11-14


    Table of Contents:
  • Perl: Appending and Writing to Files
  • Writing to Files
  • Overwriting Without Deleting Everything
  • How to Delete a File
  • More Ways to Test Files

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl: Appending and Writing to Files - How to Delete a File
    ( Page 4 of 5 )

    We can use the following command to delete the file:


    unlink("superhero.txt");

    (Note: remember, if you did not put it in the same directory as the code you are writing, you must add the location of the file).

    You can do the same thing with variables:


    $my_file="superhero.txt";

    unlink($my_file);

    And voila! Your file is deleted. Of course if you need it later you are screwed, so it is always a good idea to make a backup somewhere in case you need it later.

    How To Check a File

    There are several ways to test files within Perl. The funniest to me is the Check Existence, which literally checks to see if a file exists. I guess not everyone can have a super human brain like you and me. 

    Does it Exist?!?

    Here is the code to see if a file exists:


    if (-e "superhero.txt")

    {

    #whatever you want to occur if the file exists

    }

    The -e tells the code to see if the preceding file name exists. You can do the same thing with a variable too of course.


    $my_file="superhero.txt";

    if (-e $my_file)

    {

    #whatever you want to occur if the file exists

    }

    You can also see if a file exists by measuring the size of the file. For that you would replace -e with -z or -s (z = zero sized file, s= more than zero).


    $my_file="superhero.txt";

    if (-z $my_file)

    {

    #whatever you want to occur if the file is zero sized

    }

    or

     

    $my_file="superhero.txt";

    if (-s $my_file)

    {

    #whatever you want to occur if the file is greater than 0 in size

    }

    You can of course also test if the file has Text or Binary data:


    $my_file="superhero.txt";

    if (-T $my_file)

    {

    #whatever you want to occur if the file is a text file

    }

    and


     

    $my_file="superhero.txt";

    if (-B $my_file)

    {

    #whatever you want to occur if the file is a binary file

    }



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek