use strict; use warnings; print bin_to_dec('1101100'); sub bin_to_dec { my $bits = shift; my( $power, $magnitude, $num ); die "$bits is not a pure bit string.\n" if $bits =~ m/[^10]/; if ( $bits =~ m/ (?{ $power = length($_) - 1; }) (?: ([10]) (?{ $magnitude = 2 ** $power; $^N eq '1' and $num += $magnitude; $power--; }) )+ /x ) { return $num; } else { die "Unable to resolve bits: $bits.\n"; } } #### use strict; use warnings; while ( ) { chomp; print bin_to_dec($_), "\n"; } sub bin_to_dec { my $bits = shift; my( $power, $magnitude, $num ); die "$bits is not a pure bit string.\n" if $bits =~ m/[^10]/; if ( $bits =~ m/ (?{ $power = length($_) - 1; $num = 0; }) (?: ([10]) (?{ $magnitude = 2 ** $power; $^N eq '1' and $num += $magnitude; $power--; }) )+ /x ) { return $num; } else { die "Unable to resolve bits: $bits.\n"; } } __DATA__ 00000000 00000011 00000111 11100000 __OUTPUT__ 0 Use of uninitialized value in print at test.pl line 9, line 2. Use of uninitialized value in print at test.pl line 9, line 3. Use of uninitialized value in print at test.pl line 9, line 4.