Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: the #include business

by esh (Pilgrim)
on Aug 23, 2003 at 06:17 UTC ( [id://286015]=note: print w/replies, xml ) Need Help??


in reply to the #include business

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

Replies are listed 'Best First'.
Re: Re: the #include business
by Anonymous Monk on Aug 23, 2003 at 06:31 UTC
    Wow! Two great answers and quickly, even! I have just tried out the method suggested by chromatic, and it works perfectly. Indeed, my background much stronger in c, and other ancient languages than in perl. I so confidently expected to find a #include directive in perl that when I first needed it, I simply put it in, and was surprised that it didn't work!

    Oh, yes... My project involves a number of independent perl "daemons" that share a number of common constant values, such as database table names, hence the need for an include file.

    Thanks very much for the excellent explanation. I may even be moved to drop something in the collection plate, instead of taking some out as I usually do.

      That's the difference between Perl and C!

      In Perl you put the common variables and constants in the main program and have the "daemons" access them from there.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://286015]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found