blib/lib/Convert/Bencode_XS.pm

filter: Statementstmt 100% 17 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 21 / 21 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Bencode_XS.xs ↑ Summary
1 package Convert::Bencode_XS;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.06';
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use base qw(Exporter DynaLoader);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 __PACKAGE__->bootstrap($VERSION);
12  
13 2 our @EXPORT_OK = qw(&bencode &bdecode &cleanse $COERCE);
14 2 our %EXPORT_TAGS = (
15 all => \@EXPORT_OK,
16 code => [qw(&bencode &bdecode)],
17 );
18  
19 2 our $COERCE = 1;
20  
21 =head1 NAME
22  
23 Convert::Bencode_XS - Faster conversions to/from Bencode format
24  
25 =head1 SYNOPSIS
26  
27 use Convert::Bencode_XS qw(bencode bdecode);
28 use Data::Dumper;
29
30 print "Serializing:\n", bencode([123, [''], "XXX"]), "\n\n";
31
32 print Dumper bdecode('d3:fool3:bar4:stube6:numberi123ee');
33  
34 __END__
35 Serializing:
36 li123el0:e3:XXXe
37  
38 $VAR1 = {
39 'number' => '123',
40 'foo' => [
41 'bar',
42 'stub'
43 ]
44 };
45
46  
47 =head1 DESCRIPTION
48  
49 =over 4
50  
51 =item bencode($stuff)
52  
53 Returns a bencoded string representing what's in $stuff. $stuff can be
54 either a scalar, an array reference or a hash reference. Every nesting of
55 these data structures is allowed, other ones will croak.
56  
57 =item bdecode($bencoded)
58  
59 Returns a Perl data structure: it could be either a scalar, array reference
60 or hash reference depending on what's in $bencoded. Dictionaries are
61 converted in hashes, lists in arrays, scalars in strings.
62 If $COERCE (see below) is set
63 to a false value then scalars encoded like integers will be cleanse() before
64 being returned so that a re-serialization of the structure will give back
65 exactly the same bencoded string.
66  
67 =back
68  
69 =head1 TO COERCE AND TO CLEANSE
70  
71 Read on just if you are having problems serializing some data using this module:
72 it should work "as is" for 99% of cases. But if you're unlucky enough
73 maybe you need to read this chapter.
74  
75 The original definition of the Bencode protocol poses some problems
76 when ported to
77 languages other than Python, cause:
78  
79 1) there is a distinction between integers and strings
80  
81 2) integers are allowed to be any length.
82  
83 This is kinda contradictory so we have to come up with specialized
84 solutions to serialize certain types of data. For instance, strings that
85 looks like integers. This is cause there is little distinction between the two
86 in Perl. So, by default, bencode() will serialize all strings that looks like
87 integers as integers. Example:
88  
89 print bencode("123");
90 # outputs "i123e"
91  
92 If you don't want this to happen you can do this:
93  
94 $Convert::Bencode_XS::COERCE = 0; #this is 1 by default
95 print bencode("123");
96 # outputs "3:123"
97  
98 Setting $Convert::Bencode_XS::COERCE to a false value will serialize everything
99 that is a string as a string. But what about numbers? If they are hardcoded
100 into your program
101 there should be no problem. Otherwise you need to cleanse them. Example:
102  
103 use Convert::Bencode_XS qw(:all); # imports also cleanse() and $COERCE
104
105 $COERCE = 0;
106  
107 print bencode(123);
108 # outputs "i123e"
109
110 my ($num) = "abc123def" =~ /(\d+)/;
111 print bencode($num);
112 # outputs "3:123", but we know it is a number!
113 cleanse($num); # cleanse() to the rescue!
114 print bencode($num);
115 # outputs "i123e"
116  
117 Problems may arise if you want to use a arbitrary sequence of integers as
118 a real integer, mainly because it could surpass the maximum allowed by
119 your platform. (At the moment there is no solution for that). See the tests
120 in this distribution to have a better idea of what works and what not.
121  
122 =head1 WHY?
123  
124 Convert::Bencode_XS exists for a couple of reasons, first of all performance.
125 Especially bdecode() is between 10 and 200 times faster than
126 Convert::Bencode version (depending on file):
127 the great speed increase is in part due to the iterative
128 algorithm used. bencode() is written in C for better performance, but
129 it still uses a recursive algorithm. It manages to be
130 around 3 to 5 times faster than Convert::Bencode version.
131 Check out the "extras" directory in this distribution for benchmarks.
132  
133 The second reason is fun and i wished to try out something i learnt about XS
134 programming.
135  
136 =head1 BUGS
137  
138 =head2 In bencode()
139  
140 - No detection of recursive references yet
141  
142 Next come not real BUGS but more liberal interpretation of the protocol:
143  
144 - Hashes keys are forced to be strings. So if we find a number we don't
145 croak, but we use it as a string.
146  
147 - Strings like "007" will be treated as strings and encoded as such
148  
149  
150 =head1 SEE ALSO
151  
152 The Bencode format is described at
153 http://bitconjurer.org/BitTorrent/protocol.html
154  
155 The original Python bencode and bdecode functions can be found in file
156 bencode.py in the BitTorrent sources.
157  
158 See also Convert::Bencode by R. Kyle Murphy for a PurePerl implementation.
159  
160 =head1 AUTHOR
161  
162 Giulio Motta, E<lt>giulienk@cpan.orgE<gt>
163  
164 =head1 COPYRIGHT AND LICENSE
165  
166 Copyright (C) 2003-2006 by Giulio Motta
167  
168 This library is free software; you can redistribute it and/or modify
169 it under the same terms as Perl itself, either Perl version 5.8.1 or,
170 at your option, any later version of Perl 5 you may have available.
171  
172 =cut
173  
174 2 1;
« Bencode_XS.xs ↑ Summary