Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Perl bytecode

by mulander (Monk)
on Dec 06, 2005 at 15:38 UTC ( [id://514499]=perlquestion: print w/replies, xml ) Need Help??

mulander has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Monks,
How can I tell the interpreter ( or something else ;P ) to give me the bytecode of a script.
Here is what I want ( this is from polish wikipedia )
print "Hello, world!\n";
should give:
OP enter COP nextstate OP pushmark SVOP const PV "Hello, world!\n" LISTOP print LISTOP leave
I must say, I want it for no particullar reason, I am just
courisous of various things and I really want to know all
what can be known about Perl ;)

I tried perl --help but I saw no flags allowing me to dump the bytecode to a file or at least print it to STDOUT.

Replies are listed 'Best First'.
Re: Perl bytecode
by Fletch (Bishop) on Dec 06, 2005 at 15:47 UTC

    Perhaps B::Concise produces something like you want? An example from its documentation:

    % perl -MO=Concise -e '$a = $b + 42' 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 7 <2> sassign vKS/2 ->8 5 <2> add[t1] sK/2 ->6 - <1> ex-rv2sv sK/1 ->4 3 <$> gvsv(*b) s ->4 4 <$> const(IV 42) s ->5 - <1> ex-rv2sv sKRM*/1 ->7 6 <$> gvsv(*a) s ->7
      Thank you both for anwsers.
      Fletch you hit the bull's eye, It is exactly what I wanted to obtain.
      Thank you again for the time and trouble.
      Next time I will try harder searching my own anwser, as It seems that it was just under my nose.
        With the -exec flag, B::Concise gives output closer to your example:
        $ perl -MO=Concise,-exec -we'print "Hello, world!\n";' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v 3 <0> pushmark s 4 <$> const[PV "Hello, world!\n"] s 5 <@> print vK 6 <@> leave[1 ref] vKP/REFC -e syntax OK
        by showing execution order rather than the op tree (but no longer shows optimized-away ops and can make branching ops less clear).
Re: Perl bytecode
by VSarkiss (Monsignor) on Dec 06, 2005 at 15:46 UTC
Re: Perl bytecode
by xdg (Monsignor) on Dec 06, 2005 at 15:51 UTC

    You'll want to use something in the B module hierarchy.

    Re: Advanced Perl Programming, 2nd edition walks through some detailed examples of how to examine the opcode tree (I think with B::Generate, but I don't have the book with me at the moment).

    Update:

    perl -MO=Debug foo.pl

    This may quickly give you what you want.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Perl bytecode
by blazar (Canon) on Dec 06, 2005 at 15:52 UTC
    I had seen nice examples adopting perl -MO=Terse script.pl (of which I could hardly understand anything ;-) ... now perldoc B::Terse tells me that it is just a wrapper around B::Concise, so I'd look into the latter...

      Sample Terse output:

      >perl -MO=Terse -e "print qq{Hello, world!\n};" LISTOP (0x1ab0b04) leave [1] OP (0x1ab8bac) enter COP (0x1ab0b3c) nextstate LISTOP (0x1ab0bb8) print OP (0x1ab0b90) pushmark SVOP (0x1ab0be0) const SPECIAL #0 Nullsv -e syntax OK

      The Concise equivalent:

      >perl -MO=Concise -e "print qq{Hello, world!\n};" 6 <@> leave[t1] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 5 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const(SPECIAL Null)[t2] s ->5 -e syntax OK

      The Debug equivalent:

      >perl -MO=Debug -e "print qq{Hello, world!\n};" LISTOP (0x1bb2ec0) op_next 0x0 op_sibling 0x0 op_ppaddr PL_ppaddr[OP_LEAVE] op_targ 1 op_type 178 op_seq 6612 op_flags 13 op_private 64 op_first 0x1ab8b84 op_last 0x1bb2f84 op_children 3 OP (0x1ab8b84) op_next 0x1bb2f08 op_sibling 0x1bb2f08 op_ppaddr PL_ppaddr[OP_ENTER] op_targ 0 op_type 177 op_seq 6607 op_flags 0 op_private 0 COP (0x1bb2f08) op_next 0x1bb2f5c op_sibling 0x1bb2f84 op_ppaddr PL_ppaddr[OP_NEXTSTATE] op_targ 0 op_type 174 op_seq 6608 op_flags 1 op_private 0 cop_label cop_stashpv main cop_file -e cop_seq 6608 cop_arybase 0 cop_line 1 cop_warnings 0x0 . . .
Re: Perl bytecode
by johnnywang (Priest) on Dec 06, 2005 at 19:30 UTC
    Can someone please explain the option usage:
    perl -MO=Bytecode -e '...'
    I've only used
    perl -Mmodule -e ' ....'
    "perl --help" doesn't mention this "-MO=". Incidentally, it seems most of the B modules are installed by default, what does "B" stand for?

      -MO loads the O module, of course, which loads the B::* module listed after the equal sign.

      B stands for Backend.

      As you know, -M is the command line equivalent of use. The equal sign specifies the arguments to pass to the import function. So perl -MO=Bytecode is the command line equivalent of use O 'Bytecode'; in actual Perl. Likewise perl -Mopen=:utf8,:std is the equivalent of use open qw(:utf8 :std);.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://514499]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found