Accessing The File System With Perl

 

Perl provides support to create, write to, append to, delete, and read from files on your web server.

 

Create an empty file:

 

open(FILEHANDLE, “>/path/to/my/file.txt”) || die(‘cannot open file: ‘ . $!);

close(FILEHANDLE);

 

The greater than sign before the path to the file we’re working with tells perl what mode to open the file in.  A greater than sign “>” means open in write mode, erasing any previous contents of the file.  Two greater than signs mean open in append mode, meaning add our new data to the file after the data that already existed.  This is really useful when trying to log certain things your program does, or when you need to keep adding any kind of data to a file and don’t want the previous data erased.  No greater than signs means we are going to read from the opened file.

 

You have to know the path to the file you’re opening however in order to work with it.  You can almost always get the path to your document root (where your web server serves its initial index.html file from) by using this syntax:

 

my $document_root = $ENV{‘DOCUMENT_ROOT’};

 

This reads the hash of environment variables that is available to all cgi scripts to give you your ‘root path’ for your hosting account.

 

The FILEHANDLE above is exactly that, a “file handle”.  It’s how we reference the file we are working with.  You should always call close on files you don’t need to work with any more, by calling the “close” function, and passing the name of the file handle you’ve previously created to it.

 

The code:

 

|| die(‘cannot open file: ‘ . $!);

 

is very useful to us.  The die function prints an error message if what we tried to do didn’t succeed.  The $! Variable is a special perl variable which contains the last error the perl program had before it exited.

 

Writing data to a file (in write mode – existing data in file is erased):

 

open(FILEHANDLE, “>/path/to/my/file.txt”) || die(‘cannot open file: ‘ . $!);

print FILEHANDLE ‘some data is now in this file, and if any data was already in the file, it is now gone.’;

close(FILEHANDLE);

 

Writing data to a file (in append mode – existing data in file is added to):

 

open(FILEHANDLE, “>>/path/to/my/file.txt”) || die(‘cannot open file: ‘ . $!);

Print FILEHANDLE ‘some new data is now in this file, as well as the data that was already there’;

close(FILEHANDLE);

 

Reading Data From A File:

 

open(FILEHANDLE, “/path/to/my/file.txt”) || die(‘cannot open file: ‘ . $!);

# this will only print the first line of the file:

print $_;

# this will print all lines of data in the file:

while(<FILEHANDLE>)

{

  print $_;

}

close(FILEHANDLE);

 

$_ is a special variable in perl that means ‘the current line we’re on in a file’.

 


<-- Previous: Basic Perl Syntax Guide � Control Structures and Conditional Statements | Next: What is CGI, and what do I need to know about it to program in it? -->


Please rate this cgi tutorial on cgi-resources.com:

CGI-Resources Rating:


Copy the Shrug Emoji
Ecommerce Shopping Cart Software
ShopCMS Paypal Shopping Cart
ShopCMS Paypal Shopping Cart
Free CGI Scripts
CGI Tutorial
Software Engineering Consultant
Search Engine Optimization Tips
How To Choose Quality Web Hosting
Free Search Engine Ranking Software
HTTP Compression
Install CGI Scripts
Tell A Friend Script
LittleFTP Free FTP Client For Windows


Link To
This Page!


Copy the following code and paste it into your html file.
Ecommerce Shopping Cart Software | ShopCMS Paypal Shopping Cart | Software Engineering Consultant | Free Search Engine Ranking Software | HTTP Compression | Install CGI Scripts | Search Engine Optimization Tips | CGI Tutorial | CGI Scripts | How To Choose Quality Web Hosting | Tell A Friend Script