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

ActiveState perl 5.10.0 (Binary build 1004 287188)
Quick and messy, but here are some examples: (works)
#!/usr/bin/perl use Win32::GUI; my $w = 800; my $h = 800; my $i; $main = Win32::GUI::Window->new( -name => 'main', -width => $w, -height => $h, -menu => $menu, ); $main->AddButton( -height => 20, -width => 100, -text => 'Go!', -name => 'execute', -top => 20, -left => 650, -tabstop => 1, ); $main->Show(); Win32::GUI::Dialog(); sub another_sub { $top = 50; for ($x = 0; $x <= 2; $x++) { $uniqueWindow[$i] = $main->AddTextfield( -height => 200, -width => $w-30, -background => [255,255,255], -top => $top, -left => 10, -text => "", -name => $i."Textfield", -align => left, -readonly => 1, -multiline => 1, -autovscroll => 1, -vscroll => 1, ); $uniqueWindow[$i]->SetLimitText(1000); $top+=205; $i++; } } sub execute_Click { another_sub(); foreach $j (0..$i-1) { $SUB = $j."Textfield_MaxText"; print "-->Assigning sub function for $j\n"; my $idx = $j; # make a copy my $SUB = "${idx}Textfield_MaxText"; *$SUB = sub { variableMaxText( $idx ); } } for ($x=0;$x<100;$x++) { foreach my $txt (0..$i-1) { $uniqueWindow[$txt]->Append("Characters to make it overflo +w"); } } } sub variableMaxText { my ($self) = $_[0]; print "Variable window got maxtexted ($self).\n"; $uniqueWindow[$self]->Text(""); }
And I think it should, but it doesn't (minor change, didn't include everything)
foreach $j (0..$i-1) { $SUB = $j."Textfield_MaxText"; print "-->Assigning sub function for $j\n"; my $SUB = "${idx}Textfield_MaxText"; *$SUB = sub { variableMaxText( $j ); } }
Output from the success:
-->Assigning sub function for 0 -->Assigning sub function for 1 -->Assigning sub function for 2 Variable window got maxtexted (1). Variable window got maxtexted (2). Variable window got maxtexted (1). Variable window got maxtexted (2).
Output from the failure:
-->Assigning sub function for 0 -->Assigning sub function for 1 -->Assigning sub function for 2 Variable window got maxtexted (). Variable window got maxtexted ().
And if we get crazy and assign my $j where I define my $i:
-->Assigning sub function for 0 -->Assigning sub function for 1 -->Assigning sub function for 2 Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2). Variable window got maxtexted (2).

Replies are listed 'Best First'.
Re^5: Global vs. local?
by Corion (Patriarch) on May 28, 2009 at 12:10 UTC

    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.

Re^5: Global vs. local?
by ig (Vicar) on May 28, 2009 at 12:35 UTC