| 1 | # ABSTRACT: Role providing common output format options | ||||||||||
| 2 | |||||||||||
| 3 | package App::karr::Role::Output; | ||||||||||
| 4 | 3 | our $VERSION = '0.602'; | |||||||||
| 5 | 3 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use MooX::Options; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | # Loaded without importing: a Moo::Role composes every sub in this package into | ||||||||||
| 8 | # its consumers, so an imported json_encode would become a method on every | ||||||||||
| 9 | # command class. | ||||||||||
| 10 | 3 | ▶ | use App::karr::Encoding (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | 3 | option json => ( | |||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | doc => 'JSON output', | ||||||||||
| 16 | ); | ||||||||||
| 17 | |||||||||||
| 18 | # Characters out, not octets: STDOUT carries the CLI's :encoding(UTF-8) layer | ||||||||||
| 19 | # (App::karr::Encoding::enable_std_utf8), so encoding here as well would give an | ||||||||||
| 20 | # agent double-encoded JSON -- the machine-readable half of ticket #53. | ||||||||||
| 21 | 0 | ▶ | sub print_json { | ||||||||
|
Subroutine
print_json: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 22 | 0 | my ($self, $data) = @_; | |||||||||
| 23 | 0 | print App::karr::Encoding::json_encode($data) . "\n"; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | sub print_json_results { | ||||||||
|
Subroutine
print_json_results: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 29 | 0 | my ($self, @results) = @_; | |||||||||
| 30 | 0 | ▶ | return unless $self->json; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | ▶ | $self->print_json(@results == 1 ? $results[0] : \@results); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 3 | 1; | |||||||||
| 35 | |||||||||||
| 36 | __END__ | ||||||||||