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.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: does perl have a concept of "int main()"? (variable scoping question)
  • Download Code