in reply to Re: Caching files question
in thread Caching files question
(with an added "or die ...", as appropriate)my $fh = myOpen( "<", $pathname )
Also, I was curious why you bother to localize $/, given that you are calling sysread, which doesn't use $/ at all. And when doing sysread, it would be good to check the return value more carefully -- zero means total failure, but any value other than the size of the file would mean a partial failure, which would probably be just as bad:
(updated to include the whole subroutine with a simplified conditional block, added the check for non-zero return from "-s", and removed an unnecessary "$size" variable)my %cache; sub myOpen { my( $mode, $path ) = @_; my $fh; if ( not exists $cache{ $path } ) { -s $path or return; # don't do a 0-length or non-existent f +ile open $fh, $mode, $path or return; ( sysread( $fh, $cache{ $path }, -s _ ) == -s _ ) or return; close $fh; } open $fh, $mode, \$cache{ $path } or return; return $fh; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Caching files question
by vit (Friar) on Aug 17, 2008 at 15:37 UTC | |
by graff (Chancellor) on Aug 17, 2008 at 18:57 UTC | |
|
Re^3: Caching files question
by BrowserUk (Patriarch) on Aug 18, 2008 at 00:02 UTC |