Bencode(3pm) | User Contributed Perl Documentation | Bencode(3pm) |
Bencode - BitTorrent serialisation format
use Bencode qw( bencode bdecode ); my $bencoded = bencode { 'age' => 25, 'eyes' => 'blue' }; print $bencoded, "\n"; my $decoded = bdecode $bencoded;
This module implements the BitTorrent bencode serialisation format, as described in <http://www.bittorrent.org/beps/bep_0003.html#bencoding>.
Takes data to be encoded as a single argument which may be a scalar, or may be a reference to either a scalar, an array or a hash. Arrays and hashes may in turn contain values of these same types. Plain scalars that look like canonically represented integers will be serialised as such. To bypass the heuristic and force serialisation as a string, use a reference to a scalar.
The second argument is optional (in which case it defaults to "str") and specifies how to treat "undef" values. You can pick one of three options:
Croaks on unhandled data types.
Takes a string and returns the corresponding deserialised data structure.
If you pass a true value for the second option, it will disregard the sort order of dict keys. This violation of the bencode format is somewhat common.
If you pass an integer for the third option, it will croak when attempting to parse dictionaries nested deeper than this level, to prevent DoS attacks using maliciously crafted input.
Croaks on malformed data.
You may also get this error if a malformed item follows.
The format does not support this.
Strings and numbers are practically indistinguishable in Perl, so "bencode()" has to resort to a heuristic to decide how to serialise a scalar. This cannot be fixed.
Aristotle Pagaltzis <pagaltzis@gmx.de>
This software is copyright (c) 2017 by Aristotle Pagaltzis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2022-08-18 | perl v5.34.0 |