garskoci has asked for the wisdom of the Perl Monks concerning the following question:
Hello!
I have a couple of questions about using hashes.
1) Is there a maximum number of elements that I can use in a hash?
2) I have a couple of hashes and I would like to keep them in seperate file from my main program. I tried to use "require" to bring them in when the program is launched, but this does not work. No errors though. I do have a "1;" at the end of my hash file that I'm trying to include. I also tried "use".
Any ideas?
Thanks.
Re: general hash questions
by wazzuteke (Hermit) on Dec 16, 2005 at 03:45 UTC
|
| [reply] [d/l] [select] |
Re: general hash questions
by japhy (Canon) on Dec 16, 2005 at 04:18 UTC
|
If you used 'my' on the hashes in the file you're trying to include, that's the wrong move. They won't be visible outside that file. You don't want lexicals, you want package variables.
| [reply] |
|
Thanks for the replies. This was my problem. I had placed "my" on each hash.
| [reply] |
Re: general hash questions
by sauoq (Abbot) on Dec 16, 2005 at 03:46 UTC
|
| [reply] |
Re: general hash questions
by davido (Cardinal) on Dec 16, 2005 at 05:59 UTC
|
Perl doesn't impose artificial restrictions on such things as how big a data structure can grow. In fact, it places very few artifical restrictions, period. It might be considered an artifical restriction that the regular expression engine is not re-entrant, but that's a stretch. And there is a warning that gets spit out if you dive too deep into recurision, unless you turn off the warning. That's somewhat contrived. But generally, no you don't have to worry about Perl preventing you from consuming 100% of your memory and swapfile space with gigantic datastructures.
As for storing your hashes, you might also look at using a data serializer such as Storable. But in the immediate, your problem seems to have already been solved by remembering not to expect lexicals to behave like package globals.
| [reply] |
Re: general hash questions
by chas (Priest) on Dec 16, 2005 at 04:40 UTC
|
That should work fine. Put your hash def in a file "filename.pl" and then in your script file have the line
require "filename.pl"; (This assumes that filename.pl and your script are in the current directory
in which you are running the script.)
If the hash def is in a file "filename.pm", then you can have
the line use filename; (This latter assumes that filename.pm is in the same directory as the script using it and that you are running the script in that directory and that
the current directory is included in @INC.)
But read the docs on "use", "require" and also "do"; the latter may be more appropriate for what you want (and easier to manage.)
Oh, and there shouldn't be a limit on the size of the hash -
till you run out of memory.
(Update: Looks like the problem was solved while I was
responding.) | [reply] [d/l] [select] |
Re: general hash questions
by SamCG (Hermit) on Dec 16, 2005 at 05:14 UTC
|
Is there some problem with using a dbm to do this? I know they're not entirely fashionable anymore, but they're pretty good for storing hashes. I think it might also make it easier to programmatically make changes to your hash to simply tie it to a dbm (maybe you could do this with a file-based hash too, I don't know). | [reply] |
|
|