SunQuest
 
       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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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? 
PERL

Perl: Appending and Writing to Files
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · > we must first escape using the backslash(/).Do you mean '\' ? -pamag
       · Hey thanks for catching that pamag; I did indeed intend for that to read:...
     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway