Home arrow Perl Programming arrow Page 5 - Perl: Appending and Writing to Files

More Ways to Test Files - Perl

In our last tutorial we left off on the topic of creating files and manipulating the data therein. In this article we will discuss how to append to a file and how to write to a file. If there is time, we will also discuss working with file checks.

TABLE OF CONTENTS:
  1. Perl: Appending and Writing to Files
  2. Writing to Files
  3. Overwriting Without Deleting Everything
  4. How to Delete a File
  5. More Ways to Test Files
By: James Payne
Rating: starstarstarstarstar / 9
November 14, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

There are also ways to test if a file is read, writable, or executable:


$my_file="superhero.txt";

if (-r $my_file)

{

#whatever you want to occur if the file is readable

}


$my_file="superhero.txt";

if (-w $my_file)

{

#whatever you want to occur if the file is writable

}


$my_file="superhero.txt";

if (-x $my_file)

{

#whatever you want to occur if the file is executable

}

And lastly, if you want to test more than one you can use the OR (||), and AND (&&) (ow my brains!) operators.


$my_file="superhero.txt";

if ( (-e $my_file) && (-w $my_file) )

{

#whatever you want to occur if the file exists and is writable

}

Here is a table for a quick reference:

 

File Checks

What it Does

-e

Checks to see if the file exists

-z

Checks to see if a file size is zero

-s

Checks to see if a file size is not zero

-r

Checks to see if a file is readable

-w

Checks to see if a file is writable

-x

Checks to see if a file is executable

-T

Checks to see if a file is a text file

-B

Checks to see if a file is a binary file

&&, ||

And and OR Operators used to check if one criteria or another is true, or if this criteria AND that criteria are true

All right folks. That's it for this section covering Files. In the next series we will cover how to work with Directories and possibly forms, time permitting.

So swing by often.

Till then...



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

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: