My expectation was that the EXPR would be evaluated once and the invariant result used between every element of the LIST.

Then you should probably adjust (and loosen) your expectations about exactly how much optimization has or hasn't been done to a particular feature in a particular build of Perl.

If you manage to construct code that behaves differently depending on whether the implementation of some feature (especially one that uses the value more than once) evaluates the value once or twice, then you've written code that is likely to break when an optimization gets done.

If I had reason to pass a magical scalar that changes values to join, then I'd tell Perl to fix the value first via:

say join "$inc", @arr;
and the wording looks the be the same for for all versions

Yeah, people don't usually update the documentation of the basic functionality when an optimization is made.

In your mind, one of these two has to be broken?

sub join1 { my( $sep, @vals ) = @_; my $str = ''; while( @vals ) { $str .= shift @vals; $str .= $sep if @vals; } return $str; } sub join2 { my( $sep, @vals ) = @_; $sep = "$sep"; # Added my $str = ''; while( @vals ) { $str .= shift @vals; $str .= $sep if @vals; } return $str; }

Now if I go and optimize out the copying of most of @_ into @vals, that breaks something too? Or are both of those broken because they don't avoid copying from @_?

Next you'll tell me that join2 is broken because it evaluates $sep even if when @vals is empty and so $sep isn't actually used.

IMO, none of these are bugs. They are implementation details that reasonably should be expected to change at any time when some bug gets fixed, some code gets optimized, some code gets refactored, etc.

Making scalars that change every time you look at them is weird stuff. You have to put up with weird things happening and take extra care when you do stuff like that.

- tye        


In reply to Re: Undocumented join() feature, now defunct? (optimization) by tye
in thread Undocumented join() feature, now defunct? by johngg

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.