I'm writing a web app in Perl with Dancer
. Suppose I declared an our
variable $var
in A.pm
, and assign the content of file1
to it:
sub get_file_content {
our $var = <FILE1>;
}
I declared var
as our
because I would like to use it later in another file B.pm
:
sub display_file_content {
&display($A::var);
}
So, after get_file_content
was executed, $var
should be the content of file1. Before display_file_content
is executed, I changed the content of file1, and clicked some button to execute display_file_content
. As supposed, the original content of file1 was displayed.
Then I did the same thing as above, except that after changing the content of file1, I didn't click display_file_content
button. Instead, I start another web request for the same page in another web browser. I executed get_file_content
first, and click the button to go through display_file_content
directly. The modified content of file1 was displayed in the second web browser. This is normal. However, when I click the display button in the first web browser, it also displayed the modified content, instead of the original one. As a comparison with the first experiment without the second web request in another web browser, it seems the variable $var
is shared in both web request-response process. But why did it happen? Could you please give some explanations?
"It seems the variable var is shared in both request-response processes" because it is. The our
keyword declares a package global. You're running your Dancer app in a persistent environment, your global variables are persistent also. You're going to need to reset any our
variables at the beginning of each request.