Re: [code-review] Bencode.pm
Andrew Savige <ajsavige-/[email protected]> Thu, 2 Oct 2003 13:30:16 +1000 (EST)
| Newsgroups | gmane.comp.lang.perl.code-review-ladder |
|---|---|
| Message-ID | <[email protected]> |
--- Caleb Epstein <[email protected]> wrote: > When encoding a HASH -> bencoded form, there is no way to tell if the > user intends a value which contains all-digits to be encoded as an > integer or a string. Not sure if this helps you, but you can tell if a Perl value is a string or a number by exploiting that bitwise operators operate differently on numbers and strings. This trick is due to merlyn; for example: my $x = "123"; if ((~$x & $x) ne '0') { print "x '$x' is a string\n"; } else { print "x '$x' is a number\n"; } my $y = 123; if ((~$y & $y) ne '0') { print "y '$y' is a string\n"; } else { print "y '$y' is a number\n"; } prints: x '123' is a string y '123' is a number (Devel::Peek can also tell the difference). Also, I noticed you are using capturing parens, for example: /^(0|[1-9]\d*):/ It is faster to use non-capturing ones, for example: /^(?:0|[1-9]\d*):/ /-\ http://search.yahoo.com.au - Yahoo! Search - Looking for more? Try the new Yahoo! Search