You're confusing package globals and lexical variables. You wrote this code to demonstrate that you can see package variables in package R:
package R; my $R = 10; package main; my $subcode = "sub { local \$_; package R; print $R}"; my $subref = eval $subcode; &$subref;
That's not doing what you think it's doing. That code is accessing $R not because you've specified "package R" but because $R is lexically-scoped to the containing file. If you had been running your quasi-benchmark code in a different file, it wouldn't have worked. That's why, in your original code, the benchmarked code couldn't access $now and %url when you declared them with my: because they were declared lexical to the enclosing file, and your code was running in a different file.

That's why you need to make your variables package globals, presumably using the vars pragma, but probably you could also use our, in Perl 5.6. Because those package globals are in your package's namespace, so when Benchmark executes your code in the "caller's package", it's accessing the correct variables.

As for your other question:

> P.P.S. Here are my results with the correct code. Grep is > still faster than the other algorithms, so the original > question still stands (how can Perl execute an O(N) plus > an O(N log N) algorithm faster than a single O(N)?)
I don't know, but I think lhoward had the best suggestion: the sort may be O(N log N), but that's not the only measure of efficiency; how you code the algorithm, and how it's optimized, is also a big influence on speed. So running an O(N log N) algorithm in tight C code could still be faster than running an O(N) algorithm written in Perl.

In reply to Re: Benchmark.pm's scoping behavior by btrott
in thread Benchmark.pm's scoping behavior by Russ

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.