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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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


    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


       · 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

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

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT