Cache-Repository

 view release on metacpan or  search on metacpan

lib/Cache/Repository.pm  view on Meta::CPAN


are both the same.

=back

Returns undef if the tag doesn't exist, or 0 for any other retrieval error.

=cut

sub retrieve
{
    my $self = shift;
    my %opts = @_;

    my $rc = 1;
    my $fh = undef;
    my $filename;

    my $callback = sub {
        my %cb_opts = @_;
        # filename, data, start, end, error

        if ($cb_opts{error})
        {
            $rc = 0;
            warn $cb_opts{error};
            return 0;
        }

        if ($cb_opts{target})
        {
            $filename = File::Spec->catfile($opts{basedir},$cb_opts{filename});
            symlink($cb_opts{target}, $filename);
            return 1;
        }

        if ($cb_opts{start})
        {
            require File::Path;
            require File::Basename;

            $filename = File::Spec->catfile($opts{basedir},$cb_opts{filename});
            File::Path::mkpath(File::Basename::dirname($filename));

            $fh = IO::File->new($filename, "w") or do {
                warn "Can't write to $filename: $!";
                $rc = 0;
                return 0;
            };
            binmode $fh;
        }

        $fh->print($cb_opts{data}) if defined $cb_opts{data};

        if ($cb_opts{end})
        {
            $fh->close();
            $fh = undef;

            chmod $cb_opts{mode}, $filename;
            chown $cb_opts{owner}, $cb_opts{group}, $filename;
        }
        1;
    };

    return undef unless $self->retrieve_with_callback(
                                                      %opts,
                                                      callback => $callback,
                                                     );

    $rc;
}

=item retrieve_as_hash

Retrieves all the files associated with the given tag into memory.  The
hash (or hash-ref in scalar context) is returned.  To use a specific hash,
pass in a ref to it.

Keys to the hash are the filenames.  The values are hashes with keys of:
C<content> (the file contents), C<mode> (file mode), C<owner> (UID for the
file), and C<group> (GID for the file) if the filename is a real file, 
and a key of C<target> if the file is a symlink.

Parameters:

=over 4

=item tag

Required.  The tag to retrieve.

=item hash

If this parameter is specified, this hash ref will be used instead of creating
a new hash ref.  For example:

    my %files;
    my $ref = $rep->retrieve(tag => 'groupname', hash => \%files);
    # \%files == $ref

=item files

The list of files to be retrieved.  Defaults to all files.  This parameter
may be a simple scalar, or an array ref, e.g.,

    files => 'foo.txt'

or

    files => [ 'foo.txt' ]

are both the same.

=back

Returns undef if the retrieval failed.

=cut

sub retrieve_as_hash



( run in 2.712 seconds using v1.01-cache-2.11-cpan-71847e10f99 )