Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

general hash questions

by garskoci (Novice)
on Dec 16, 2005 at 03:28 UTC ( #517128=perlquestion: print w/replies, xml ) Need Help??
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.

Replies are listed 'Best First'.
Re: general hash questions
by wazzuteke (Hermit) on Dec 16, 2005 at 03:45 UTC
    By the time I post this, I'm sure I'll be the third person to say the same thing, but the first place I would look for answers like this, is here; using Super Search. Or, always Googling for something like Perl Hashes. Either way will usually get some good information for you.

    Next to that, I'll do my best in answering your questions:
    1. The only limit for hashes are amount of memory you want to use. I've been able to drain over 2GB of memory at times due to data structures, like a hash, that are just too big.

    2. use/require may only be half the battle. Take a look into creating global variables in Perl. I've found some of the following resources to be usefull:

    Hope this helps, and good luck!!

    perlmonks Hash Q/A might also be a good place to start your adventure!!

    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );
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.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Thanks for the replies. This was my problem. I had placed "my" on each hash.
Re: general hash questions
by sauoq (Abbot) on Dec 16, 2005 at 03:46 UTC
    I tried to use "require" to bring them in when the program is launched ... I also tried "use".

    perldoc -f do

    "My two cents aren't worth a dime.";
Re: general hash questions
by davido (Archbishop) 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.


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 "" and then in your script file have the line
    require "";
    (This assumes that and your script are in the current directory in which you are running the script.)
    If the hash def is in a file "", then you can have the line use filename; (This latter assumes that 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.)
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).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://517128]
Approved by GrandFather
[LanX]: Wep programing?

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2017-05-24 15:55 GMT
Find Nodes?
    Voting Booth?