Click to See Complete Forum and Search --> : nature of 'require' in Perl


goofball
09-09-2003, 12:58 PM
When require is used in Perl to import a .pl file's variables at run time, is there an effect on a process' memory aside from what the imported vars take up?

Say I want to require 'data.pl';
that contains only this:

#!/usr/bin/perl

%data = (
key_one => '1',
key_two => '2',
etcetera => '...'
);

1;

I then write more data to that file, and then
require it again to re-import it's %data hash that contains the new data. (In know I have to delete the filepath from %INC before I can require that file a second time). I know that each time I require the file, %data gets replaced so there is no problem with memory there.

But what I want to know is, does the require function import the actual code of the data.pl file (including the camas, etc.) as if the code were being compiled into the running app? I'm not sure how that works, and I don't want to risk maxing out a process' memory by re-requiring files a million times.

Thanks for any help!

GaijinPunch
09-10-2003, 03:33 AM
Logic tells me that it's going to suck up the whole file, but I've been surprised many times with perl.

Have you thought of using a REGEX to get only the values from the file you need? Will take some trickier coding, but if you're conscience on memory, it might be a better solution.

goofball
09-10-2003, 03:42 PM
Naw... if I wanted to use a regexp, I might as well just go with a flat txt file database and open filehandles.
That would defeat the whole purpose of using .pl files, the beauty of which is that you can grab an entire set of data with just one statement: require 'data.pl';

- but thanks for the suggestion.

If been using this "require" system on a database with almost 2,000 .pl files, and so far there hasn't been any problem. But the database is growing, and I'm just looking out for the future. I'll keep running tests, and maybe I'll post back when I find out more.

Scriptage
09-10-2003, 07:40 PM
I don't quite understand why you are requiring the perl program many times within another program. What you are doing (if I am not mistaken) is emulating dbmopen();

try

dbmopen(my %data, "data", 0755) || die "Cannot open dbmfile: $!";

#Do your adding and such

dbmclose(%data);

This creates either a data.db file (on webservers) or 2 files, data.dir and data.pag if not.

Regards

Carl