Why, if variables declared private using my shouldn’t be available in a subroutine, are they available in a subroutine?
The subroutine that you're testing with is defined inside the scope where the lexical variable exists. If you do something like this instead:
{ my $user_name=param('username'); print "IN SCRIPT: user_name == $user_name<br>\n"; login(); print "IN SCRIPT: user_name == $user_name\n"; } sub login { print "IN SUBROUTINE: user_name == $user_name<br>"; print "Can I change it in the subroutine?<br>"; $user_name = "Bill"; print "IN SUBROUTINE: user_name == $user_name<br>\n"; }
...then it won't even compile under strict because the sub refers to a variable ($user_name) that's not available in its scope. Put the login sub inside the braces that have my $user_name, and it will work again. See?
Why does my CGI script maintain information between executions?
This is something that mod_perl does.
Are there any other variable declarations I should know aside from my and local when using strict?
No. I general, you should almost always use my instead of local. If you want global variables, look at our or vars.
In reply to Re: My, subroutines, scope, and CGI – Why can I see a private (my) variable in a subroutine but it doesn’t get updated?
by kyle
in thread My, subroutines, scope, and CGI – Why can I see a private (my) variable in a subroutine but it doesn’t get updated?
by Calm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |