"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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.