SunQuest
 
       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  
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 
IBM developerWorks
 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

    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: 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...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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: 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 1 hosted by Hostway