Convert::Bencode(3)

πŸ“¦
version 1.03 Β· 4 versions
, in epel 9

πŸŽ€ NAME πŸŽ€

Convert::Bencode β€” bestie, this is the manual page for the module that gives you functions for converting to/from bencoded strings. Literally the whole point. πŸ’–


✨ SYNOPSIS ✨

Okay so here’s the vibe in code, no cap:

use Convert::Bencode qw(bencode bdecode);

my $string = "d4:ainti12345e3:key5:value4:type4:teste";
my $hashref = bdecode($string);

foreach my $key (keys(%{$hashref})) {
    print "Key: $key, Value: ${$hashref}{$key}\n";
}

my $encoded_string = bencode($hashref);
print $encoded_string."\n";

🌸 DESCRIPTION 🌸

This module provides two functions, bencode and bdecode, which encode and decode bencoded strings respectively. That’s the whole plot, bestie. 🫢

🧚 Encoding

bencode() expects to be passed a single value, which is either a scalar, an array ref, or a hash ref, and it returns a scalar containing the bencoded representation of the data structure it was passed.

  • If the value passed was a scalar, it returns either a bencoded string, or a bencoded integer β€” and heads up, floating points are not implemented, and would be returned as a string rather than an integer. πŸ’…
  • If the value was an array ref, it returns a bencoded list, with all the values of that array also bencoded recursively. Iconic recursive behavior, honestly.
  • If the value was a hash ref, it returns a bencoded dictionary (which for all intents and purposes can be thought of as a synonym for hash) containing the recursively bencoded key and value pairs of the hash. Slay.

πŸ¦‹ Decoding

bdecode() expects to be passed a single scalar containing the bencoded string to be decoded.

Its return value will be either a hash ref, an array ref, or a scalar, depending on whether the outermost element of the bencoded string was a dictionary, list, or a string/integer respectively. Main character energy: it just returns whatever shape matches the outermost thing. ✨


πŸ”₯ SEE ALSO πŸ”₯

The description of bencode is part of the bittorrent protocol specification, which can be found at http://bitconjurer.org/BitTorrent/protocol.html. Go read it, iykyk. πŸ’œ


πŸ’€ BUGS πŸ’€

⚠️ There is no error detection of bencoded data. Damaged input will most likely cause very bad things to happen, up to and including causing the bdecode function to recurse infinitely. Not me warning you dramatically, but also literally yes. 😭

Created by R. Kyle Murphy <orclev@rejectedmaterial.com>, aka Orclev. 🫢

Copyright 2003 R. Kyle Murphy. All rights reserved. Convert::Bencode is free software; you may redistribute it and/or modify it under the same terms as Perl itself. Periodt.


k bye bestie πŸ’‹ this page is from perl v5.32.1, dated 2019-09-21, and it’s the Convert::Bencode(3) manual β€” mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from