package mytest; use Dancer ':syntax'; use List::Util; use File::Slurp qw(read_file write_file); our $VERSION = '0.1'; set serializer => 'JSON'; get '/' => sub { template 'index' => { photos => AoH_imagelist(), }; }; #--------------------------------------------------------------------- # # function to read image-dir-gallery and create imagelist stored in AoH # #--------------------------------------------------------------------- sub AoH_imagelist { my @photos; my $images_dir = Dancer::FileUtils::path( setting('appdir'), 'public/images/gallery' ); opendir( my $dh, $images_dir ) || die "can't opendir $images_dir: $!"; my @image_files = grep { $_ if /\.jpg$/i } readdir($dh); for my $i (@image_files) { push @photos, { file => "$i", caption => "" }; } #-------v this doesn't work? --------# my $filename = config->{mytest}{json}; my $json = -e $filename ? read_file $filename : '{}'; my $data = \@photos; #my $data = from_json $json; $data = { file => params->{file} , caption => params->{caption}, }; # description to be added write_file $filename, to_json($data); #-------^-----------------------------# return \@photos; } true;