in reply to Re^2: Help displaying binary data bit-by-bit in binary notation
in thread Help displaying binary data bit-by-bit in binary notation

Since unpack returns a string, simply add a bit (pun intended) of split and you're done:

my $bin_str = unpack( "b*", $string ); print join( "\n", split( //, $bin_str ) );

Updated output:

unpack b = [10000110] 1 0 0 0 0 1 1 0

Replies are listed 'Best First'.
Re^4: Help displaying binary data bit-by-bit in binary notation
by ikegami (Patriarch) on Jun 15, 2006 at 03:12 UTC

    The following also works:

    my $bin_str = unpack( "b*", $string ); print join( "\n", $bin_str =~ /(.)/g );

    It avoids split plus a (magical) regexp when just a regexp will do.

    It avoids split // which confuses Text::Balanced.

      How does Text::Balanced enter this scenario? What's the problem?

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        The problem occurs if that code were to appear in something parsed using Text::Balanced, such as a Parse::RecDescent grammar. It's not relevant most of the time, but it has bitten me twice in the past.