From 9e6ae8b2da14b1505488bd77fc9847bb1cbd524b Mon Sep 17 00:00:00 2001 From: vdudouyt Date: Mon, 13 May 2024 19:09:22 +0300 Subject: [PATCH] Strings/numbers distinction fix (following JSON::PP) --- lib/Bencode.pm | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/Bencode.pm b/lib/Bencode.pm index 2614848..f7067cf 100644 --- a/lib/Bencode.pm +++ b/lib/Bencode.pm @@ -1,6 +1,5 @@ use 5.006; use strict; -use warnings; package Bencode; our $VERSION = '1.502'; @@ -121,7 +120,7 @@ sub _bencode; sub _bencode { map +( ( not defined ) ? ( $undef_encoding or croak 'unhandled data type' ) - : ( not ref ) ? ( m/\A (?: 0 | -? [1-9] \d* ) \z/x ? 'i' . $_ . 'e' : length . ':' . $_ ) + : ( not ref ) ? ( ("" & $_) ne "" ? 'i' . $_ . 'e' : length . ':' . $_ ) : ( 'SCALAR' eq ref ) ? ( length $$_ ) . ':' . $$_ # escape hatch -- use this to avoid num/str heuristics : ( 'ARRAY' eq ref ) ? 'l' . ( join '', _bencode @$_ ) . 'e' : ( 'HASH' eq ref ) ? 'd' . do { my @k = sort keys %$_; join '', map +( length $k[0] ) . ':' . ( shift @k ) . $_, _bencode @$_{ @k } } . 'e' @@ -297,10 +296,4 @@ The format does not support this. =back -=head1 BUGS AND LIMITATIONS - -Strings and numbers are practically indistinguishable in Perl, so C -has to resort to a heuristic to decide how to serialise a scalar. This cannot -be fixed. - =cut