Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: strange scope

by nedals (Deacon)
on May 23, 2007 at 00:46 UTC ( [id://616911]=note: print w/replies, xml ) Need Help??


in reply to strange scope

Consider...
if (my $x = ...) { ... } elsif (my $x = ...) { ... }

Looking at it this way, you will notice that both $x's are actually in the same block. Hence the error.
There are no 'curlys' enclosing the seperate conditions.

So use something like Herkum's solution.

Replies are listed 'Best First'.
Re^2: strange scope
by liverpole (Monsignor) on May 23, 2007 at 01:02 UTC
    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$..$/

      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!
      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
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://616911]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-23 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found