in reply to Re^5: directory tree to hash of arrays
in thread directory tree to hash of arrays

The subroutine takes a directory as an argument...then iterates over the directories and files found in that directory. The result needs to be sent as JSON to a web-application that uses the resulting data to populate a directory tree (file browser). The data structure you were poking at (array with single element which is a hashref...) is structured that way because the Javascript UI framework expects a JSON object with a "data" section that contains, not surprisingly, data...and a metadata section that contains other information about the datasource (e.g. if paging is used, how many records, time if retrieval, etc.).

I didn't refuse to use your example...I simply thought that the actual structure I wanted was more useful. So, if it makes it easier, here is your structure:

[ {information => { text => 'dir_A', data => { size => 99, type => 'folder' }, contents => [ { text => 'file_B', data => { size => 99, type => 'file' } }, { text => 'dir_C', data => { size => 99, type => 'folder' }, contents => [ { text => 'file_D', data => { size => 99, type => 'file' } }, { text => 'dir_E', data => { size => 99, type => 'folder' }, contents => [ { text => 'file_F', data => { size => 99, type => 'file' } } ] }, { text => 'file_G', data => { type => 'file', size => 99 } } ] }, { text => 'file_H', data => { type => 'file', size => 99 } } ] } } ]

The subroutine would be used thusly:

my $arrayref = _directoryTree('/usr/local/bin','/usr/local');

The reason for the filter is that I don't want to expose the entire path to the user of the web-application. All they need to see is the directory structure starting at a certain level...so, rather that showing them a tree consisting of /home/user/somedirectory/web-app/public/data... The filter could be used to "start" with /public/data as the "root", rather than the whole thing. The arrayref is then passed to the web-app framework(Mojolicious) and rendered as JSON to the client side thusly:

get => 'directory_list' sub { my $self = shift; my $arrayref = _directoryTree('/usr/local/bin','/usr/local'); $self->render(json => $arrayref); };

On a final note. I understand what you said about the two different uses of a key called "data". Since those are in two entirely different hashes, they have nothing to do with each other. The convention for the javascript framework is to construct a datasource with a "data" section containing the data and another data section for each record that contains any user-defined fields (e.g. those the framework does not specifically call out)...making it more sensible on the javascript side. If the field is in the documentation then just call object.text for instance, for an undocumented field, call object.data.customThing. Not my choice of conventions...I am just working with it.