"Careful there, good friar."

The lslice opcode comes before the shift in this output of B::Terse -- but keep in mind that B::Terse by default shows an optree, which is empatically not in execution order.

lslice is a BINOP, which means it has two children. The first child is the list of elements it will be grabbing (in this case, that comes from the range operator in list context, aka flip/flop in our opcodes above). The second child is the list from which we'll actually be slicing. The fact that the children are ordered that way is the reason the $_[1] etc. are evaluated first.

For an easier way to figure out what order things happen in, try the -exec option to Terse, like so:

perl -MO=Terse,exec -e'print +(split//,shift)[$_[1]..$_[2]]'

I won't show you the output, because I have one more tip. Just a few weeks ago, Stephen McCamant released a new backend module that blows Terse out of the water. It's called B::Concise, and while it's only been added to the standard distro for very recent bleadperls, you can download it from the APC. Once you've got it, stick it somewhere in your INC path under a `B' directory, and use it like so:

[~] $ perl -MO=Concise -e'print +(split//,shift)[$_[1]..$_[2]]' h <@> leave vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 g <@> print vK ->h 3 <0> pushmark s ->4 f <2> lslice lK/2 ->g - <1> ex-list lK ->8 4 <0> pushmark s ->5 - <1> null lK/1 ->- 7 <1> flop lK ->8 j <1> flip[t4] lK ->8 5 <|> range(other->6)[t3] lK/1 ->i - <1> ex-aelem sK/2 ->j - <1> ex-rv2av sKR/1 ->- i <$> aelemfast(*_) s/1 ->j - <0> ex-const s ->- - <1> ex-aelem sK/2 ->7 - <1> ex-rv2av sKR/1 ->- 6 <$> aelemfast(*_) s/2 ->7 - <0> ex-const s ->- - <1> ex-list lK ->f 8 <0> pushmark s ->9 e <@> split[t2] lK ->f 9 </> pushre(//) s ->a c <1> shift sK/1 ->d b <1> rv2av[t1] sKRM/1 ->c a <$> gv(*ARGV) s ->b d <$> const(IV 0) s ->e

Or, to see the exec order:

[~] $ perl -MO=Concise,-exec -e'print +(split//,shift)[$_[1]..$_[2]]' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v 3 <0> pushmark s 4 <0> pushmark s 5 <|> range(other->6)[t3] lK/1 6 <$> aelemfast(*_) s/2 7 <1> flop lK goto 8 i <$> aelemfast(*_) s/1 j <1> flip[t4] lK 8 <0> pushmark s 9 </> pushre(//) s a <$> gv(*ARGV) s b <1> rv2av[t1] sKRM/1 c <1> shift sK/1 d <$> const(IV 0) s e <@> split[t2] lK f <2> lslice lK/2 g <@> print vK h <@> leave vKP/REFC

As you can see from the above, the aelemfast (which accesses elements of the array in the glob *_) opcodes execute before the shift.

-dlc


In reply to (Using B::* to help follow execution order) Re: strange shift @_ problem by dchetlin
in thread strange shift @_ problem by ChOas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.