The problem is that Perl doesn't look ahead far enough to figure out whether {
means "start an anonymous hash reference" or "start a code block". It should (ideally) look to the corresponding }
and see if there is or isn't a comma, and act accordingly, but it doesn't. It only looks a little bit ahead and tries to guess. And this time it's wrong, and you get a syntax error about a comma that shouldn't be there, except that it should so don't move it.
perldoc -f map
will tell you all about this. Basically, it says that if you put +{
, Perl will understand that this means "not a code block" and guess that it's a hash reference. This is probably the cause of your syntax error. As another suggestion, it might work to say map({ HASH STUFF }, $rs->all)
- I bet money Perl won't guess it's a code reference here.
I couldn't get it to work, but not having $rs
or a ->TO_JSON
or a variable named $q
I couldn't get any of this to work anyway. I hope this helps. If not, post a little more code. Don't worry, we don't bite.
Also, while we're at it, why not write it this way:
my $results;
$results->{data} = [ map MAGIC MAP STUFF, $rs->all ];
Might arguably be more readable, especially if you're adding a lot of stuff to $results
all at once.