use List::Util qw( reduce ); sub bin2dec { my ( $str ) = @_; reduce { mul2( $a, $b ) } "", split //, $str; } #### sub mul2 { my ( $str, $have_overflow ) = @_; my $ret = join '', reverse map { my $c = $_ * 2 + ( $have_overflow ? 1 : 0 ); $have_overflow = ( $c >= 10 ); ( $c % 10 ); } reverse split //, $str; ( $have_overflow ? 1 : '' ) . $ret; } #### sub mul2 { my ( $str, $have_overflow ) = @_; join '', reverse map { my $c = $have_overflow ? 1 : 0; $c += $_ * 2 if length; $have_overflow = ( $c >= 10 ); # return a zero char only if not at top of string ( $c % 10 ) or ( length() ? 0 : '' ); } reverse '', split //, $str; }