Re: [Boston.pm] converting a thing to a number
email-2Ro/Dj86MvDSUeElwK9/[email protected] Tue, 22 Feb 2022 23:33:58 -0500
| Newsgroups | gmane.comp.lang.perl.perl-mongers.boston |
|---|---|
| Message-ID | <[email protected]> |
Third option is to use oct() and hex() based on pattern
~/perl -le 'print hex("0xdeadbeef")'
3735928559
~/perl -le 'print oct("0177")' #also handles 0b
127
my $match = /^\s*0([xb])?[0-9a-f]+?/;
my $num;
if( $match && $1 eq 'x' ){ $num = hex() }
elsif( $match && ( $1 eq 'b' or $1 eq undef) ){ $num = oct() }
else{ $num = $_+0 }