Bronston has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on this code for someone, using a bunch of other people's base code. Not sure what I'm working with which makes modifying it very difficult. Here's some functional base code.
my $items = [{ name => 'Arbitrary menu name', url => \&Arbitrary menu function, },{ name => 'Arbitrary menu name 2', url => \&Arbitrary menu 2, }]; $callback->({ items => $items });
I'm assuming that's a hash in a hash, if I'm wrong please correct me there. Here's some of my code.
opendir DIR, $currentDir or die "cannot open dir $currentDir: $!"; my @file= readdir DIR; closedir DIR; my $items = [{ name => 'Stuff', url => \&Arbitrary menu, foreach my $file (@file) { },{ name => $file, url => \&Arbitrary menu 2, } }]; $callback->({ items => $items });
Basically I just want to shove the name of each file in a directory into each name "key?" there. I'm sure it's obvious my code doesn't work and it's probably even obvious to someone other then me why. Please feel free to elaborate as much as possible when explaining this to me. This isn't going to be the end of this project and the better I understand it the less likely I am to have to bug you fine folks with questions. Thank you very much for any help in advance, Bronston ps. A common response I see on here is for more of the code being used. I don't know if that will come up here but I don't really have more. At least not without posting the entire server this uses.

Replies are listed 'Best First'.
Re: Stuck trying to append to a hash, I think
by NetWallah (Canon) on Nov 18, 2013 at 21:54 UTC
    The structure you are generating is actually an arrayref-of-hashrefs.

    Try this (untested):

    my $items; for my $file (@file) { push @$items, { name => $file, url => \&Arbitrary_menu_2, # WHere do you get this from + ? }; }

                 When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

      Well that was simple. Thank you very much. "url => \&Arbitrary_menu_2, # WHere do you get this from" Each of these entries is a menu option, when clicked it runs the subroutine listed in the url, that truly is arbitrary as in i just made it up as it's more or less irrelevant to the code.
Re: Stuck trying to append to a hash, I think
by ww (Archbishop) on Nov 18, 2013 at 22:07 UTC
Re: Stuck trying to append to a hash, I think
by Laurent_R (Canon) on Nov 18, 2013 at 22:28 UTC

    This is only a question of vocabulary, but I would say that $item is a reference to what is commonly called an Array of Hashes (AoH). From a strict technical standpoint, there is not really such thing as an AoH in Perl, the only way to store hashes into an array is to store in the array a list of references to hashes. But this is commonly called array of hashes because that is really the underlying intent of the data structure. The only reason I am making this point is that it might be easier for you to understand the expression Array of Hashes (even though it is a slight abuse of language), rather than array of hash refs, which might make less sense to you.