Perl
  Home arrow Perl arrow 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? 
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
    ( Page 1 of 5 )

    In our last tutorial we left off on the topic of creating files and manipulating the data therein. In this article we will discuss how to append to a file and how to write to a file. If there is time, we will also discuss working with file checks.

    How to Append to a File

    Sometimes you want to append or add additional information to a file. To do so is simple. Just open up the file with the original data and add however many lines you wish to the end of the page. Let's say we want to some data to our superhero.txt file.

    Here is what it originally looked like:


      The Incredible Hulk|Super Strength|I rip my pants


      Daredevil|Heightened Senses|I have poor fashion sense due to blindness


      Apache Chief|the ability to grow Very Tall|I wear a skirt

    Now are going to add a new row for the superhero I consider to probably be the lamest of all:

      Aqua Man|the ability to communicate with fish|deep fryers and tartar sauce

    Our superhero.txt file should now look like this:

      The Incredible Hulk|Super Strength|I rip my pants

      Daredevil|Heightened Senses|I have poor fashion sense do to blindness

      Apache Chief|the ability to grow Very Tall|I Wear a skirt

      Aqua Man|the ability to communicate with fish|deep fryers and tartar sauce

    Amazing right?

    Of course we can also append data with code, like so:


    $my_file="suphero.txt";

    open(PLOT,">>$my_file") || die("This file will not open!");

    print PLOT "Aqua Man/|the ability to communicate with fish/|deep fryers and tartar saucen";

    close(PLOT);

    I know that the above code looks a little convoluted. You will notice several differences right off the bat. First, when we open the file we use double quotes after the comma, and we have added two greater than characters (>>) to the front of the filename we wish to open. These two greater than symbols tell the program we wish to append data and not read or overwrite data.

    Next we use our buddy the print command, followed by our referencer PLOT, to place the data in our table. As you can see, we use the pipe(|) delimiter again to separate the columns. Before we place the pipes(|) in however, we must first escape using the backslash(/). Finally, we add an n (newline) to the end of the sentence and close our connection.

    We could accomplish the same thing using variables:


    $my_file="superhero.txt";

    $heroname="Aqua Man";

    $heropower="the ability to communicate with fish";

    $heroweakness="deep fryers and tartar sauce";


    open(PLOT,">>$my_file") || die("The file cannot be opened!");

    print DAT "$heroname|$heropower|$heroweaknessn";

    close(PLOT);

    This will have the same affect as our previous code, although it may be a little easier to read and understand.



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

       

    PERL ARTICLES

    - 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
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT