File Coverage

blib/lib/Wikibase/Datatype/Print/Sense.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Print::Sense;
2              
3 7     7   348109 use base qw(Exporter);
  7         44  
  7         684  
4 7     7   51 use strict;
  7         23  
  7         129  
5 7     7   34 use warnings;
  7         15  
  7         203  
6              
7 7     7   1405 use Error::Pure qw(err);
  7         24113  
  7         193  
8 7     7   176 use Readonly;
  7         14  
  7         278  
9 7     7   1846 use Wikibase::Datatype::Print::Statement;
  7         32  
  7         323  
10 7     7   83 use Wikibase::Datatype::Print::Utils qw(print_glosses print_statements);
  7         27  
  7         349  
11 7     7   51 use Wikibase::Datatype::Print::Value::Monolingual;
  7         19  
  7         1408  
12              
13             Readonly::Array our @EXPORT_OK => qw(print);
14              
15             our $VERSION = 0.09;
16              
17             sub print {
18 4     4 1 6536 my ($obj, $opts_hr) = @_;
19              
20 4 100       29 if (! $obj->isa('Wikibase::Datatype::Sense')) {
21 1         8 err "Object isn't 'Wikibase::Datatype::Sense'.";
22             }
23              
24             # Id.
25 3         19 my @ret = (
26             'Id: '.$obj->id,
27             );
28              
29             # Glosses.
30 3         36 push @ret, print_glosses($obj, $opts_hr,
31             \&Wikibase::Datatype::Print::Value::Monolingual::print);
32              
33             # Statements.
34 3         36 push @ret, print_statements($obj, $opts_hr,
35             \&Wikibase::Datatype::Print::Statement::print);
36              
37 3 100       20 return wantarray ? @ret : (join "\n", @ret);
38             }
39              
40             1;
41              
42             __END__
43              
44             =pod
45              
46             =encoding utf8
47              
48             =head1 NAME
49              
50             Wikibase::Datatype::Print::Sense - Wikibase sense pretty print helpers.
51              
52             =head1 SYNOPSIS
53              
54             use Wikibase::Datatype::Print::Sense qw(print);
55              
56             my $pretty_print_string = print($obj, $opts_hr);
57              
58             =head1 SUBROUTINES
59              
60             =head2 C<print>
61              
62             my $pretty_print_string = print($obj, $opts_hr);
63              
64             Construct pretty print output for L<Wikibase::Datatype::Sense>
65             object.
66              
67             Returns string.
68              
69             =head1 ERRORS
70              
71             print():
72             Object isn't 'Wikibase::Datatype::Sense'.
73              
74             =head1 EXAMPLE
75              
76             =for comment filename=create_and_print_sense.pl
77              
78             use strict;
79             use warnings;
80              
81             use Unicode::UTF8 qw(decode_utf8 encode_utf8);
82             use Wikibase::Datatype::Print::Sense;
83             use Wikibase::Datatype::Sense;
84             use Wikibase::Datatype::Snak;
85             use Wikibase::Datatype::Statement;
86             use Wikibase::Datatype::Value::Item;
87             use Wikibase::Datatype::Value::Monolingual;
88             use Wikibase::Datatype::Value::String;
89              
90             # One sense for Czech noun 'pes'.
91             # https://www.wikidata.org/wiki/Lexeme:L469
92              
93             # Statements.
94             my $statement_item = Wikibase::Datatype::Statement->new(
95             # item for this sense (P5137) dog (Q144)
96             'snak' => Wikibase::Datatype::Snak->new(
97             'datatype' => 'wikibase-item',
98             'datavalue' => Wikibase::Datatype::Value::Item->new(
99             'value' => 'Q144',
100             ),
101             'property' => 'P5137',
102             ),
103             );
104             my $statement_image = Wikibase::Datatype::Statement->new(
105             # image (P5137) 'Canadian Inuit Dog.jpg'
106             'snak' => Wikibase::Datatype::Snak->new(
107             'datatype' => 'commonsMedia',
108             'datavalue' => Wikibase::Datatype::Value::String->new(
109             'value' => 'Canadian Inuit Dog.jpg',
110             ),
111             'property' => 'P18',
112             ),
113             );
114              
115             # Object.
116             my $obj = Wikibase::Datatype::Sense->new(
117             'glosses' => [
118             Wikibase::Datatype::Value::Monolingual->new(
119             'language' => 'en',
120             'value' => 'domesticated mammal related to the wolf',
121             ),
122             Wikibase::Datatype::Value::Monolingual->new(
123             'language' => 'cs',
124             'value' => decode_utf8('psovitá šelma chovaná jako domácí zvíře'),
125             ),
126             ],
127             'id' => 'ID',
128             'statements' => [
129             $statement_item,
130             $statement_image,
131             ],
132             );
133              
134             # Print.
135             print encode_utf8(scalar Wikibase::Datatype::Print::Sense::print($obj))."\n";
136              
137             # Output:
138             # Id: ID
139             # Glosses:
140             # domesticated mammal related to the wolf (en)
141             # psovitá šelma chovaná jako domácí zvíře (cs)
142             # Statements:
143             # P5137: Q144 (normal)
144             # P18: Canadian Inuit Dog.jpg (normal)
145              
146             =head1 DEPENDENCIES
147              
148             L<Error::Pure>,
149             L<Exporter>,
150             L<Readonly>,
151             L<Wikibase::Datatype::Print::Statement>,
152             L<Wikibase::Datatype::Print::Utils>,
153             L<Wikibase::Datatype::Print::Value::Monolingual>.
154              
155             =head1 SEE ALSO
156              
157             =over
158              
159             =item L<Wikibase::Datatype::Sense>
160              
161             Wikibase sense datatype.
162              
163             =back
164              
165             =head1 REPOSITORY
166              
167             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
168              
169             =head1 AUTHOR
170              
171             Michal Josef Špaček L<mailto:skim@cpan.org>
172              
173             L<http://skim.cz>
174              
175             =head1 LICENSE AND COPYRIGHT
176              
177             © 2020-2023 Michal Josef Špaček
178              
179             BSD 2-Clause License
180              
181             =head1 VERSION
182              
183             0.09
184              
185             =cut
186