Perl
  Home arrow Perl arrow Page 2 - 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 Open a File
    ( Page 2 of 5 )

    Before you can read the script, you have to open it. Unless you are Superman and have X-ray vision. And even then you might want to lay off the x-ray vision; get it mixed up with your laser eye beams and you can say good-by to that Super Hero pension check.

    The syntax for opening a file is: open(HANDLE, “FileName/Location”); Since we are saving our file and script in the same directory, we don't need to worry about location in this instance. Handle is used to reference the file when you read it and close it. Here is a sample of how it should look:


    open(PLOT, “super.txt”);

    You can also store the file name as a variable if you like, so that you can change it more easily in the future:


    $my_file=”super.txt”;

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

    The above code does several things. First, it creates a variable named $my_file and adds the value “super text” to it. Then we open the file, referencing it with the newly created variable. Lastly, I added an OR to the program, that states either load the program, OR create a pop-up alert that tells the user: I refuse to open your file! We do this to alert the user in case there is a problem opening the file.



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