in reply to Accessing the hash name in perl
I think that there are much better solutions than trying to parse a Perl file manually. However, what the best solution is depends on several things.
Can you change the program that generates this input file? If so, you should at least add the our and 1; as duyet showed, because the file you posted is actually not really valid Perl - if you try to run it, you'll get the error message "Global symbol "$test" requires explicit package name (did you forget to declare "my $test"?) at ..." On the other hand, if you can change how the data is stored, then it's probably much better to use a data serialization format such as JSON (using JSON::MaybeXS), for example.
Can you completely trust the source of this file? If so, you can make Perl parse it using one of the functions do, require, or use, as several monks have shown. However, note that this will execute arbitrary Perl code, so these functions can introduce huge security holes if any of the input files can be changed by untrusted users, for example.
If you're already married to Perl as a serialization format (which, as I said above, I wouldn't necessarily recommend), then I can suggest one of my own modules as a possibility to more safely parse the file - note however that it still has quite a few limitations, for example it will not parse the statements package hash; use strict; use warnings; in your input, so you'd have to strip those manually using e.g. a regex. You can read about my module at Undumping Perl.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Accessing the hash name in perl
by vrk (Chaplain) on Mar 22, 2017 at 15:11 UTC | |
by haukex (Archbishop) on Mar 22, 2017 at 15:30 UTC | |
by afoken (Chancellor) on Mar 23, 2017 at 07:35 UTC |