Storing Hash Data Externally in Perl
I'm currently working to refactor a script that has a reliance on three hashes (simple hashes), initialized at the beginning of the script. In total, these hash values take up over a hundred lines in the script. In order to improve overall readability and cleanliness of the code, should I store this information outside of the script and read in the information at the start? The data itself should be mostly static (individual entries may have to be changed on occasion).
If yes, how would I go about storing it in a database/suggested storag开发者_StackOverflow社区e medium? (I'm a noob when it comes to SQL).
Sounds like you have configuration data. The Mastering Perl book has a chapter discussing several choices.
I would probably use something like JSON or one of the formats supported by Config::Any. For simple mappings an INI format will probably suffice. I tend to use JSON for more complex scenarios.
I would not store it in a separate file or database just because it will slow down your program for no good reason. Just move your existing initialization code to a separate constants.pl file and in your main file do require "constants.pl"
Don't forget to change your hashes's declaeration from
myto
ourto make it visible across files.
精彩评论