| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # file::dump Brik | ||
| 5 | # | ||
| 6 | package Metabrik::File::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::File::Write); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable read write) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | input => [ qw(file) ], | ||
| 20 | output => [ qw(file) ], | ||
| 21 | append => [ qw(0|1) ], | ||
| 22 | }, | ||
| 23 | attributes_default => { | ||
| 24 | append => 1, | ||
| 25 | }, | ||
| 26 | commands => { | ||
| 27 | read => [ qw(file) ], | ||
| 28 | write => [ qw($data|$data_ref|$data_list output|OPTIONAL) ], | ||
| 29 | }, | ||
| 30 | require_modules => { | ||
| 31 | 'Data::Dump' => [ ], | ||
| 32 | 'Metabrik::File::Read' => [ ], | ||
| 33 | }, | ||
| 34 | }; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub read { | ||
| 38 | my $self = shift; | ||
| 39 | my ($input) = @_; | ||
| 40 | |||
| 41 | $input ||= $self->input; | ||
| 42 | $self->brik_help_run_undef_arg('read', $input) or return; | ||
| 43 | |||
| 44 | my $fr = Metabrik::File::Read->new_from_brik_init($self) or return; | ||
| 45 | $fr->input($input); | ||
| 46 | $fr->encoding($self->encoding); | ||
| 47 | $fr->as_array(1); | ||
| 48 | $fr->strip_crlf(1); | ||
| 49 | |||
| 50 | $fr->open or return; | ||
| 51 | my $data = $fr->read or return; | ||
| 52 | $fr->close; | ||
| 53 | |||
| 54 | my @vars = (); | ||
| 55 | my $buf = ''; | ||
| 56 | for (@$data) { | ||
| 57 | $buf .= $_; | ||
| 58 | |||
| 59 | if (/^$/) { | ||
| 60 | push @vars, $buf; | ||
| 61 | $buf = ''; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | # Gather last remaining line, if any | ||
| 66 | if (length($buf)) { | ||
| 67 | push @vars, $buf; | ||
| 68 | $buf = ''; | ||
| 69 | } | ||
| 70 | |||
| 71 | my @res = (); | ||
| 72 | for (@vars) { | ||
| 73 | my $h = eval($_); | ||
| 74 | if ($@) { | ||
| 75 | chomp($@); | ||
| 76 | $self->log->warning("read: eval failed: $@"); | ||
| 77 | next; | ||
| 78 | } | ||
| 79 | push @res, $h; | ||
| 80 | } | ||
| 81 | |||
| 82 | return \@res; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub write { | ||
| 86 | my $self = shift; | ||
| 87 | my ($data, $output) = @_; | ||
| 88 | |||
| 89 | $output ||= $self->output; | ||
| 90 | $self->brik_help_run_undef_arg('write', $data) or return; | ||
| 91 | $self->brik_help_run_undef_arg('write', $output) or return; | ||
| 92 | |||
| 93 | $self->log->debug("write: data[$data]"); | ||
| 94 | |||
| 95 | $self->open($output) or return; | ||
| 96 | |||
| 97 | if (ref($data) eq 'ARRAY') { | ||
| 98 | for (@$data) { | ||
| 99 | my $r = $self->SUPER::write(Data::Dump::dump($_)."\n\n"); | ||
| 100 | if (! defined($r)) { | ||
| 101 | return $self->log->error("write: write failed"); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | my $r = $self->SUPER::write(Data::Dump::dump($data)."\n\n"); | ||
| 107 | if (! defined($r)) { | ||
| 108 | return $self->log->error("write: write failed"); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | $self->close; | ||
| 113 | |||
| 114 | return 1; | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ | ||