in reply to lvalue substring oddities

I've become persuaded that the current behavior should be kept. It is deducible from operator precedence and, once known, is not the cause of any great confusion.

What will be the effect of the proposed 'fix' on constructions like this?

{ my $foo = 'ABC-2N322-850103'; sub prefix () :lvalue { substr $foo, 0, index( $foo, '-') } }
Will the compiler keep the old value from the index? Does the associativity of assignment change? How is prefix() to know whether it is in lvalue position in a call? Any such change needs to try these things.

Update: Actually, the code above has a surprise in it, anyway. The result of the index call is kept instead of evaluated a second time:

{ my $foo = 'ABC-2N322-850103'; sub prefix () :lvalue { substr $foo, 0, index( $foo, '-') } sub foo () { $foo } } print foo, $/; my $bar = prefix = 'DEFG'; print $bar, $/ print foo, $/; __END__ output: ABC-2N322-850103 DEF DEFG-2N322-850103

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: lvalue substring oddities
by ysth (Canon) on Nov 12, 2003 at 05:02 UTC
    I've become persuaded that the current behavior should be kept. It is deducible from operator precedence and, once known, is not the cause of any great confusion.
    Somewhat agree. The existing behaviour is also IMO easier to document clearly than with the proposed change.

    But to throw fuel on the fire:

    $ perl -wl $x = "abc"; for $z (substr($x,1,1)) { print ":$z:"; $z = "zz"; print ":$z:" } __END__ :b: :z:
    If you alias something to the substr or pass it as a parameter, you can get some pretty odd results currently. OTOH, this is not a lot different from this case:
    "abc"=~/.(.)/; for $z ($1) { print ":$z:"; "zz" =~ /.(.)/; print ":$z:" }
Re: Re: lvalue substring oddities
by ysth (Canon) on Nov 12, 2003 at 16:58 UTC
    To reply to your sub :lvalue case, the index shouldn't be kept from one call to prefix() to the next with either the existing or proposed behaviour. However there is a bug (#24200) with substr used in an :lvalue sub. That bug should be fixed whatever else happens. (The bug is that rvalue calls to the sub will not work correctly after the first lvalue call.)

    Aside from that, your code is a perfect example of how things can go wrong the way things are. Just saying (prefix = "AB") = "ABC" gets rid of the first '-'; with the proposed change it would leave $foo back in its original state.

    Update to respond to Zaxo's update; the "surprise" is exactly the subject of the thread. When you say $foo = substr() = $bar substr returns a magic value that is first stored and then fetched. The "problem" is that both use exactly the same length, even if the store expanded or contracted the string. substr() being replaced by an :lvalue sub is exactly the same case...the sub is called only once but the magic value returned is acted on twice, both times with the originally determined length.