File Coverage

blib/lib/Querylet/Output.pm
Criterion Covered Total %
statement 15 15 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 25 25 100.0


line stmt bran cond sub pod time code
1 1     1   1403 use strict;
  1         2  
  1         30  
2 1     1   5 use warnings;
  1         3  
  1         41  
3             package Querylet::Output 0.402;
4             # ABSTRACT: generic output handler for Querlet::Query
5              
6 1     1   5 use Carp;
  1         1  
  1         187  
7              
8             #pod =head1 SYNOPSIS
9             #pod
10             #pod This is an abstract base class, meant for subclassing.
11             #pod
12             #pod package Querylet::Output::Tabbed;
13             #pod use base qw(Querylet::Output);
14             #pod
15             #pod sub default_type { 'tabbed' }
16             #pod sub handler { \&as_tabbed }
17             #pod
18             #pod sub as_tabbed {
19             #pod my ($query) = @_;
20             #pod my @output;
21             #pod push @output, join("\t", @{$query->columns});
22             #pod push @output, join("\t", @{$_{@{$query->colummns}}}) for @{$query->results};
23             #pod return join("\n", @output);
24             #pod }
25             #pod
26             #pod 1;
27             #pod
28             #pod Then, in a querylet:
29             #pod
30             #pod use Querylet::Output::Tabbed;
31             #pod
32             #pod output format: tabbed
33             #pod
34             #pod Or, to override the registered type:
35             #pod
36             #pod use Querylet::Output::Tabbed 'tsv';
37             #pod
38             #pod output format: tsv
39             #pod
40             #pod =head1 DESCRIPTION
41             #pod
42             #pod This class provides a simple way to write output handlers for Querylet, mostly
43             #pod by providing an import routine that will register the handler with the
44             #pod type-name requested by the using script.
45             #pod
46             #pod The methods C and C must exist, as described below.
47             #pod
48             #pod =head1 IMPORT
49             #pod
50             #pod Querylet::Output provides an C method that will register the handler
51             #pod when the module is imported. If an argument is given, it will be used as the
52             #pod type name to register. Otherwise, the result of C is used.
53             #pod
54             #pod =cut
55              
56             sub import {
57 2     2   2187 my ($class, $type) = @_;
58 2 100       9 $type = $class->default_type unless $type;
59              
60 2         8 my $handler = $class->handler;
61              
62 2         20 Querylet::Query->register_output_handler($type => $handler);
63             }
64              
65             #pod =head1 METHODS
66             #pod
67             #pod =over 4
68             #pod
69             #pod =item default_type
70             #pod
71             #pod This method returns the name of the type for which the output handler will be
72             #pod registered if no override is given.
73             #pod
74             #pod =cut
75              
76 1     1 1 282 sub default_type { croak "default_type method unimplemented" }
77              
78             #pod =item handler
79             #pod
80             #pod This method returns a reference to the handler, which will be used to register
81             #pod the handler.
82             #pod
83             #pod =cut
84              
85 1     1 1 1162 sub handler { croak "handler method unimplemented" }
86              
87             #pod =back
88             #pod
89             #pod =cut
90              
91             "I do endeavor to give satisfaction, sir.";
92              
93             __END__