in reply to does perl have a concept of "int main()"? (variable scoping question)
In perl, it seems that all variables are visible to all functions by default, i.e. they have global scope.
If you haven't discovered my, it's time to read up on it.
However, perhaps you've already discovered that and what you are getting at is that if you lay out your code like this (as many do):
my $mainVar1 = ...; my $mainVar2 = ...; while( something ) { if( isSomething() ) { my $result = doSomething(); doSomethingElse() if $result eq somthing; } } exit; sub isSomething { ...; } sub doSomething { ...; } sub doSomethingElse { ...; }
Then all the variables -- including my vars -- declared for use by your main code, are visible to your subroutines. (And that can tempt people into 'cheating' by accessing them directly rather than passing them via arguments.
The (a, and my prferred) solution is to declare your subs at the top of the file and put your 'main' code at the bottom.
|
|---|