in reply to Re: Global Variables
in thread Global Variables

Wonderful, that did it. So, you need to define variables before getting into a block, because if you declare them in a block, they are local to the block?

Replies are listed 'Best First'.
Re^3: Global Variables
by shmem (Chancellor) on Nov 27, 2007 at 22:30 UTC
    Yes. Each block is a scope. Even bare curlies (without any flow control statements) make a block.
    use strict; { my $foo = 2; { my $foo = 1; # masks outer $foo my $bar = $foo; print "foo is '$foo'\n"; # foo is '1' } print "foo is '$foo'\n"; # foo is '2' print $bar; # # error, $bar not visible here my $foo; # warning: $foo already declared # as lexical in this scope. } print "foo = $foo\n"; # error, $foo not visible here

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re^3: Global Variables
by moritz (Cardinal) on Nov 27, 2007 at 22:22 UTC
    Exactly.
Re^3: Global Variables
by jrsimmon (Hermit) on Nov 27, 2007 at 22:41 UTC
    Well...close, but not exactly. If you are using "use strict", which you evidently are, this is 100% correct. But you should know that without the strict option, a variable can be declared anywhere simply by using it. When not using the strict constraints, the best way I know to describe scoping is "global once declared".
      But you should know that without the strict option, a variable can be declared anywhere simply by using it.

      IMHO, since kmullin is a self-confessed mewbie, you should add a strong health warning to that statement, perhaps something along these lines. Don't be tempted to turn off strictures to silence intractable compilation errors; always use strict;.

      Cheers,

      JohnGG

        Render your statement in 20 point bold, and I'd have to agree.

        Ditto the -w switch ( or use warnings)