Perl
  Home arrow Perl arrow Page 2 - File Tests in Perl
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

File Tests in Perl
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2007-05-10

    Table of Contents:
  • File Tests in Perl
  • File Test Operators
  • The stat and lstat Functions
  • The localtime Function
  • Bitwise Operators
  • Using the Special Underscore Filehandle

  • 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

    File Tests in Perl - File Test Operators


    (Page 2 of 6 )

    Don’t worry if you don’t know what some of the other file tests mean—if you’ve never heard of them, you won’t be needing them. But if you’re curious, get a good book about programming for Unix. (On non-Unix systems, these tests all try to give results analogous to what they do on Unix, or give undef for an unavailable feature. Usually, you’ll be able to guess what they’ll do.)

    If you omit the filename or filehandle parameter to a file test (that is, if you have-ror just-s), the default operand is the file named in$_.* So, to test a list of filenames to see which ones are readable, you type the following:

      foreach (@lots_of_filenames) {
       
    print "$_ is readable\n" if -r; # same as -r $_
      }

    But if you omit the parameter, be careful that whatever follows the file test doesn’t look like it could be a parameter. For example, if you wanted to find out the size of a file in KB rather than in bytes, you might be tempted to divide the result of-sby1000(or1024), like this:

      # The filename is in $_
      my $size_in_K = -s / 1000; # Oops!

    When the Perl parser sees the slash, it doesn’t think about division. Since it’s looking for the optional operand for-s, it sees what looks like the start of a regular expression in forward slashes. To prevent this confusion, put parentheses around the file test:

      my $size_in_k = (-s) / 1024; # Uses $_ by default

    Explicitly giving a file test a parameter is safer.

    More Perl Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning Perl, Fourth Edition," published...
     

    Buy this book now. This article is excerpted from chapter 11 of the book Learning Perl, Fourth Edition, written by Randal L. Schwartz, Tom Phoenix and brian d foy. Check it out today at your favorite bookstore. Buy this book now.

       

    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 6 hosted by Hostway