*update* : bad link corrected

*update2* : reply to myself using B::Deparse => Re: strange usage of s/// -- I'm still confused :)

*update3* : problem solved, it's a Perl 5.14 and 5.16 bug.


Thanks, contributors, especially slms. As explained in slms's post(Re^2: strange usage of s///), Perl 5.18 behavior is correct :
Perl v5.18.1 handles them as one would expect:
perl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K$"//; print' $_ = 'abcdef'; s[@{[s/.././gr];}\K$"][]; print $_; -e syntax OK
perl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K //; print' $_ = 'abcdef'; s[@{[s/.././gr];}\K ][]; print $_; -e syntax OK

With Perl 5.14 and 5.16, there is a bug and some quantum tunnelling in s/// (yep! ;):

perl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K$"//; print' $_ = 'abcdef'; s[@{[s/.././rg];}\K][$"]; print $_; -e syntax OK
perl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K //; print' $_ = 'abcdef'; s[@{[s/.././rg];}][\\K ]; print $_; -e syntax OK

original post:

Dear Monks,

Could you, please, help me to understand one mysterious golf solution posted by "tails" at http://golf.shinh.org/p.rb?Bisection . (problem: read line, eg "abcdef", and print bisection, ie "abc def")

This is not the best answer (shortest) but it seems very strange... Here is the code (http://golf.shinh.org/reveal.rb?Bisection/tails_1377006056&pl => http://golf.shinh.org/reveal.rb?Bisection/tails+%28%3F%29_1377006539&pl):

#!perl -pl s/@{[s".."."gr]}\K$"//

*update* : why $" is different from literal space character in this regex? how this regex can add something to $_?

My other strange tests

$_="abcdef"; s/@{[s".."."gr]}/$& /;print # "...abcdef" $_="abcdef"; s/@{[s".."."gr]}//;print # "...abcdef" $_="abcdef"; s/@{[s".."."gr]}\K //;print # "\K def"

FYI, "tails" also wins the contest with:

#!perl -p s/.(?0).|\K/ $&/

Nice usage of (?0), tails! :)

English is not my mother tongue.
Les tongues de ma mère sont "made in France".

In reply to strange usage of s/// [solved: Perl 5.14 and 5.16 bug] by brx

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.