in reply to Re: Use variable outside if statement
in thread Use variable outside if statement
use strict; use warnings; my $var = 10; { my $var = 20; print $var, "\n"; } print $var, "\n"; __OUTPUT__ 20 10
...just a quick demonstration of using {...} blocks to limit scope and protect variables of broader scope.
Perl is great at providing mechanisms for lexical scoping: file-based scope, package scope, block scope, subroutine scope, as well as other mechanisms such as do{...}; and eval. It can be a tricky maze to wander at first, but definately flexible.
Perl programmers are lucky in that they can decide just how complex or simple they want their scoping and modularization to be.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Use variable outside if statement
by skyknight (Hermit) on Dec 18, 2003 at 16:33 UTC |