| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # string::dump Brik | ||
| 5 | # | ||
| 6 | package Metabrik::String::Dump; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable encode decode) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | use_identation => [ qw(0|1) ], | ||
| 20 | use_base64 => [ qw(0|1) ], | ||
| 21 | strip_crlf => [ qw(0|1) ], | ||
| 22 | }, | ||
| 23 | attributes_default => { | ||
| 24 | use_identation => 1, | ||
| 25 | use_base64 => 0, | ||
| 26 | strip_crlf => 0, | ||
| 27 | }, | ||
| 28 | commands => { | ||
| 29 | encode => [ qw($data) ], | ||
| 30 | decode => [ qw($data) ], | ||
| 31 | }, | ||
| 32 | require_modules => { | ||
| 33 | 'Data::Dump' => [ ], | ||
| 34 | }, | ||
| 35 | }; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub encode { | ||
| 39 | my $self = shift; | ||
| 40 | my ($data) = @_; | ||
| 41 | |||
| 42 | $self->brik_help_run_undef_arg('encode', $data) or return; | ||
| 43 | |||
| 44 | if (! $self->use_identation) { | ||
| 45 | $Data::Dump::INDENT = ""; # No indentation shorten length | ||
| 46 | } | ||
| 47 | if (! $self->use_base64) { | ||
| 48 | $Data::Dump::TRY_BASE64 = 0; # Never encode in base64 | ||
| 49 | } | ||
| 50 | |||
| 51 | my $encoded = Data::Dump::dump($data); | ||
| 52 | |||
| 53 | if ($self->strip_crlf) { | ||
| 54 | $encoded =~ s{\n}{}g; | ||
| 55 | } | ||
| 56 | |||
| 57 | return $encoded; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub decode { | ||
| 61 | my $self = shift; | ||
| 62 | my ($data) = @_; | ||
| 63 | |||
| 64 | $self->brik_help_run_undef_arg('decode', $data) or return; | ||
| 65 | |||
| 66 | my $decoded = eval($data); | ||
| 67 | |||
| 68 | return $decoded; | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | |||
| 73 | __END__ | ||