Why are you unable to require the file? Can you change the format of the file from being Perl qualified syntax to being just the data?
i.e.:
key:value
this:that
hibby:jibby
jabby:fribby
flibby:flabby
It would be much easier to turn the contents of a file like this into a hash.
I like Jim's solution (sorry LightElf, and Welcome, by the way) because it lets you use the data-file in different ways maybe, and it certainly doesn't hide the name of the variable in another file as it would if you required perl code. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
I don't entirely agree that either solution is better than the other. There are times when one will work better, and times when the other is more appropriate. I've used both methods, depending on what I wanted to accomplish and a variety of other considerations. For somewhat sophisticated configutation files I kind of like to use the require hash method. Tracy Dryden
tracy@bydisn.com
I want to open the file and read the info into another hash. I have to be able to use some other method than using 'require' or 'use'. Any ideas?
Unquote
Given the request, I don't see how 'require'-ing the hash is really an option. While I don't know why these constraints exist, I always like to recognize them in my reponses.
As far as storing a hash in a file in perl syntax form, only to be require'd later: why? What possible advantage could storing a hash in perl syntax possibly give? It certainly complies with laziness and impatience, but hubris? For the same reasons Mike pointed out, I wouldn't store the data as a 'syntax hash'.
I was using the require function to get the configuration file but I was using the eval function to test of its existance and getting odd errors that occured when the configuration was updated through another cgi script. Therefore, I sought another solution.
Placing the hash in the file in the key:dat format solved all my issues.
Jim, as for why someone would want to create a file in perl syntax hash form only to require it later: it makes a great way to store configuration options OUTSIDE of the source code, but get them into the program quickly and conveniently. It's also handy if you have multiple configuration files (i.e. a different one for each user). You can use the username as part of the required file name. Tracy Dryden
tracy@bydisn.com
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.