On Wed, Aug 14, 2013 at 6:09 PM, Alexey Mishustin <shum...@shumkar.ru>wrote:
> 2013/8/15 Uri Guttman <u...@stemsystems.com>: > > On 08/14/2013 04:22 PM, Jim Gibson wrote: > >> > >> > >> On Aug 14, 2013, at 12:30 PM, Alexey Mishustin wrote: > >> > >>> Testing a value and assigning it - I have never done this at the same > >>> time... > >> > >> > >> > >> Doing both in while statements is very common: > >> > >> while( my $line = <$fh> ) { > >> ... > >> } > >> > >> Try to write that loop with two separate statements, one an > >> assignment > > > > and the other an if statement, and you may see the advantage of the > > currently-allowed syntax. > >> > >> > >> The general policy is that assignment statements return a value that > > > > may be further used or tested. > >> > >> > > > > i have a common idiom when dealing with a value in a hash: > > > > if( my $foo = $ref->{foo} ) { > > > > do stuff with $foo > > } > > > > OK, you have got me convinced. It's useful. > > I'm sorry only that there is no built-in option with which one could > enable/disable easily assignments inside `if'. (E.g., like re 'eval'/ > no re 'eval'). It would "provide choices"... > > It might not be too hard to implement that, actually. What follows is NOT a topic for the beginners list, but feel free to nag perl5-porters about it if you want to learn about the internals :D You "just" need to detect if there's a LOGOP where op_type is OP_COND_EXPR and the condition is one of the assign ops. There's also room to improve this in the core itself -- I don't see why this warns for if ( $foo = 2 ) { ... } but not for if ( $foo = constant ) { ... } Might be worth filing a bug there!