Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is weird.

I have some variables that won't show up in a required file.

Example...

$name = "turd"; require "./turd.pl";
turd.pl is an html document with surrounding print<<ENDHTML; ENDHTML things...

whenever I run the program, the variable $name is never shown, and even though I see the html without the variables, I do see this error (which is repeated 3 times...)

Use of uninitialized value in concatenation (.) at /turd.pl line 1.

line one contains "print<<ENDHTML;"

The Weird thing in all of this is that if i replace the require statement with exactly what is in the "required" file, everything works fine.

Even though they are not required in this particular subroutine, I have multiple required files in this script, and all of them are working fine. I am at a loss.

Any ideas?

Edit: chipmunk 2001-07-13

Replies are listed 'Best First'.
Re: Weird things...
by Wookie (Beadle) on Jul 13, 2001 at 20:39 UTC
    If you want to include settings from an external file - the easiest way I have found to do this is to have the external file like:
    #!/usr/bin/perl $name = 'turd'; $blah = 'sample';
    Then in your main program:
    #!/usr/bin/perl -w use strict; use vars qw($name $blah) require './turd.pl';
    That should allow you to read in the variables :)
Re: Weird things...
by Anonymous Monk on Jul 13, 2001 at 19:52 UTC
    correction..
    $name="turd"; require "./turd.pl";
    line one contains "print< < ENDHTML"
Re: Weird things...
by bikeNomad (Priest) on Jul 13, 2001 at 19:52 UTC
    What kind of variable is $name? If it's a lexical (my) variable, it won't be visible in the included file. You could make it a local, or you could eval the contents of the file:

    open FH, 'included.pl'; my $text; { local $/ = undef; $text = <FH>; } close FH; my $name = 'turd'; eval $text; die "error in included file: $@" if $@;
Re: Weird things...
by HyperZonk (Friar) on Jul 13, 2001 at 19:51 UTC
    Is name initialized in the main routine? If so, you need to qualify its use in loaded modules with main::. Example: in turd.pl, use $main::name in place of $main.