in reply to the #include business

You are running into scoping problems. Defining a lexical with my does not produce a variable which can be seen across file barriers.

If test2.pl contained,

#!/usr/bin/perl -w use strict; use vars '$x'; $x="this is it\n"; 1;
I think your program would act as you wish.

A more sophisticated approach would make your test2.pl a .pm and define a namespace with package.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: the #include business
by esh (Pilgrim) on Aug 23, 2003 at 07:09 UTC

    After studying chromatic's sample code, I see why what you propose is not quite sufficient. You need to either have a BEGIN{} block around the "require" or use a "use"--which is very close to a "require" inside a BEGIN.

    The reason for this is that the "require" is processed at run time and the $x variable used in test1.pl does not get defined until the test2.pl is compiled. Unfortunately, it never gets to test2.pl because the $x causes an error during the compile of test1.pl.

    Putting the "require" in a BEGIN solves this by forcing test2.pl to be compiled (and $x to be declared) before the rest of test1.pl (and the usage of $x).

    I have tended to use "use" instead of "require" since it became available in Perl, so didn't think of this problem until seeing chromatic's response.

    -- Eric Hammond