The stupid question is the question not asked | |
PerlMonks |
Re: the #include businessby esh (Pilgrim) |
on Aug 23, 2003 at 06:17 UTC ( [id://286015]=note: print w/replies, xml ) | Need Help?? |
Since you used the term "#include" I assume you're coming from a C or C++ background where the included file is inserted into the calling file by the pre-processer before the parser gets ahold of it. In those languages it's as if you had typed the second file inside the first so variables declared in the included file would be available in the parent. In this part of Perl, the language works a bit more like Java and Modula 2. The packages you "require" or "use" have their own scoping which is applied when you use keywords like "my". The "my" in test2.pl restricts the lexical scope of the $x variable to the enclosing block. In this case, the block is the file test2.pl so the variable $x can only be used inside that file and will not be able to be accessed elsewhere. It looks like you have reduced a real life problem to the bare essense to elminate any non-contributing factors to studying the problem. In general, this is a great thing as it saves others a lot of time wading through code unrelated to the problem at hand. Unfortunately, it also makes it difficult for me to know what to recommend as a workaround to your problem as I don't know exactly how the data is being used and why you want to share it. There are options of global variables, "our" variables, package variables with external access, returning values from a function, and probably other ideas folks could come up with. You're probably going to get some recommendations that you not share data at all, but sometimes it's a good thing. It's just difficult to provide an opinoin with the sterilized sample. I can give you one other hint: The shabang line in test2.pl (#!/usr/bin/perl -w) is unnecessary and, in fact, is ignored when that file is "require"d. It is probably just misleading to the code reader unless test2.pl is actually run as a top level program itself. There's no special first line needed for Perl packages which are used in a "require" or "use". -- Eric Hammond
In Section
Seekers of Perl Wisdom
|
|