File Coverage

blib/lib/Text/TNetstrings.pm
Criterion Covered Total %
statement 21 27 77.7
branch 4 8 50.0
condition 1 3 33.3
subroutine 5 5 100.0
pod n/a
total 31 43 72.0


line stmt bran cond sub pod time code
1             package Text::TNetstrings;
2 2     2   49945 use strict;
  2         6  
  2         75  
3 2     2   11 use warnings;
  2         9  
  2         64  
4 2     2   11 use base qw(Exporter);
  2         12  
  2         254  
5              
6             =head1 NAME
7              
8             Text::TNetstrings - Data serialization using typed netstrings.
9              
10             =head1 VERSION
11              
12             Version 1.2.0
13              
14             =cut
15              
16 2     2   1732 use version 0.77; our $VERSION = version->declare("v1.2.0");
  2         5055  
  2         16  
17              
18             =head1 SYNOPSIS
19              
20             An implementation of the tagged netstring specification, a simple data
21             interchange format better suited to low-level network communication than
22             JSON. See http://tnetstrings.org/ for more details.
23              
24             use Text::TNetstrings qw(:all);
25              
26             my $data = encode_tnetstrings({"foo" => "bar"}) # => "12:3:foo,3:bar,}"
27             my $hash = decode_tnetstrings($data) # => {"foo" => "bar"}
28              
29             =head1 EXPORT
30              
31             =over
32              
33             =item C
34              
35             =item C
36              
37             =item C<:all>
38              
39             The "all" tag exports all the above subroutines.
40              
41             =back
42              
43             =cut
44              
45             our @EXPORT_OK = qw(encode_tnetstrings decode_tnetstrings $Useperl);
46             our %EXPORT_TAGS = (
47             "all" => \@EXPORT_OK,
48             );
49              
50             =head1 ENVIRONMENT
51              
52             =over
53              
54             =item C
55              
56             =item C
57              
58             Can be set to a boolean value which controls whether the pure Perl
59             implementation of C is used. The C
60             module is a dual implementation, with all functionality written in both
61             pure Perl and also in XS ('C'). By default, The XS version will be used
62             whenever possible, as it is much faster. This option allows you to
63             override the default behaviour.
64              
65             =item C
66              
67             Unless C or C is set, an attempt will
68             be made to load the XS module. If it can not be loaded it will fail
69             quietly and fall back to the pure Perl module. If C is
70             set a warning will be issued if loading the XS module fails.
71              
72             =back
73              
74             =cut
75              
76             BEGIN {
77 2   33 2   697 my $xs = $ENV{"TNETSTRINGS_XS"} || !($ENV{"TNETSTRINGS_PUREPERL"} || $ENV{"PERL_ONLY"});
78 2 50       12 if($xs) {
79 2 50       13 if($ENV{"TNETSTRINGS_XS"}) {
80 0         0 require Text::TNetstrings::XS;
81 0         0 *encode_tnetstrings = \&Text::TNetstrings::XS::encode_tnetstrings;
82 0         0 *decode_tnetstrings = \&Text::TNetstrings::XS::decode_tnetstrings;
83             } else {
84 2         7 eval {
85 2         1225 require Text::TNetstrings::XS;
86 2         9 *encode_tnetstrings = \&Text::TNetstrings::XS::encode_tnetstrings;
87 2         7 *decode_tnetstrings = \&Text::TNetstrings::XS::decode_tnetstrings;
88             };
89 2 50       10 $xs = 0 if $@;
90             }
91             }
92 2 50       140 if(!$xs) {
93 0           require Text::TNetstrings::PP;
94 0           *encode_tnetstrings = \&Text::TNetstrings::PP::encode_tnetstrings;
95 0           *decode_tnetstrings = \&Text::TNetstrings::PP::decode_tnetstrings;
96             }
97             };
98              
99             =head1 SUBROUTINES/METHODS
100              
101             =head2 encode_tnetstrings($data)
102              
103             Encode a scalar, hash or array into TNetstring format.
104              
105             =head2 decode_tnetstrings($string)
106              
107             Decode TNetstring data into the appropriate scalar, hash or array. In
108             array context the remainder of the string will also be returned, e.g.:
109              
110             my ($data, $rest) = decode_tnetstrings("0:~foo"); #=> (undef, "foo")
111              
112             =head1 MAPPING
113              
114             =head2 Perl -> TNetstrings
115              
116             =over
117              
118             =item ARRAY
119              
120             Perl array references become TNetstring lists.
121              
122             =item HASH
123              
124             Perl hash references become TNetstring dictionaries. The TNetstring
125             specification does not dictate an ordering, thus Perl's pseudo-random
126             ordering is used.
127              
128             =item Unblessed
129              
130             Other unblessed references are not allowed, and an exception will be
131             thrown. This uncludes Cs, Cs, etc.
132              
133             =item boolean::true, boolean::false
134              
135             These special values become TNetstring true and false values,
136             respectively.
137              
138             =item Blessed Objects
139              
140             Blessed objects are not representable in TNetstrings, and thus an
141             exception will be thrown.
142              
143             =item Scalars
144              
145             Due to Perl not having distinct string, floating point or fixed point
146             integers, the encoded type is a best guest. Undefined scalars will be
147             encoded as TNetstring nulls (c<0:~>), values which look like a floating
148             point number are encoded as floats, values which look like a fixed point
149             integer are encoded as integers, and everything else is encoded as
150             a string (using stringification).
151              
152             =back
153              
154             =cut
155              
156             =head1 AUTHOR
157              
158             Sebastian Nowicki
159              
160             =head1 SEE ALSO
161              
162             L for the TNetstrings specification.
163              
164             L for better performance.
165              
166             L if XS is not supported.
167              
168             =head1 BUGS
169              
170             Please report any bugs or feature requests to C
171             rt.cpan.org>, or through the web interface at
172             L.
173             I will be notified, and then you'll automatically be notified of
174             progress on your bug as I make changes.
175              
176             =head1 SUPPORT
177              
178             You can find documentation for this module with the perldoc command.
179              
180             perldoc Text::TNetstrings
181              
182              
183             You can also look for information at:
184              
185             =over
186              
187             =item * RT: CPAN's request tracker (report bugs here)
188              
189             L
190              
191             =item * AnnoCPAN: Annotated CPAN documentation
192              
193             L
194              
195             =item * CPAN Ratings
196              
197             L
198              
199             =item * Search CPAN
200              
201             L
202              
203             =item * GitHub
204              
205             L
206              
207             =back
208              
209             =head1 CHANGES
210              
211             =head2 v1.2.0
212              
213             =over
214              
215             =item Support for encoding L objects.
216              
217             =back
218              
219             =head2 v1.1.1
220              
221             =over
222              
223             =item Performance improvements
224              
225             =item Bug fixes for strings containing C bytes
226              
227             =back
228              
229             =head2 v1.1.0
230              
231             =over
232              
233             =item XS module for improved performance
234              
235             =back
236              
237             =head2 v1.0.1
238              
239             =over
240              
241             =item Performance improvements
242              
243             =back
244              
245             =head2 v1.0.0
246              
247             =over
248              
249             =item Initial release
250              
251             =back
252              
253             =head1 LICENSE AND COPYRIGHT
254              
255             Copyright 2011 Sebastian Nowicki.
256              
257             This program is distributed under the MIT (X11) License:
258             L
259              
260             Permission is hereby granted, free of charge, to any person
261             obtaining a copy of this software and associated documentation
262             files (the "Software"), to deal in the Software without
263             restriction, including without limitation the rights to use,
264             copy, modify, merge, publish, distribute, sublicense, and/or sell
265             copies of the Software, and to permit persons to whom the
266             Software is furnished to do so, subject to the following
267             conditions:
268              
269             The above copyright notice and this permission notice shall be
270             included in all copies or substantial portions of the Software.
271              
272             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
273             EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
274             OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
275             NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
276             HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
277             WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
278             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
279             OTHER DEALINGS IN THE SOFTWARE.
280              
281             =cut
282              
283             1;
284