When I'm writing a short program I may have to open a file, extract it's contents and close it again
a half dozen times. When I'm writing a longish program I may have to do this
several hundred times. It seems a waste of time to keep typing:
if(open(FILE, "<$Data/file_name.txt")) {
flock(FILE, '2');
@file = ;
close(FILE);
chomp(@file);
}
Each time I type the above, that's 119 bytes I'm using. Multiply that by
two hundred times and you have about 23Kb worth of characters. By using the
getfile.pl utility you will save yourself time typing and server space.
To use this utility, load it with your other script that is calling it and CHMOD
it to 755. The other script must have a require statement before the subroutine
getfile() is called. The require statement looks like this:
require 'getfile.pl';
The way it works is you pass the script the path to the file you want opened
and then assign the returned value to an array. Here are a few examples showing
how the script is called:
- By specifying the abs-path yourself:
@new_array = getfile('/home/name/html/path/to/file.txt');
- By passing the abs-path as a variable:
@new_array = getfile($file_path);
@new_array = getfile("$Data/$user/$file");
|