| 1 | package Catmandu::Interactive; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Catmandu; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | has in => ( | ||||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||
| 13 | default => sub { | ||||||||||||||||||||||||||||
| 14 | 0 | ▶ | Catmandu::Util::io(\*STDIN); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | 1 | ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has out => ( | ||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | default => sub { | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | Catmandu::Util::io( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | \*STDOUT, | ||||||||||||||||||||||||||||
| 23 | mode => 'w', | ||||||||||||||||||||||||||||
| 24 | binmode => ':encoding(utf-8)' | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | 1 | ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | has silent => (is => 'ro'); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | has exporter => (is => 'ro', default => sub {'YAML'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | has exporter_args => (is => 'ro', default => sub {+{}}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | has header => ( | ||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | default => sub { | ||||||||||||||||||||||||||||
| 38 | 5 | ▶ | "\e[36m\n" | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | . " A_A ____ _ _ \n" | ||||||||||||||||||||||||||||
| 40 | . " (-.-) / ___|__ _| |_ _ __ ___ __ _ _ __ __| |_ _ \n" | ||||||||||||||||||||||||||||
| 41 | . " |-| | | / _` | __| '_ ` _ \\ / _` | '_ \\ / _` | | | | \n" | ||||||||||||||||||||||||||||
| 42 | . " / \\ | |__| (_| | |_| | | | | | (_| | | | | (_| | |_| | \n" | ||||||||||||||||||||||||||||
| 43 | . " | | \\____\\__,_|\\__|_| |_| |_|\\__,_|_| |_|\\__,_|\\__,_|\n" | ||||||||||||||||||||||||||||
| 44 | . " | || | | \\___ version: $Catmandu::VERSION \n" | ||||||||||||||||||||||||||||
| 45 | . " \\_||_/_/ \e[0m\n" | ||||||||||||||||||||||||||||
| 46 | . " \n" | ||||||||||||||||||||||||||||
| 47 | . "Commands: | Interactive support is still \n" | ||||||||||||||||||||||||||||
| 48 | . " \\h - the fix history | experimental. Run: \n" | ||||||||||||||||||||||||||||
| 49 | . " \\r - repeat the previous fix | \$ catmandu run <your_fix_script> \n" | ||||||||||||||||||||||||||||
| 50 | . " \\q - quit | to access all Catmandu features \n"; | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 1 | ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | has data => (is => 'rw', default => sub {+{}}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 5 | ▶ | has _history => (is => 'ro', default => sub {[]}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub run { | ||||||||||||||||||||||||||||
| 59 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=12 SCAR=26.3
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 5 | my $keep_reading = 0; | |||||||||||||||||||||||||||
| 62 | 5 | my $buffer = ''; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 5 | $self->head; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 5 | $self->prompt; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | ▶ | while (my $line = $self->in->getline) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 10 | ▶ | if ($line =~ /^\\(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | next if length $buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | my ($command, $args) = split(/\s+/, $1, 2); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 3 | ▶ | if ($command eq 'h') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 75 | 1 | $self->cmd_history; | |||||||||||||||||||||||||||
| 76 | 1 | $self->prompt('fix'); | |||||||||||||||||||||||||||
| 77 | 1 | next; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | elsif ($command eq 'r') { | ||||||||||||||||||||||||||||
| 80 | 1 | ▶ | if (@{$self->_history} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | $line = $self->_history->[-1]; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 0 | $self->prompt('fix'); | |||||||||||||||||||||||||||
| 85 | 0 | next; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | elsif ($command eq 'q') { | ||||||||||||||||||||||||||||
| 89 | 1 | last; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 0 | $self->error("unknown command $command"); | |||||||||||||||||||||||||||
| 93 | 0 | $self->prompt('fix'); | |||||||||||||||||||||||||||
| 94 | 0 | next; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 8 | ▶ | $line = "$buffer$line" if length $buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 8 | ▶ | if (length $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 8 | my ($fixes, $keep_reading, $error) | |||||||||||||||||||||||||||
| 102 | = $self->parse_fixes($line, $keep_reading); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 8 | ▶ | if ($error) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 105 | 2 | $buffer = ''; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | elsif ($keep_reading == 0) { | ||||||||||||||||||||||||||||
| 108 | 6 | my $fixer = Catmandu::Fix->new(fixes => $fixes); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 6 | $self->data($fixer->fix($self->data)); | |||||||||||||||||||||||||||
| 111 | 6 | $self->export; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | push(@{$self->_history}, $line); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 6 | $buffer = ''; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | else { | ||||||||||||||||||||||||||||
| 118 | 0 | $buffer = $line; | |||||||||||||||||||||||||||
| 119 | 0 | $self->prompt('...'); | |||||||||||||||||||||||||||
| 120 | 0 | next; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 8 | $self->prompt('fix'); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub cmd_history { | ||||||||||||||||||||||||||||
| 129 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cmd_history: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | $self->out->printf(join("", @{$self->_history})); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub head { | ||||||||||||||||||||||||||||
| 135 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
head: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 5 | ▶ | $self->out->printf("%s\n", $self->header) unless $self->silent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub error { | ||||||||||||||||||||||||||||
| 141 | 2 | ▶ | my ($self, $txt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 2 | ▶ | $self->out->print("ERROR: $txt\n") unless $self->silent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub prompt { | ||||||||||||||||||||||||||||
| 146 | 14 | ▶ | my ($self, $txt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
prompt: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 147 | 14 | ▶ | $txt //= 'fix'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$txt //= "fix"
MC/DC100%1/1
$txt //= "fix"
$txt
Truth table100%2/2
A
A= $txt
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 14 | ▶ | $self->out->printf("\e[35m%s > \e[0m", $txt) unless $self->silent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub export { | ||||||||||||||||||||||||||||
| 153 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
export: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | my $exporter = Catmandu->exporter( | ||||||||||||||||||||||||||||
| 155 | $self->exporter, | ||||||||||||||||||||||||||||
| 156 | 6 | %{$self->exporter_args}, | |||||||||||||||||||||||||||
| 157 | fh => $self->out | ||||||||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||||||||
| 159 | 6 | $exporter->add($self->data); | |||||||||||||||||||||||||||
| 160 | 6 | $exporter->commit; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub parse_fixes { | ||||||||||||||||||||||||||||
| 164 | 8 | ▶ | my ($self, $string, $keep_reading) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_fixes: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 8 | my $parser = Catmandu::Fix::Parser->new; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 8 | my $fixes; | |||||||||||||||||||||||||||
| 169 | 8 | my $error = 0; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | try { | ||||||||||||||||||||||||||||
| 172 | 8 | ▶ | $fixes = $parser->parse($string); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 173 | 6 | $keep_reading = 0; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | catch { | ||||||||||||||||||||||||||||
| 176 | 2 | ▶ | if (ref($_) eq 'Catmandu::FixParseError' | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.7
Branch: 1 branch
Condition33%1/3
ref $_ eq "Catmandu::FixParseError" and $_->message =~ /Can't use an undefined value as a SCALAR reference at/
MC/DC0%0/2
ref $_ eq "Catmandu::FixParseError" and $_->message =~ /Can't use an undefined value as a SCALAR reference at/
ref $_ eq "Catmandu::FixParseError"
$_->message =~ /Can't use an undefined value as a SCALAR reference at/
Truth table33%1/3
A and B
A= ref $_ eq "Catmandu::FixParseError"B= $_->message =~ /Can't use an undefined value as a SCALAR reference at/
| |||||||||||||||||||||||||||||
| 177 | && $_->message | ||||||||||||||||||||||||||||
| 178 | =~ /Can't use an undefined value as a SCALAR reference at/) | ||||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||
| 180 | 0 | $keep_reading = 1; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | else { | ||||||||||||||||||||||||||||
| 183 | 2 | $_ =~ s/\n.*//g; | |||||||||||||||||||||||||||
| 184 | 2 | $self->error($_); | |||||||||||||||||||||||||||
| 185 | 2 | $error = 1; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 8 | }; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 8 | return ($fixes, $keep_reading, $error); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 1 | 1; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =pod | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 NAME | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Catmandu::Interactive - An interactive command line interpreter of the Fix language | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # On the command line | ||||||||||||||||||||||||||||
| 205 | catmandu run | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # Or, in Perl | ||||||||||||||||||||||||||||
| 208 | use Catmandu::Interactive; | ||||||||||||||||||||||||||||
| 209 | use Getopt::Long; | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | my $exporter = 'YAML'; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | GetOptions("exporter=s" => \$exporter); | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | my $app = Catmandu::Interactive->new(exporter => $exporter); | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | $app->run(); | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | This module provide a simple interactive interface to the Catmandu Fix language. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =over | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =item in | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Read input from an IO::Handle | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =item out | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Write output to an IO::Handle | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =item silent | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | If set true, then no headers or prompts are printed | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =item data | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | A hash containing the input record | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item exporter | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | The name of an exporter package | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =item exporter_args | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | The options for the exporter | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =back | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head1 METHODS | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head2 run | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Run the interactive environment. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | L<Catmandu> | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||