David Granas wrote: > Hi, > > Im just learning Perl and was a little confused with why I couldnt > prevent my subroutines from reading variables from the main program. > Here is an example: > > use strict; > > my $var = 1; > > test(); > > sub test > { > print $var; > } > > I had thought that a my variable would not be able to be read by the > subroutine, but it can still see it. So my question is how can I > prevent subroutines from having access to variables in the main > program.
Perl's behavior here is not different from that of other languages like C. If you want, you can create a sub main (a la C) and put your main program and its variables there. You need a call to main() to start off the program: use strict; main(); sub main { my $var = 1; test(); } sub test { print $var; # doesn't see the $var from above } Since "my" variables are visible only from the point of declaration to the end of the enclosing block or file, you can acheive the same result by putting your "main" program and it's variable declarations at the bottom of the file: use strict; sub test { print $var; # doesn't see the $var from below } my $var = 1; # only visible from here to end of file test(); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]