Perl
  Home arrow Perl arrow Page 6 - Perl 101 (Part 4) - Mind Games
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 
Moblin 
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 101 (Part 4) - Mind Games
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2000-06-29

    Table of Contents:
  • Perl 101 (Part 4) - Mind Games
  • Handle With Care
  • Different Strokes
  • A Little Brainwashing
  • Die! Die! Die!
  • Testing Times
  • Popguns And Pushpins
  • Shifting Things Around
  • The Real World
  • Miscellaneous Stuff

  • 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 101 (Part 4) - Mind Games - Testing Times


    (Page 6 of 10 )

    Perl also comes with a bunch of operators that allow you to test the status of a file - for example, find out whether it exists, whether it's empty, whether it's readable or writable, and whether it's a binary or text file. Of these, the most commonly used operator is the "-e" operator, which is used to test for the existence of a specific file.

    Here's an example which asks the user to enter the path to a file, and then returns a message displaying whether or not the file exists:
    #!/usr/bin/perl
    print "Enter path to file: ";
    $path = <STDIN>;
    chomp $path;
    if (-e $path)
    {
    print "File exists!\n";
    }
    else
    {
    print "File does not exist!\n";
    }

    There are many more operators - here's a list of the most useful ones, together with an example which builds on the one above to provide more information on the file specified by the user.
    OPERATOR: TESTS WHETHER:
    -----------------------------
    -z File exists and is zero bytes in size
    -s File exists and is non-zero bytes in size
    -r File is readable
    -w File is writable
    -x File is executable
    -T File is text
    -B File is binary

    And here's a script that demonstrates how you could use these operators to obtain information on any file on your system:
    #!/usr/bin/perl
    # ask for file path and process it
    print "Enter path to file: ";
    $path = <STDIN>;
    chomp $path;
    # test for existence
    if (-e $path)
    {
    print "File exists!\n";
    # test for size
    if (-z $path)
    {
    print "File is empty.\n";
    }
    else
    {
    print "File is not empty.\n";
    }
    # test for read access
    if (-r $path)
    {
    print "File is readable.\n";
    }
    else
    {
    print "File is not readable.\n";
    }
    # test for write access
    if (-w $path)
    {
    print "File is writable.\n";
    }
    else
    {
    print "File is not writable.\n";
    }
    # test for executable bit
    if (-x $path)
    {
    print "File is executable.\n";
    }
    else
    {
    print "File is not executable.\n";
    }
    # test for whether file is text or binary
    if (-T $path)
    {
    print "File is a text file.\n";
    }
    elsif (-B $path)
    {
    print "File is a binary file.\n";
    }
    }
    else
    {
    print "File does not exist!\n";
    }

    And here's what it might look like:
    Enter path to file: /usr/bin/mc
    File exists!
    File is not empty.
    File is readable.
    File is not writable.
    File is executable.
    File is a binary file.



    This article copyright Melonfire 2000. All rights reserved.

    More Perl Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    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