Home arrow Perl Programming arrow Page 6 - Perl 101 (Part 4) - Mind Games

Testing Times - Perl

This week's article teaches you how to use Perl to interact withfiles on your system, and also provides you with a quick crash course invarious array functions.

TABLE OF CONTENTS:
  1. Perl 101 (Part 4) - Mind Games
  2. Handle With Care
  3. Different Strokes
  4. A Little Brainwashing
  5. Die! Die! Die!
  6. Testing Times
  7. Popguns And Pushpins
  8. Shifting Things Around
  9. The Real World
  10. Miscellaneous Stuff
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 11
June 29, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: