in reply to Re: Re: [substr] anomaly or mine?
in thread [substr] anomaly or mine?

In the first case, this is a pure rvalue usage. In the second case, due to the assignment, you're using it as an lvalue, but then, due to the print, turing the resulting expression back into an rvalue. This double conversion seems to be the root of your problems.

I was expecting it to return '', since that's what the assignment operator usually does. As soon as you introduce the lvalue-function angle, I think you're playing with fire.

Replies are listed 'Best First'.
Re: Re^3: [substr] anomaly or mine?
by BrowserUk (Patriarch) on Aug 20, 2002 at 00:38 UTC

    Updated: to correct brainfart!

    Actually, I didn't see it that way and was about to suggest that nobody would think twice about using a slice as an rvalue in a list context...but decided to try it first...and waddayaknow...

    0.04 0.02 >@a = qw(a b c d e) 0.04 0.02 >print "@a", $/ a b c d e 0.04 0.02 >@a[1..3] = qw(x y z) 0.04 0.02 >print "@a", $/ a x y z e 0.04 0.02 >print @a[1..3] = qw(p q r) p q r 0.04 0.02 >print "@a", $/ a p q r e

    So, once I got the syntax right, a slice in a list context does work pretty much as you would expect. It does the assignment and then, returns the value assigned.


    What's this about a "crooked mitre"? I'm good at woodwork!

      Well, @a[1..3]= "x" is the same as @a[1..3]= ( "x", undef, undef ), and so this seems reasonable to me. [ Note that scalar context changes things since list assignment is documented to return the number of items in the right-hand-side list when evaluated in a scalar context so 1 == scalar( @a[1..3]= "x" ). ]

      If there were a way to assign to some kind of variable-length array slice, then we could make a more appropriate comparison between it and the substr "bug" case.

              - tye (but my friends call me "Tye")

        Agreed tye, my bad. If you take a look at my updated version, once I remembered the syntax for slices (not so very hard) it actually made the point I set out to make in the first place. That yo can use a slice as an rvalue in a list context and get the result that you would expect!

        I spent 2 hours verifying the original bug and chose to post rather than mention it in the CB cos I often get extreme lag as you saw earlier. I end up trying to answer the question before the last question asked etc.

        Then I had a brainfart ........


        What's this about a "crooked mitre"? I'm good at woodwork!