tunafish has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I've been coding Perl for 12 years. I wouldn't call myself a Perl coder, but I've written several large applications in the language. Last week, I heard, for the first time, that it's good practice to "use strict" and decided to try this out by bringing one of said applications into compliance. (Yeah, I'm Johnny-come-lately.)
Turned out to be not too bad, since my code was mostly fine in that regard (all variables within subroutines were declared with "my"), except for one small, but substantial problems. For ease of coding, I've separated subroutines into several files that I load into the main script using require. The main script is merely a collection of calls to the various subroutines. Now, I have some global variables that I define in the main script, and that are used throughout the sub-scripts. Now that I'm using the strict pragma, these variables are scoped to just the main script, and are empty in the subroutines.
Now the easiest solution would be to combine all of the files so as to eliminate the problem, but I feel like that can't (or at least shouldn't) be a "best practice". I guess I can define the variables within each of the files, but ditto. I've read a bunch of stuff regarding exporting variables, but I'm calling the other scripts using "require" not "use", and don't really understand why and if I should be using "use". I've also come across importing variables from a file that you're calling with require, but I have the opposite problem.
Am I missing something really obvious? I don't get it! Sorry about how muddled this question is, I'm just rather confused.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using variables in require file... not possible?
by graff (Chancellor) on Oct 16, 2011 at 04:23 UTC | |
|
Re: Using variables in require file... not possible?
by eyepopslikeamosquito (Archbishop) on Oct 16, 2011 at 06:53 UTC | |
|
Re: Using variables in require file... not possible?
by Rubber Cthulhu (Acolyte) on Oct 16, 2011 at 08:56 UTC | |
by anneli (Pilgrim) on Oct 16, 2011 at 10:03 UTC | |
|
Re: Using variables in require file... not possible?
by Marshall (Canon) on Oct 15, 2011 at 18:08 UTC | |
|
Re: Using variables in require file... not possible?
by choroba (Cardinal) on Oct 15, 2011 at 19:43 UTC | |
|
Re: Using variables in require file... not possible?
by NetWallah (Canon) on Oct 15, 2011 at 18:28 UTC |