in reply to Re: strange scope
in thread strange scope

Hi anagramster,

If you use strict and warnings (or at least warnings), you should get a warning like:

Found = in conditional, should be == at scope_test.pl line 5.

For example, in the following code:

use strict; use warnings; while (my $y = 1) { $y = 0; print "hello (y = $y)\n"; }

You will get the warning about "= in conditional".

It's also interesting to note that this test program, which is essentially equivalent to:

use strict; use warnings; my $y = 0; while ($y = 1) { $y = 0; print "hello (y = $y)\n"; }

will never terminate, even though $y is set to zero each time, because it's set to 1 immediately before the condition of the while statement is evaluated.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^3: strange scope
by jasonk (Parson) on May 23, 2007 at 14:29 UTC

    I find it interesting that so many people jumped on the "you don't want to do an assignment in a conditional" bandwagon without even knowing what the right hand side of the assignment is. There are times when an assignment in a conditional is exactly what you want. Here is an example that I use almost daily with DBIx::Class...

    my $rs = $schema->resultset( 'Foo' ); while ( my $record = $rs->next ) { # do something with the record }

    The warning you mention only occurs if the right hand side of the assignment is a constant, and the OP didn't include what was in the RHS.


    We're not surrounded, we're in a target-rich environment!
Re^3: strange scope
by Beechbone (Friar) on May 23, 2007 at 15:31 UTC
    I find it a bit strange that you assume that someone who askes about the meaning of a warning does not "use warnings".

    I also find it strange that a node that completely ignores the question of the op gets such a high reputation.


    Search, Ask, Know something completly different
Re^3: strange scope
by blazar (Canon) on May 23, 2007 at 09:16 UTC
    will never terminate, even though $y is set to zero each time, because it's set to 1 immediately before the condition of the while statement is evaluated.

    Yep, one would want to use redo there, in that case.