in reply to Re^2: scope of variable
in thread scope of variable

Forgot to mentioned, I have  use strict;

Replies are listed 'Best First'.
Re^4: scope of variable
by BrowserUk (Patriarch) on May 27, 2013 at 12:44 UTC
    Forgot to mentioned, I have use strict;

    You should also be using warnings (or -w) because then you would see:

    Variable "$targetSheet" will not stay shared at - line 46. Variable "$row" will not stay shared at - line 47. Variable "$col" will not stay shared at - line 47. Variable "$group" will not stay shared at - line 47. Variable "$assert" will not stay shared at - line 47. Variable "$ftest" will not stay shared at - line 47. Variable "$mergeAcross" will not stay shared at - line 55. Variable "$mergeDown" will not stay shared at - line 59. Variable "$isComment" will not stay shared at - line 62. Variable "@str" will not stay shared at - line 63. Variable "$isData" will not stay shared at - line 63. Variable "$targetSheet" will not stay shared at - line 68. Variable "$isComment" will not stay shared at - line 73. Variable "@str" will not stay shared at - line 77. Variable "$hvp_col" will not stay shared at - line 78. Variable "$col" will not stay shared at - line 78. Variable "$grp_col" will not stay shared at - line 79. Variable "$ass_col" will not stay shared at - line 80. Variable "$tst_col" will not stay shared at - line 81. Variable "$plan_row" will not stay shared at - line 82. Variable "$row" will not stay shared at - line 82. Variable "$group" will not stay shared at - line 83. Variable "$assert" will not stay shared at - line 84. Variable "$ftest" will not stay shared at - line 85. Variable "$isData" will not stay shared at - line 88. Variable "$targetSheet" will not stay shared at - line 94. Variable "$isData" will not stay shared at - line 95. Variable "$isComment" will not stay shared at - line 95. Variable "@str" will not stay shared at - line 96. Variable "$func" will not stay shared at - line 101. Variable "$compl" will not stay shared at - line 101. Variable "$group" will not stay shared at - line 101. Variable "$assert" will not stay shared at - line 101. Variable "$ftest" will not stay shared at - line 101.

    And resolving those would go a long way to explaining the problem you describe in the OP.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: scope of variable
by hdb (Monsignor) on May 27, 2013 at 12:40 UTC

    bart's comment on closures is exactly the reason for your problem. See this simplified example:

    use strict; use warnings; sub func { my $calls = shift; my $strange = "Call $calls"; print "In func (start): $strange\n"; sub subfunc1 { print "In subfunc1: $strange\n"; $strange .= " even"; } sub subfunc2 { print "In subfunc2: $strange\n"; } subfunc1(); subfunc2(); print "In func (end): $strange\n"; } func(1); func(2); func(3); func(4);

    which creates the following output (including a couple of warnings!):

    Variable "$strange" will not stay shared at closure2.pl line 10. Variable "$strange" will not stay shared at closure2.pl line 15. In func (start): Call 1 In subfunc1: Call 1 In subfunc2: Call 1 even In func (end): Call 1 even In func (start): Call 2 In subfunc1: Call 1 even In subfunc2: Call 1 even even In func (end): Call 2 In func (start): Call 3 In subfunc1: Call 1 even even In subfunc2: Call 1 even even even In func (end): Call 3 In func (start): Call 4 In subfunc1: Call 1 even even even In subfunc2: Call 1 even even even even In func (end): Call 4

    which clearly demonstrates that the variable $strange in the subfuncs is decoupled from $strange in func in the second and subsequent calls to func. The warnings also tell the same story.

    UPDATE: Compare to the following:

    use strict; use warnings; my $strange; sub subfunc1 { print "In subfunc1: $strange\n"; $strange .= " even"; } sub subfunc2 { print "In subfunc2: $strange\n"; } sub func { my $calls = shift; $strange = "Call $calls"; print "In func (start): $strange\n"; subfunc1(); subfunc2(); print "In func (end): $strange\n"; } func(1); func(2); func(3); func(4);

    whose output is probably what you expected. Whether or not this is good style is a diffent question ...