That's because localtime() is only called once (during the variable assignment), not every time you print out the variable. If you want the time to be taken in each loop iteration you either need to assign to the variable inside the loop:
foreach my $allfilesfound (@dir_contents) { my $time = localtime(); print "This file=$allfilesfound took $time to open<br>"; }
Or assign it an anonymous subroutine and call that in the loop.
my $time = sub { localtime() }; foreach my $allfilesfound (@dir_contents) { print "This file=$allfilesfound took ".$time->()." to open<br>"; }
BTW, I'm assuming that you do more inside the loop than your example code shows, because this code doesn't actually open the file at all. Also, check out Time::HiRes for more accurate timing.
In reply to Re: Timing Files in Directory
by tirwhan
in thread Timing Files in Directory
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |