Perl
  Home arrow Perl arrow Page 5 - Perl: Working with 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: Working with Files
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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: Working with Files - How the Code Works
    ( Page 5 of 5 )

    First we create a variable named $my_file to hold the name of our file, and assign it the value super.txt. Next we open the file, read from it, and close it, storing the data in an array called @my_data. Next we create some simple HTML headers so it will print to a web page, and then we go on to create a foreach loop, that will loop though the data in our array. We create a new variable named $superhero which will hold each line of data in our file (it only holds one line at a time; as it passes through the loop each time a new line replaces the old). Next, we use the chop method to cut off our Enter Key data (we don't want this in our variable, or it will mess up our print out).

    Next we create three variables to hold the three columns that reside within our data file. Then to assign a value to these variables (again, the data in the variables changes to the next line on each pass through the loop), we use the Split Method. If you recall from before we used the pipe(|) symbol to separate the columns in our data file. Now we are going to use the same separator to split up our data. Since the pipe(|) character is a special character in Perl, it must be escaped with a backslash(), hence the weird code:/|/ .

    Finally we use the data in our newly created variable to print out text to the browser. And of course we finish by closing out our HTML tags.

    Well, you should now have a good understanding of how to create files, open files, read them, store their data into variables, close them, and manipulate that data. In our next tutorial we will discuss how to append to files, and how to write to files when you get lonely and miss them. So check back often.

    Till then...



     
     
    >>> 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
    Stay green...Green IT