Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

(Using B::* to help follow execution order) Re: strange shift @_ problem

by dchetlin (Friar)
on Jan 19, 2001 at 10:37 UTC ( #52947=note: print w/replies, xml ) Need Help??

in reply to Re: strange shift @_ problem
in thread strange shift @_ problem

"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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://52947]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-06-20 07:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.