First of all, it's a warning instead of an error (unless of course you trapped warnings and turned it into fatal error). There are some classification of warnings, but they are usually a strong indication that something bad is lurking in the code that would give you unexpected result.
are the two blocks part of the same "virtual" code block?
They are in the same scope as Zaxo explains. You can also ask Perl for more help by using diagnostics:
#!/usr/bin/perl use strict; use warnings; use diagnostics; if (my $x = 1) { # } elsif (my $x = 2) { # }
would say:
"my" variable $x masks earlier declaration in same scope at test.pl li +ne 9 (#1) (W misc) A "my" or "our" variable has been redeclared in th +e current scope or statement, effectively eliminating all access to t +he previous instance. This is almost always a typographical error. +Note that the earlier variable will still exist until the end of the +scope or until all closure referents to it are destroyed. Found = in conditional, should be == at test.pl line 9 (#2) (W syntax) You said if ($foo = 123) when you meant if ($foo == 123) (or something like that).

But there's something else. If that's what you really have in your code, you get another warning as liverpole said about Found = in conditional as you can see from the output above. Why? Simple assignment like $x = 'some_value' would always evaluate to true or false (depending the value of 'some_value'). However, if you assign from an expression such as calling a function, then it would be taken as you assign a variable and want to evaluate that variable at once. Consider this:

#!/usr/bin/perl use strict; use warnings; use diagnostics; # set $x from get_num(), if it returns true value # then do something with $x if (my $x = get_num()) { print $x, "\n"; } else { print "empty\n"; } sub get_num { 3 }
would print 3 without any warnings. I use this construct very often to firstly save a value from a function calling to proceed it further only if it's true. And I keep the variable from visible out of the scope since the variable would be irrelevant for the rest of the code.

If you instead, want to test beyond true or false of a variable, you could say for example:

if ( (my $x = get_num()) >= 3 ) { ... }
but I think it's too much noise. I would instead write something similiar to Herkum's code:
my $x = get_num(); # assumed to always returns a number if ($x < 0 ) { # negative numbers } elsif ($x < 3) { # less } else { # OK }
So, basically, be clear about what you want to do and what result you expect.

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re: strange scope by naikonta
in thread strange scope by anagramster

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.