On 6 October 2010 23:01, Jakub Narebski <jnareb@xxxxxxxxx> wrote: > +# creates get_depth() and set_depth($depth) etc. methods > +foreach my $i (qw(depth root namespace)) { > + Â Â Â my $field = $i; > + Â Â Â no strict 'refs'; For each item, you'll set "no strict refs"? This might be better off outside the loop. It's still scoped appropriately inside the subroutine. > + Â Â Â my $file = $self->path_to_key($key); > + Â Â Â return undef unless (defined $file && -f $file); PBP (Perl Best Practises) will tell you that explicitly returning undef is discouraged -- "undef" should be reserved for those errors you cannot handle, not ones you don't want to. > + > + Â Â Â # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed > + Â Â Â # via CHI::Driver::File (from CHI-0.33) > + Â Â Â my $buf = ''; > + Â Â Â open my $read_fh, '<', $file > + Â Â Â Â Â Â Â or return undef; Ditto. > + Â Â Â binmode $read_fh, ':raw'; > + > + Â Â Â my $size_left = -s $read_fh; > + > + Â Â Â while ($size_left > 0) { > + Â Â Â Â Â Â Â my $read_cnt = sysread($read_fh, $buf, $size_left, length($buf)); > + Â Â Â Â Â Â Â return undef unless defined $read_cnt; Ditto. > + Â Â Â Â Â Â Â last if $read_cnt == 0; > + Â Â Â Â Â Â Â $size_left -= $read_cnt; > + Â Â Â Â Â Â Â #last if $size_left <= 0; > + Â Â Â } > + > + Â Â Â close $read_fh > + Â Â Â Â Â Â Â or die "Couldn't close file '$file' opened for reading: $!"; > + Â Â Â return $buf; > +} "use Carp;" would be more useful here, and hence croak() and confess(). > +sub store { > + Â Â Â my ($self, $key, $data) = @_; > + > + Â Â Â my $dir; > + Â Â Â my $file = $self->path_to_key($key, \$dir); > + Â Â Â return undef unless (defined $file && defined $dir); See above. > + Â Â Â # ensure that directory leading to cache file exists > + Â Â Â if (!-d $dir) { > + Â Â Â Â Â Â Â eval { mkpath($dir, 0, 0777); 1 } > + Â Â Â Â Â Â Â Â Â Â Â or die "Couldn't create leading directory '$dir' (mkpath): $!"; > + Â Â Â } Why is this eval()ed? It will still return false and set $! appropriately. > + Â Â Â # generate a temporary file > + Â Â Â my ($temp_fh, $tempname) = $self->_tempfile_to_path($file, $dir); > + Â Â Â chmod 0666, $tempname > + Â Â Â Â Â Â Â or warn "Couldn't change permissions to 0666 / -rw-rw-rw- for '$tempname': $!"; > + > + Â Â Â # Fast spew, adapted from File::Slurp::write, with unnecessary options removed > + Â Â Â # via CHI::Driver::File (from CHI-0.33) > + Â Â Â my $write_fh = $temp_fh; > + Â Â Â binmode $write_fh, ':raw'; > + > + Â Â Â my $size_left = length($data); > + Â Â Â my $offset = 0; > + > + Â Â Â while ($size_left > 0) { > + Â Â Â Â Â Â Â my $write_cnt = syswrite($write_fh, $data, $size_left, $offset); > + Â Â Â Â Â Â Â return undef unless defined $write_cnt; Again, with the undef. > + Â Â Â Â Â Â Â $size_left -= $write_cnt; > + Â Â Â Â Â Â Â $offset += $write_cnt; # == length($data); > + Â Â Â } > + > + Â Â Â close $temp_fh > + Â Â Â Â Â Â Â or die "Couldn't close temporary file '$tempname' opened for writing: $!"; > + Â Â Â rename($tempname, $file) > + Â Â Â Â Â Â Â or die "Couldn't rename temporary file '$tempname' to '$file': $!"; > +} > + > +# get size of an element associated with the $key (not the size of whole cache) > +sub get_size { > + Â Â Â my ($self, $key) = @_; > + > + Â Â Â my $path = $self->path_to_key($key) > + Â Â Â Â Â Â Â or return undef; Again with the undef > + Â Â Â if (-f $path) { > + Â Â Â Â Â Â Â return -s $path; > + Â Â Â } > + Â Â Â return 0; > +} > + > +# ...................................................................... > +# interface methods > + > +# Removing and expiring > + > +# $cache->remove($key) > +# > +# Remove the data associated with the $key from the cache. > +sub remove { > + Â Â Â my ($self, $key) = @_; > + > + Â Â Â my $file = $self->path_to_key($key) > + Â Â Â Â Â Â Â or return undef; > + Â Â Â return undef unless -f $file; Again with the undef. > + Â Â Â unlink($file) > + Â Â Â Â Â Â Â or die "Couldn't remove file '$file': $!"; > +} > + > +# Getting and setting > + > +# $cache->set($key, $data); > +# > +# Associates $data with $key in the cache, overwriting any existing entry. > +# Returns $data. > +sub set { > + Â Â Â my ($self, $key, $data) = @_; > + > + Â Â Â return unless (defined $key && defined $data); return what? > + Â Â Â $self->store($key, $data); > + > + Â Â Â return $data; > +} > + > +# $data = $cache->get($key); > +# > +# Returns the data associated with $key. ÂIf $key does not exist > +# or has expired, returns undef. > +sub get { > + Â Â Â my ($self, $key) = @_; > + > + Â Â Â my $data = $self->fetch($key) > + Â Â Â Â Â Â Â or return undef; > + > + Â Â Â return $data; > +} > + > +# $data = $cache->compute($key, $code); > +# > +# Combines the get and set operations in a single call. ÂAttempts to > +# get $key; if successful, returns the value. ÂOtherwise, calls $code > +# and uses the return value as the new value for $key, which is then > +# returned. > +sub compute { > + Â Â Â my ($self, $key, $code) = @_; > + > + Â Â Â my $data = $self->get($key); > + Â Â Â if (!defined $data) { > + Â Â Â Â Â Â Â $data = $code->($self, $key); > + Â Â Â Â Â Â Â $self->set($key, $data); > + Â Â Â } Can you guarantee $code here? unless( defined $code and ref $code eq 'CODE' ) { .... } Wouldn't it be easier to eval{} this and checj $@? [...] -- Thomas Adam -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html