Regardless of any contortions Benchmark goes through to execute code in the calling package, lexical variables are not in any package. It is impossible for code to access lexical variables that are not in scope.

No arguments here. My only problem with your post was the line I quoted. I've just reread your original post and you have to admit its a little ambiguous. Yes the code is executed inside of the _file_ Benchmark, but not in the package Benchmark. And this leads to an interesting point (about which Ill have to trawl the docs for at a later time) the two are different. Consider the below code

#Put in file TestA.pm in a root lib directory package TestA; my $var="TestA"; 1; #Put in file TestB.pm in a root lib directory package TestB; use TestA; my $var2="TestB"; package TestA; print "'$var','$var2'"; 1;
Now if you do perl TestB.pm you will see
'','TestB'
But if you stick the entire code snippet into TestB.pm then you will see
'TestA','TestB'
Even though you might expect that the lexical variable $var would be out of scope.

(Incidentally I have a development Lib directory that I use for this type of stuff and have my enviornment configured to always include it.)
Update
It seems to me the moral of this is that when you put many packages in a single file everything following the package declaration should be placed into an anoymous block. Ie:

package TestA; { my $var="TestA"; 1; } package TestB; { use TestA; my $var2="TestB"; } package TestA; { print "'$var','$var2'"; 1; }

Yves / DeMerphq
--
Have you registered your Name Space?

In reply to Re: x 5 Tribute to TMTOWTDI (A question of scope) by demerphq
in thread Tribute to TMTOWTDI by s0ttle

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.