6

I have some perl files which have been "bleached" (don't know if it was from ACME::Bleach, or something similar). Not being very fluent in perl, I'd like to understand what the one-liner that starts the file does to decode the whitespace that follows:

$_=<<'';y;\r\n;;d;$_=pack'b*',$_;$_=eval;$@&&die$@;$_

The rest of the file is whitespace characters, and the file is executable by itself (it's placed in a /bin directory).

[Solution], thanks to @JB.

The pack portion of this seems the most complex, and it took me a while to notice what was going on. Pack is taking the LSB only of every 8 characters, and unpacking that as a big-endian character in binary. Tabs hence become '0's, and spaces become '1's.

    '\t\t   \t  ' => '#'
in binary:
    00001001 00001001 00100000 00100000 00100000 00001001 00100000 0100000
every LSB:
    1 1 0 0 0 1 0 0
convert from from big-endian format:
    0b00100011 == 35 == ord('#')
JimB
  • 104,193
  • 13
  • 262
  • 255

2 Answers2

11
  • $_ = << ''; reads the rest of the file into the accumulator.
  • y;\r\n;;d; strips carriage returns and line feeds.
  • $_ = pack 'b*', $_; converts characters to bits in $_, LSB first.
  • $_ = eval; executes $_ as Perl code.
  • $@ && die $@; $_ handles exceptions and the return code gracefully.
JB.
  • 40,344
  • 12
  • 79
  • 106
  • OK, I think it's the `pack` function that's tripping me up. Can you clarify how something like `'\t\t \t '` translates to `'#'`? – JimB Sep 26 '11 at 16:05
  • Sorry, looks like the raw characters still has spaces collapsed, that string should be `tab tab sp sp sp tab sp sp sp`. – JimB Sep 26 '11 at 16:33
  • `tab` is `chr(9)`, odd; `sp` is `chr(32)`, even. So your string translates to bit sequence 1, 1, 0, 0, 0, 1, 0, 0, 0, which (LSB first) is 0x23, the ASCII code for `'#'`. (you have an extra bit) – JB. Sep 26 '11 at 17:40
3

You can use unbleach.pl to remove bleaching, if that's what you're really trying to do.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. It also guards against link rot. –  Sep 27 '11 at 19:29
  • @0A0D, Fine, fixed. I thought it went without saying in this case. – ikegami Sep 27 '11 at 19:36
  • You're being voted down bc I never asked how to unbleach the file (plus unbleach.pl won't work directly on these files without some tweaks). I stated that I wanted to learn how the perl line that starts each file works. – JimB Sep 27 '11 at 21:04
  • @JimB, I already explained I was guessing at the reason for which you were asking. Very few people actually ask the question to which they actually want an answer. See [XY Problem](http://www.perlmonks.org/?node_id=542341). Keep in mind that StackOverflow expects answers to be useful to future readers too. By downvoting (as oppose to not accepting the answer), you are saying that `unbleach.pl` isn't useful to anyone asking the question you asked, and I'm afraid my extensive experience at answering Perl questions shows that you're wrong. – ikegami Sep 27 '11 at 22:49
  • @JimB, Even though it turned out not to be what you wanted, I shall not delete my answer because it could be still be useful to future readers. I'm disappointed you think that you think this behaviour is worth discouraging. – ikegami Sep 27 '11 at 22:52
  • @ikegami - That's fine, I was only postulating as to why you were downvoted. I'm sure unbleach.pl will be helpful to someone. – JimB Sep 28 '11 at 01:04