| 1 | package Jifty::DBI::Filter::Storable; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw|Jifty::DBI::Filter|; | ||
| 7 | use Storable (); | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Jifty::DBI::Filter::Storable - Encodes arbitrary data using Storable | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This filter allows you to store arbitrary Perl data structures in a | ||
| 16 | column of type 'blob', using L<Storable> to serialize them. | ||
| 17 | |||
| 18 | =head2 encode | ||
| 19 | |||
| 20 | If value is defined, then encodes it using L<Storable/nfreeze>. Does | ||
| 21 | nothing if value is not defined. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | sub encode { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | my $value_ref = $self->value_ref; | ||
| 29 | return unless defined $$value_ref; | ||
| 30 | |||
| 31 | $Storable::Deparse = 1; | ||
| 32 | $$value_ref = Storable::nfreeze($value_ref); | ||
| 33 | } | ||
| 34 | |||
| 35 | =head2 decode | ||
| 36 | |||
| 37 | If value is defined, then decodes it using L<Storable/thaw>, otherwise | ||
| 38 | does nothing. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub decode { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | my $value_ref = $self->value_ref; | ||
| 46 | return unless defined $$value_ref; | ||
| 47 | |||
| 48 | # Storable doesn't take Unicode strings. | ||
| 49 | Encode::_utf8_off($$value_ref); | ||
| 50 | |||
| 51 | local $@; | ||
| 52 | $Storable::Eval = 1; | ||
| 53 | $$value_ref = eval { ${ Storable::thaw($$value_ref) } }; | ||
| 54 | } | ||
| 55 | |||
| 56 | =head1 SEE ALSO | ||
| 57 | |||
| 58 | L<Jifty::DBI::Filter>, L<Storable> | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | 1; |