Perl
  Home arrow Perl arrow Perl: Working with 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: Working with Files
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-11-07

    Table of Contents:
  • Perl: Working with Files
  • How to Open a File
  • How to Read a File
  • Manipulating Data
  • How the Code Works

  • 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Perl: Working with Files
    (Page 1 of 5 )

    In our last series of tutorials we worked with Conditionals and Loops to create some basic Perl programs. This time around we are going to be working with files. Text files, CGI files, PL files, boiled files, fried files, Files Benedict, steamed Files. Okay, so I was kidding about the steamed Files.

    How to Create a File

    Creating a file to read from in Perl is pretty simple. Simply open up a .txt file and enter the following data:

      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

    When you enter the data, note a few things. First, there are no headers here. Second, we separate each column of data with a pipe(|) symbol. And third, each line of data is separated by pressing the Enter key. Note that nothing should appear on the blank lines, not even a space. This is because when Perl reads it, it will read that line as a new row of data. 

    When you finish entering the data, save the file. You can leave it as a .txt file if you like. The only downside to this is that the data will be visible to any users who happen upon your page. If you want to limit visibility of the data, you can save it as a .cgi or .pl file, depending upon your host. For simplicity's sake, save the file (use the name super.txt) in the same directory as you are saving your script. If you don't, you will have to point to the directory where your file is being saved.

    At this point we could set permissions, but I will save that for another time. Setting permissions allows you to choose whether the file can only be read, can be edited, neither, or both.

    More Perl Articles
    More By James Payne


       · Unfortunately this article is really superficial, anybody can learn much more by...
       · It looks like the code has been altered by whatever software is being used to...
       · Hey, thanks for the comment. To be honest with you, this is a beginner's series,...
       · Echoing the comment about the missing slashes...I can't get the 'Superhero' program...
       · Hi Tom,Did you copy and paste the code into your editor? If so, that might be...
     

       

    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