in reply to Passing hash names in a file
Well, given the other information you've added, it probably won't be so hard to parse the "glossary.pl" file on your own (the following has not been tested):
I'm basing this on the assumption that the style and syntax used in the glossary file are tightly constrained (and that the sample you showed is an accurate picture of the style being used). There's no guarantee that people adding to "glossary.pl" will always hew to the specific style shown in your post, but the fact that it's a shared resource makes it more likely that it will always have an easy, predictable format (at least, you could make it so without causing any of the dependent scripts to break.)open(GLOSSARY, "glossary.pl") or die $!; my $word = ""; my $trans_hash = (); while (<GLOSSARY>) { if ( /\%(\w+)\s*=\s*/ ) { # look for start of hash definition $word = $1; } elsif ( $word ne "" ) { if ( /(\w+)\s*=>\s*([^,])/ ) { # find key => value assignment my ($lang, $trans) = ($1,$2); $trans_hash{$word}{$lang} = $trans; } elsif ( /^\s+\);/ ) { # watch for the end of the definition $word = ""; } } }
If the glossary file is likely to grow over time, you just need to make sure that it remains consistent with your parsing assumptions -- and maybe add a couple of error checks within or after the parsing loop above, to watch for signs of trouble (e.g., see if some words lack translations for some languages).
Since you're just reading the glossary.pl script as text data and using its contents to define keys and values for a hash array, this approach doesn't really pose any safety issues.
update: added a few comments to the code.
|
|---|