-1

I'm building a webapp using dancer2 as backend tool. I've implemented the main method as follow:

#!/usr/bin/env perl
use Dancer2;

get '/mything/:text' => sub {
    my @myArray = ("");
    # Fill the array with DB data;
    return join "<br>", @myArray;
};

dance;

Everything is fine until the second time get method is used. Insted of @myArray being empty, its filled with the from the first execution.

As a dirty fix, I initialize @myArray to ("") at the end of the method, but I think that is ugly. Have you any experience on this?

Iván Rodríguez Torres
  • 4,293
  • 3
  • 31
  • 47
  • I think you'll need to show us the code which is populating `@myArray`. As an aside, `@myArray = [""]` doesn't do what you think it is doing. You probably want `@myArray = ()` instead. – Dave Cross Jul 21 '17 at 11:58
  • Yep, you're right. I initialize the array using `("")` since I want it to cointain a `""` as first element. Thanks for catching the mistake @DaveCross – Iván Rodríguez Torres Jul 21 '17 at 17:03

2 Answers2

3

To test this, I expanded your code into the following:

#!/usr/bin/env perl
use Dancer2;

get '/mything/:text' => sub {
    my @myArray = localtime;
    # Fill the array with DB data;
    return join "<br>", @myArray;
};

dance;

Using localtime() was the easiest way I could think of to get a (slightly) different array each time I make a request (assume I don't make more than one request a second).

And that works exactly as I expected. I run plackup app.psgi and visit http://localhost:5000/mything/foo and I see the array I expect. When I refresh the page I get a different array.

So Dancer works as I would expect it to. If you're seeing different behaviour, it's because you're doing something different. And we can't really help you to work out what that is until we see more of your code.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
0

Problem was related to not using perl in strict mode. The code as it was, was working properly in OSX 11.1 but not in Ubuntu 16.04. So after some tests, I found that some variables that I use to fill the array from the DB weren't properly initialized. After initialize them, everything is working as it should in OSX and Ubuntu.

Iván Rodríguez Torres
  • 4,293
  • 3
  • 31
  • 47
  • That doesn't really explain why there was a difference between OSX and Linux. Do you have any more information? – Dave Cross Jul 27 '17 at 07:18
  • @DaveCross No, I don't . I figure out that Perl handles memory in a different way in OSX than in Linux. Anyway, if you want me to do some test, just let me know – Iván Rodríguez Torres Jul 27 '17 at 22:45