I have a config file that looks a bit like this:
add
1
2
concatenate
foo
bar
blat
What I’m trying to do is turn this into hashes like %hash = (name=>"add", args=> [1,2]) etc, and push the hash references into a single array. Looping through the file and creating each hash seems straightforward enough, except I get stuck when it comes to naming these hashes to push their references into the array. The config file is going to change all the time and have a variable number of different name/arg combinations to store. Is there a way to iterate through hash names so I can push them into an array one at a time?
So far it looks like this:
my %temphash = (name=>'add', args=>[1,2]);
push (@array, \%temphash);
Can I make that %temphash into something generated on the fly and push it before moving on to the next one?
Edit: Context
The plan is to use those ‘name’ keys to call subroutines. So something like this could work:
my %subhash = (add=>\&addNumbers, concatenate=>\&concat);
Except the list of subroutines I’m going to need to call are in the config file and I won’t know what they are until I start reading from it. Even if I include the names of the subroutines right there in the config file, how do I iterate through them and add them as elements to that hash?
Well, you can simply use curly brackets to make an anonymous hash:
You can create the same effect by utilising the lexical scope of the
mydeclaration. E.g.: