in reply to strange shift @_ problem

For a bit more information have a look at this output from the compiler backend B::Terse.

This is the statement print ((split(//,shift(@_)))[$_[1]..$_[2]]);

LISTOP (0x817e1d0) print OP (0x817e1f8) pushmark BINOP (0x817e1a8) lslice UNOP (0x817e160) null [141] OP (0x817e188) pushmark UNOP (0x817e0f8) null UNOP (0x817e0d8) flop UNOP (0x817e0b8) flip [6] LOGOP (0x817e090) range [5] UNOP (0x817dfe0) null [127] UNOP (0x817dfa0) null [125] SVOP (0x817df80) aelemfast GV (0x +80f6760) *_ OP (0x817dfc0) null [5] UNOP (0x817e068) null [127] UNOP (0x817e028) null [125] SVOP (0x817e008) aelemfast GV (0x +80f6760) *_ OP (0x817e048) null [5] UNOP (0x817e118) null [141] OP (0x817e140) pushmark LISTOP (0x817df00) split [4] PMOP (0x817ddf0) pushre // UNOP (0x817dee0) shift UNOP (0x817dec0) rv2av [3] SVOP (0x817de30) gv GV (0x80f6760) *_ SVOP (0x817df28) const IV (0x80fa958) 0
Now I don't know about you but I can sort of follow much of that, but the important part is seeing that the lslice line comes before the shift one. Which I assume means that it is dealt with first.

Replies are listed 'Best First'.
(Using B::* to help follow execution order) Re: strange shift @_ problem
by dchetlin (Friar) on Jan 19, 2001 at 10:37 UTC
    "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