| 1 |
=head1 NAME |
|---|
| 2 |
|
|---|
| 3 |
Cache::IOString - wrapper for IO::String to use in Cache implementations |
|---|
| 4 |
|
|---|
| 5 |
=head1 DESCRIPTION |
|---|
| 6 |
|
|---|
| 7 |
This module implements a derived class of IO::String that handles access |
|---|
| 8 |
modes and allows callback on close. It is for use by Cache implementations |
|---|
| 9 |
and should not be used directly. |
|---|
| 10 |
|
|---|
| 11 |
=cut |
|---|
| 12 |
package Cache::IOString; |
|---|
| 13 |
|
|---|
| 14 |
require 5.006; |
|---|
| 15 |
use strict; |
|---|
| 16 |
use warnings; |
|---|
| 17 |
use IO::String; |
|---|
| 18 |
|
|---|
| 19 |
our @ISA = qw(IO::String); |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
sub open { |
|---|
| 23 |
my $self = shift; |
|---|
| 24 |
my ($dataref, $mode, $close_callback) = @_; |
|---|
| 25 |
return $self->new(@_) unless ref($self); |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
my $read; |
|---|
| 29 |
my $write; |
|---|
| 30 |
if ($mode =~ /^\+?>>?$/) { |
|---|
| 31 |
$write = 1; |
|---|
| 32 |
$read = 1 if $mode =~ /^\+/; |
|---|
| 33 |
} |
|---|
| 34 |
elsif ($mode =~ /^\+?<$/) { |
|---|
| 35 |
$read = 1; |
|---|
| 36 |
$write = 1 if $mode =~ /^\+/; |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
$self->SUPER::open($dataref); |
|---|
| 40 |
|
|---|
| 41 |
*$self->{_cache_read} = $read; |
|---|
| 42 |
*$self->{_cache_write} = $write; |
|---|
| 43 |
*$self->{_cache_close_callback} = $close_callback; |
|---|
| 44 |
|
|---|
| 45 |
if ($write) { |
|---|
| 46 |
if ($mode =~ /^\+?>>$/) { |
|---|
| 47 |
|
|---|
| 48 |
$self->seek(0, 2); |
|---|
| 49 |
} |
|---|
| 50 |
elsif ($mode =~ /^\+?>$/) { |
|---|
| 51 |
|
|---|
| 52 |
$self->truncate(0); |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
return $self; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
sub close { |
|---|
| 60 |
my $self = shift; |
|---|
| 61 |
delete *$self->{_cache_read}; |
|---|
| 62 |
delete *$self->{_cache_write}; |
|---|
| 63 |
*$self->{_cache_close_callback}->($self) if *$self->{_cache_close_callback}; |
|---|
| 64 |
delete *$self->{_cache_close_callback}; |
|---|
| 65 |
$self->SUPER::close(@_); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
sub DESTROY { |
|---|
| 69 |
my $self = shift; |
|---|
| 70 |
*$self->{_cache_close_callback}->($self) if *$self->{_cache_close_callback}; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
sub pad { |
|---|
| 74 |
my $self = shift; |
|---|
| 75 |
return undef unless *$self->{_cache_write}; |
|---|
| 76 |
return $self->SUPER::pad(@_); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
sub getc { |
|---|
| 80 |
my $self = shift; |
|---|
| 81 |
return undef unless *$self->{_cache_read}; |
|---|
| 82 |
return $self->SUPER::getc(@_); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
sub ungetc { |
|---|
| 86 |
my $self = shift; |
|---|
| 87 |
return undef unless *$self->{_cache_read}; |
|---|
| 88 |
return $self->SUPER::ungetc(@_); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
sub seek { |
|---|
| 92 |
my $self = shift; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
return $self->SUPER::setpos(@_) unless *$self->{_cache_write}; |
|---|
| 96 |
return $self->SUPER::seek(@_); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
sub getline { |
|---|
| 100 |
my $self = shift; |
|---|
| 101 |
return undef unless *$self->{_cache_read}; |
|---|
| 102 |
return $self->SUPER::getline(@_); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
sub truncate { |
|---|
| 106 |
my $self = shift; |
|---|
| 107 |
return undef unless *$self->{_cache_write}; |
|---|
| 108 |
return $self->SUPER::truncate(@_); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
sub read { |
|---|
| 112 |
my $self = shift; |
|---|
| 113 |
return undef unless *$self->{_cache_read}; |
|---|
| 114 |
return $self->SUPER::read(@_); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
sub write { |
|---|
| 118 |
my $self = shift; |
|---|
| 119 |
return undef unless *$self->{_cache_write}; |
|---|
| 120 |
return $self->SUPER::write(@_); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
*GETC = \&getc; |
|---|
| 124 |
*READ = \&read; |
|---|
| 125 |
*WRITE = \&write; |
|---|
| 126 |
*SEEK = \&seek; |
|---|
| 127 |
*CLOSE = \&close; |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
1; |
|---|
| 131 |
__END__ |
|---|
| 132 |
|
|---|
| 133 |
=head1 SEE ALSO |
|---|
| 134 |
|
|---|
| 135 |
Cache::Entry, Cache::File, Cache::RemovalStrategy |
|---|
| 136 |
|
|---|
| 137 |
=head1 AUTHOR |
|---|
| 138 |
|
|---|
| 139 |
Chris Leishman <chris@leishman.org> |
|---|
| 140 |
Based on work by DeWitt Clinton <dewitt@unto.net> |
|---|
| 141 |
|
|---|
| 142 |
=head1 COPYRIGHT |
|---|
| 143 |
|
|---|
| 144 |
Copyright (C) 2003-2006 Chris Leishman. All Rights Reserved. |
|---|
| 145 |
|
|---|
| 146 |
This module is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
|---|
| 147 |
either expressed or implied. This program is free software; you can |
|---|
| 148 |
redistribute or modify it under the same terms as Perl itself. |
|---|
| 149 |
|
|---|
| 150 |
$Id: IOString.pm,v 1.3 2006/01/31 15:23:58 caleishm Exp $ |
|---|
| 151 |
|
|---|
| 152 |
=cut |
|---|
| 153 |
|
|---|