Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In fact, this is what Perl optimizes the above statement to:

You have it backwards. «and» isn't optimized into an «if». There isn't even an «if» opcode. An «if» statement is compiled into code that includes an «and» or an «or» opcode.

$ perl -MO=Concise,-exec -e'f() and g()' 1 <0> enter v 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s 4 <#> gv[*f] s/EARLYCV 5 <1> entersub[t2] sKS/TARG 6 <|> and(other->7) vK/1 7 <0> pushmark s 8 <#> gv[*g] s/EARLYCV 9 <1> entersub[t4] vKS/TARG a <@> leave[1 ref] vKP/REFC -e syntax OK $ perl -MO=Concise,-exec -e'g() if f()' 1 <0> enter v 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s 4 <#> gv[*f] s/EARLYCV 5 <1> entersub[t4] sKS/TARG 6 <|> and(other->7) vK/1 7 <0> pushmark s 8 <#> gv[*g] s/EARLYCV 9 <1> entersub[t2] vKS/TARG a <@> leave[1 ref] vKP/REFC -e syntax OK $ perl -MO=Concise,-exec -e'if (f()) { g() }' 1 <0> enter v 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s 4 <#> gv[*f] s/EARLYCV 5 <1> entersub[t2] sKS/TARG 6 <|> and(other->7) vK/1 7 <0> pushmark s 8 <#> gv[*g] s/EARLYCV 9 <1> entersub[t4] vKS/TARG a <@> leave[1 ref] vKP/REFC -e syntax OK

«f() and g()» and «g() if f()» generate exactly the same opcode tree, so Deparse outputs what it thinks is clearest.[1]

If the whole conditional expression is negated, the «not» and the «and» opcodes will be optimized into an «or».

$ perl -MO=Concise,-exec -e'if (!f()) { g() }' 1 <0> enter v 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s 4 <#> gv[*f] s/EARLYCV 5 <1> entersub[t2] sKS/TARG 6 <|> or(other->7) vK/1 7 <0> pushmark s 8 <#> gv[*g] s/EARLYCV 9 <1> entersub[t4] vKS/TARG a <@> leave[1 ref] vKP/REFC -e syntax OK

  1. «-exec» shows the code that's executed. And while the code executed for «if (f()) { g() }» is identical to the code executed for the other two, the opcode tree is different. There are vestiges in the opcode tree of which Deparse can take advantage to differentiate the «if (f()) { g() }» case from the others. Remove «,-exec» to see the tree.

In reply to Re^2: Purpose of =~ and = in this statement by ikegami
in thread Purpose of =~ and = in this statement by sherab

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found