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

       

    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 5 Hosted by Hostway
    Stay green...Green IT