SunQuest
 
       Perl
  Home arrow Perl arrow Page 3 - 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 
VeriSign Whitepapers 
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

    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 to Read a File


    (Page 3 of 5 )

    Now that we have opened our file, we can read from it. One way to do this is to assign the data within the file to an array. Behold!


    $my_file=”super.txt”;

    open(PLOT, $my_file) || die(“I refuse to open your file!”);

    @my_data=<PLOT>;

    In the example above, we again create a variable named $my_file and store the name of our file within it (super.txt). Next, we open the file using the word PLOT as our Handle, and using the newly created variable. We add the OR and the DIE command in case the file encounters a problem while loading. Lastly, we create an array named @my_data to store the data of our file, super.txt. Note that we reference our file with the PLOT handle.

    How to Close a File

    After we read the data we will want to close the file. To do so, we simply add close(Plot); to our code, like so:


    $my_file=”super.txt”;

    open(PLOT, $my_file) || die(“I refuse to open your file!”);

    @my_data=<PLOT>;

    close(PLOT);


    I know what you are thinking: that sure was easy. Well don't get too cocky you slacker. We still haven't learned how to work with that data yet.

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