I happened to notice this in a working script as I was about to make changes. Now I'm afraid to touch it!

The variable $database is defined via my inside an if statement. Why can code outside of that block refer to it?

The closure that uses it should not be able to see into a parallel scope, as it only sees its enclosing things, right? Is there something funny about if statement blocks, or is this a perl bug, or what? The code works, and it's clear that the $database used by the subsequent code indeed is connected to Source Safe, so it's not finding a stray global.

baffled,
—John

Update: it no longer compiles, but gives the expected error. So why did it work once, on November 21? I hate anomolies.

Code attached,

sub go { print "Working area is \"$directory\"\n"; $version_text= "Iteration $version[0], " . ($version[2] ? "Revision $version[2]" : "initial release") . ($version[3] ? ", Patch $version[3]" : ""); print "Version stamp: $version_text\n"; if ($option eq 'checkout') { my $database; ###### DECLARE HERE $database= new Win32::OLE:: "SourceSafe"; my $copy= $source_safe_repository; # OLE doesn't like magic varia +bles $database->Open ($copy); } my $eachfile= sub { # on entry, $_ is name alone return unless /\.cxx$/ || /\.cpp$/ || /\.h$/ || /\.hpp$/; my $fname= $File::Find::name; $fname =~ tr[/][\\]; eval { process_file ($fname, $database); ##### USE HERE }; if ($@) { print "* An error was encountered processing \"$fname\". Error t +ext is:\n$@\n"; } }; my $filter= sub { return grep {$_ ne "Dependant_Libs" && !/\.OUT\./} @_; }; find ({wanted => $eachfile, preprocess => $filter}, $directory); print '=' x 50, "\n"; }

In reply to Something fishy about my scope by John M. Dlugosz

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.