Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Global vs. local?

by ig (Vicar)
on May 28, 2009 at 10:55 UTC ( [id://766626]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Global vs. local?
in thread Global vs. local?

I have no explanation for the failure. I tried to replicate your behavior with 5.8.8 and 5.10.0 but my tests worked in every case I tried. My last test program was:

#use strict; use warnings; sub subfunction { foreach my $j (0..5) { my $SUB = 'testsub'.$j; *$SUB = sub { display($j); }; } } sub display { my $x = shift; print "\$x = $x\n"; } subfunction(); &testsub0(); &testsub1(); &testsub2(); &testsub3(); &testsub4(); &testsub5(); __END__ $x = 0 $x = 1 $x = 2 $x = 3 $x = 4 $x = 5

Can you post a minimal running test script that demonstrates the bug unexpected behavior?

What version of perl and what platform?

Replies are listed 'Best First'.
Re^4: Global vs. local?
by jpavel (Sexton) on May 28, 2009 at 12:02 UTC
    ActiveState perl 5.10.0 (Binary build 1004 287188)

      You are not using strict, which would have enforced proper declaration of your lexical variables as lexicals:

      sub execute_Click { another_sub(); foreach $j (0..$i-1) {

      Here, $j is a global variable, which use strict; would tell you about. You need to write this as:

      sub execute_Click { another_sub(); foreach $my j (0..$i-1) {

      so that each subroutine gets its own copy of $j instead of them all using the one, shared $j.

      strict also has the convenient feature of alerting you to mistyped variable names, which is why I recommend to use it always.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://766626]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found