Bompa

1. CPU resourecs.
2. Memory
3. Bulky, cluttered code.



I did this for years:

open(IN, 'myfile.txt');
@array = <IN>;
close IN;



When I got more clever, I did this:

open(IN, 'myfile.txt');
chomp(@array = <IN>);
close IN;

This way I don't have to chomp the line endings off later in my script.  Weeeeeee!


As I grew in

perl

 , so did myfile.txt grow in size, and in fact, I had several of them.
myfile_1.txt
myfile_2.txt
myfile_3.txt
etc. etc.

Each file was quite large and I was opening them all, reading them into arrays then closing them.

It seemed an okay method to me, except it was quite a hassle when I wanted to modify an element
in any one of the arrays and save it to file.


I had a lot of these all through my code:

open(OUT, '>myfile_1.txt');
print OUT @array1;
close OUT;


Somehow I came accross Tie::File.  Whoa!  Powerful and afaik it comes with

Perl

  core.

Now I don't open files for read or write.



I just put this near the top of my code:

use Tie::File;


Then I just do this for each file/array:

tie @array1, 'Tie::File', 'myfile_1.txt';

Thereafter, whatever my code does to any element in any array, automatically is done
to the file AND the files are not loaded into memory so they can be gigantic.

That's it.
Bompa

perkiset

So TIE essentially makes a file accessible like an array... yes? The the

PERL

  processor, knowing that you have a file rather than memory associated with the reference understands to read the [nth] element of the file rather than the array?

Bompa

quote author=perkiset link=topic=429.msg2831#msg2831 date=1186975476

So TIE essentially makes a file accessible like an array... yes? The the

PERL

  processor, knowing that you have a file rather than memory associated with the reference understands to read the [nth] element of the file rather than the array?


Exactly.

JasonD

Bomps.

Thanks mate. I've been working with

Perl

  for years and this is the 1st time I have come across that module.

Excellent Applause

proton

could turn out to be very useful - thanks for the tip Applause

dirk

A tie can also be very useful for hashes:

tie (%hash, SDBM_File, $filename, O_RDWR|O_CREAT, 0644) || die;

If you have a hash with about 10 MB content it will require normally about 1 GB RAM (factor 100).

Therefor we often use tie with a hash to minimize RAM.


Perkiset's Place Home   Politics @ Perkiset's