Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

write_file to_json in Dancer app

by GertMT (Friar)
on Feb 04, 2014 at 16:32 UTC ( #1073437=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to create a little app that shows a list of files on a web-page to be commented on.
It seems all to work. The web-page is generated via reading the content of a directory. I'd like to parse the list of images to_json to use it in a form. However I can't seem to get this parsing right.

"error" : "write_file '' - sysopen: No such file or directory at /Users/test/mytest/lib/mytest.pm line 35.\n"

Maybe someone can give me a hint what I'm doing wrong?
Gert

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 Ao +H # #--------------------------------------------------------------------- sub AoH_imagelist { my @photos; my $images_dir = Dancer::FileUtils::path( setting('appdir'), 'public/images/galle +ry' ); 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;

Replies are listed 'Best First'.
Re: write_file to_json in Dancer app
by Corion (Pope) on Feb 04, 2014 at 16:35 UTC

    The error message suggests to me that $filename is empty. Maybe you need to initialize it from a different place in your configuration?

    Does uploading work if you hardcode the value for $filename instead of using the configuration?

      aha, yes, thanks. That does work.
      The file is updated if I refresh the browser. Well, with not much I'll have to admit but at least the modification time changes.
      So, I'm not there with the format but I should be able to work that out.
      Thanks!
        updating the code between the comment lines to the code I mention below makes that the data is been written to a 'json' file. Not very clever code yet as the data gets overwritten every time you run the script but that is a separate issue.
        #-------v this does work --------# my $filename = "testme.json"; my $json = -e $filename ? read_file $filename : '{}'; my $data = from_json $json; $data = \@photos; write_file $filename, to_json($data);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1073437]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2021-10-15 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (69 votes). Check out past polls.

    Notices?