6

Is there a way to create perl source code, if I have the opcode?

For example

perl -MO=Concise -e "print 123"

will output the opcode:

6  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
5     <@> print vK ->6
3        <0> pushmark s ->4
4        <$> const[IV 123] s ->5

I would like to reverse this somehow.

Blub
  • 3,762
  • 1
  • 13
  • 24
  • Similar: [how to decompile perl bytecode from perlcc -B?](http://stackoverflow.com/q/15083825/176646) – ThisSuitIsBlackNot Dec 23 '14 at 23:21
  • B::Concise is (primarily?) a debugging tool, which leads me to suspect its output isn't intended for computer consumption. I'm posting this as a comment because I don't think it's authoritative enough to serve as an answer. – Slade Dec 24 '14 at 01:02

1 Answers1

3

If by "have the opcode", you mean "have the opcode tree"

While B::Concise tries to give an accurate representation of the opcode tree, B::Deparse takes the opcode tree and produces source code from it.

$ perl -MO=Deparse -e'$x && print'
print $_ if $x;
-e syntax OK

It's quite good, though there are some limitations.

If by "have the opcode", you mean "have the output of B::Concise"

Given that B::Concise tries to be very complete, it might be possible, but I think some information is missing. I'm pretty sure there's nothing that tries to do this on CPAN, since I've never heard of blead breaking it (something you would expect to happen regularly).

ikegami
  • 367,544
  • 15
  • 269
  • 518