I've seen some cases where people take the trouble to provide a report on caching effectiveness -- basically, you want to know how many times "first_line" was called, and of those times, how often you hit on a cached value.
That sort of thing is easy to add -- here's how it might look, adding to dragonchild's proposal, which is very clear and clean:
Usually you'd want the report to be conditional on a run-time option -- e.g. the user includes a "-v" or "-d" flag when running the script from a command line to select "verbose" or "debug" mode.my %cache; my ($ncalls, $nhits); sub first_line { my ($filename) = @_; $ncalls++; if (exists $cache{$filename}) { $nhits++; } else { open( my $fh, $filename ) or die "Cannot open '$filename': $!\ +n"; $cache{$filename} = <$fh>; } return $cache{$filename}; } END { print "first_line returned cached value $nhits times in $ncalls +calls\n" }
In reply to Re: Cache Subroutine Return Value
by graff
in thread Cache Subroutine Return Value
by souravdas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |