http://qs321.pair.com?node_id=22250


in reply to Help using Benchmark.pm

Hi,

Your problem can be solved by making $myvar a global, either by using:

use vars qw|$myvar|; $myvar = "...";
or if you use perl 5.6:
our $myvar = "...";
It can also be solved by letting the keys in that hash point to functions:
timethese(100000, { Greedy => sub { $myvar =~ /"(.*)"/ }, Lazy => sub { $myvar =~ /"(.*?)"/ }, Negated => sub { $myvar =~ /"[^"]*"/ } });
If the value of one of the keys is not a reference to a function, Benchmark is forced to eval your code - and when you eval a string all the variables referenced within it will be looked up in the stash (where all the symbols live) - they are treated as globals you could say. That's why it works when you make $myvar a non-lexical variable.

Autark.