This is taken from perlman:perlsub:

When to Still Use local()

Despite the existence of my(), there are still three places where the local() operator still shines. In fact, in these three places, you must use local instead of my.

  1. You need to give a global variable a temporary value, especially $_.

    The global variables, like @ARGV or the punctuation variables, must be localized with local(). This block reads in /etc/motd, and splits it up into chunks separated by lines of equal signs, which are placed in @Fields.

        {
            local @ARGV = ("/etc/motd");
            local $/ = undef;
            local $_ = <>;  
            @Fields = split /^\s*=+\s*$/;
        } 
    

    In particular, it's important to localize $_ in any routine that assigns to it. Look out for implicit assignments in while conditionals.

---------------------

Basically, you're asking benchmark to do one of two things. Either, it has to reset all the global variables to the states they were at just before 'timethese' was called. Alternatively, the benchmarked code could be examined by 'timethese' and determine which global symbols your code uses and only reset those.

I think both of these are silly and horrible, to put it bluntly (sorry :) ). If you want to reset a variable you should do it yourself. And notice that you are warned in the documentation that you should local any global variable that needs a temporary value. Of course, you can also just reset @_ or $_ with:
@_ = ();
Which (can) be faster than:
local @_;
However, you should note that the local call will keep memory usage down. Otherwise Perl doesn't seem to flush @_ = () memory in the same way.

Anyway, this feature of not reseting your states is surely not a "side effect". Having Benchmark reset the state of global variables (think about filehandles) as an option or otherwise would definately be an interesting endeavor. However, I do not think it would be worthwhile, nor possible to implement cleanly or quickly.

Seriously, how hard is it to put your own initialization code in? And also does it not make sense that you would need to reset your conditions yourself?? Lastly, the documentation (perl's) states you should local global variables that need temporary values, and Benchmark's documentation says nothing about reseting variables for you either.

But cheers anyway, :)
Gryn


In reply to "Magic" state resets by gryng
in thread timethese, and pushing array values by husker

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.