Use strict requires me to declare variables using either my or local (as far as I can tell). From Programming Perl 2nd edition, p. 108:

“…dynamic (read: local) variables are also visible to functions called from within the block in which those variables are declared. Lexical (read: my) variables are not. They are totally hidden from the outside world including any called subroutines…”

I added the notes in parenthesis. Two test cases:

#!c:\Apps\xampp\perl\bin\perl.exe -wT use strict; my $user_name; $user_name = $ARGV[0]; print "IN SCRIPT: user_name == $user_name\n"; login(); print "IN SCRIPT: user_name == $user_name\n"; sub login { print "IN SUBROUTINE: user_name == $user_name\n"; print "Can I change it in the subroutine?\n"; $user_name = "Bill"; print "IN SUBROUTINE: user_name == $user_name\n"; }
OUTPUT:
C:\Apps\xampp\htdocs\planning>perl -wT projselect_test2.pl waylon
IN SCRIPT: user_name == waylon
IN SUBROUTINE: user_name == waylon
Can I change it in the subroutine?
IN SUBROUTINE: user_name == Bill
IN SCRIPT: user_name == Bill

This program indicates I can access the variable’s value from a subroutine, and I can change it as well. How does that reconcile with the statements above from Programming Perl? Now the stumper, which is my web login script (greatly simplified here to illustrate only this problem):

use strict; my $user_name=""; $user_name = param('username'); print "Content-type: text/html\n\n"; print "<html><body><pre>\n"; print "IN SCRIPT: user_name == $user_name<br>\n"; login(); print "IN SCRIPT: user_name == $user_name\n"; print "</pre></body></html>\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"; }
OUTPUT (first run, user name supplied is Sam):
IN SCRIPT: user_name == Sam

IN SUBROUTINE: user_name == Sam
Can I change it in the subroutine?
IN SUBROUTINE: user_name == Bill

IN SCRIPT: user_name == Bill

OUTPUT (second run, user_name supplied is George):
IN SCRIPT: user_name == George

IN SUBROUTINE: user_name == Bill # <--Crazy!!
Can I change it in the subroutine?
IN SUBROUTINE: user_name == Bill

IN SCRIPT: user_name == George

Here’s the stumper part: When I call this the first time, the $user_name within the subroutine matches the $user_name in the main script and the one that was passed from the calling html form (the html form is at the bottom of this post for completeness). If I use the back button and enter a new user name, the master script has the new user name but the subroutine has the name from it’s prior run.

My questions:

Why, if variables declared private using my shouldn’t be available in a subroutine, are they available in a subroutine?
Why does my CGI script maintain information between executions?
Are there any other variable declarations I should know aside from my and local when using strict?

<html> <body><pre> <form method="post" action="projselect_test.pl"> <h2>System login</h2> User Name: <input type="text" name="username" size=15><br> Password: <input type="password" name="password" size=15><br> <input type="submit" value="Login"><p> </pre> </body> </html>

In reply to My, subroutines, scope, and CGI – Why can I see a private (my) variable in a subroutine but it doesn’t get updated? by Calm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.