Perl
  Home arrow Perl arrow Perl: Appending and Writing to Files
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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
    (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


       · > 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: 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
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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