Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If we go and look at what these two variants get transformed into, we can see the reason for this.</p> <hr> <ul> <li><p>First lets look at the original version.</p> <pre><code>perl -MO=Deparse -e'while("abc" =~ /(.)/g){print "hi\n"}' </code></pre> <pre><code>while ('abc' =~ /(.)/g) { print "hi\n"; } </code></pre> <p>As you can see there wasn't any changing of the opcodes.</p></li> <li><p>Now if you go and change it to use the smart-match operator, you can see it does actually change.</p> <pre><code>perl -MO=Deparse -e'while("abc" ~~ /(.)/g){print "hi\n"}' </code></pre> <pre><code>while ('abc' ~~ qr/(.)/g) { print "hi\n"; } </code></pre> <p>It changes it to <a href="http://perldoc.perl.org/perlop.html#qr%2fSTRING%2fmsixpo" rel="nofollow noreferrer"><code>qr</code></a>, which doesn't recognize the <code>/g</code> option.</p> <p>This should probably give you an error, but it doesn't get transformed until after it gets parsed.</p> <hr> <p>The warning you should have gotten, and would get if you used <a href="http://perldoc.perl.org/perlop.html#qr%2fSTRING%2fmsixpo" rel="nofollow noreferrer"><code>qr</code></a> instead is:</p> <pre>syntax error at -e line 1, near "qr/(.)/g"</pre></li> </ul> <p><br></p> <p>The smart-match feature was never intended to replace the <code>=~</code> operator. It came out of the process of making <a href="http://perldoc.perl.org/perlsyn.html#Switch-statements" rel="nofollow noreferrer"><code>given</code>/<code>when</code></a> work like it does.</p> <blockquote> <p>Most of the time, <code>when(EXPR)</code> is treated as an implicit <a href="http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail" rel="nofollow noreferrer">smart match</a> of <code>$_</code>.<br> ...</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload