| 1 | package Data::Serializer::Persistent; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use vars qw($VERSION @ISA); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | $VERSION = '0.01'; | |||||||||
| 11 | |||||||||||
| 12 | 630 | ▶ | sub _store { | ||||||||
|
Subroutine
_store: called CC=5 SCAR=16.2
| |||||||||||
| 13 | 630 | my $self = (shift); | |||||||||
| 14 | 630 | my $data = (shift); | |||||||||
| 15 | 630 | my $file_or_fh = (shift); | |||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 630 | ▶ | if (ref($file_or_fh)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | #it is a file handle so print straight to it | ||||||||||
| 20 | 126 | print $file_or_fh $self->{parent}->serialize($data), "\n"; | |||||||||
| 21 | #We didn't open the filehandle, so we shouldn't close it. | ||||||||||
| 22 | } else { | ||||||||||
| 23 | #it is a file, so open it | ||||||||||
| 24 | 504 | my ($mode,$perm) = @_; | |||||||||
| 25 | 504 | ▶ | unless (defined $mode) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 504 | $mode = O_CREAT|O_WRONLY; | |||||||||
| 27 | } | ||||||||||
| 28 | 504 | ▶ | unless (defined $perm) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 504 | $perm = 0600; | |||||||||
| 30 | } | ||||||||||
| 31 | 504 | my $fh = new IO::File; | |||||||||
| 32 | 504 | ▶ | $fh->open($file_or_fh, $mode,$perm) || croak "Cannot write to $file_or_fh: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 504 | print $fh $self->{parent}->serialize($data), "\n"; | |||||||||
| 34 | 504 | $fh->close(); | |||||||||
| 35 | } | ||||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 630 | ▶ | sub _retrieve { | ||||||||
|
Subroutine
_retrieve: called CC=3 SCAR=11.0
| |||||||||||
| 39 | 630 | my $self = (shift); | |||||||||
| 40 | 630 | my $file_or_fh = (shift); | |||||||||
| 41 | 630 | ▶ | if (ref($file_or_fh)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | #it is a file handle so read straight from it | ||||||||||
| 43 | 126 | my $input = join('', <$file_or_fh>); | |||||||||
| 44 | 126 | chomp($input); | |||||||||
| 45 | 126 | return $self->{parent}->deserialize($input); | |||||||||
| 46 | #We didn't open the filehandle, so we shouldn't close it. | ||||||||||
| 47 | } else { | ||||||||||
| 48 | 504 | my $fh = new IO::File; | |||||||||
| 49 | 504 | ▶ | $fh->open($file_or_fh, O_RDONLY) || croak "Cannot read from $file_or_fh: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 504 | my $input = join('', <$fh>); | |||||||||
| 51 | 504 | chomp($input); | |||||||||
| 52 | 504 | $fh->close; | |||||||||
| 53 | 504 | return $self->{parent}->deserialize($input); | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | 2 | 1; | |||||||||
| 60 | __END__ | ||||||||||