and in my example I try to “include” a perl file to another but I get errors:
Global symbol “$var1” requires explicit package name at ./1.pl line 8.
Global symbol “$var2” requires explicit package name at ./1.pl line 9.
Execution of ./1.pl aborted due to compilation errors.
Code of the two files:
1.pl
#!/usr/bin/perl -w
use strict;
require '2.pl';
print $var1;
print $var2;
2.pl
#!/usr/bin/perl -w
my $var1 = "hello";
my $var2 = " perl!";
I’ve also tried these, but every time, I get the same errors: 2.pl
#!/usr/bin/perl -w
local $var1 = "hello";
local $var2 = " perl!";
Correct, the variables in the required file are not scoped for the main file. There is little sense in just posting the code for how to do what you want since you will not understand what the code is doing. Read Chapter 10, “Modules”, of this free online perl book:
and you will probably want to bookmark that site for future reference.
That is one way to do it but is not the typical way. Read the link I posted for you. But congratulations on finding a solution that is perfectly acceptabe for your small example. You wouldn’t want to use that convention for more complex programs though.
If I uncomment the local variable definition, of course it works, and if I take out “use strict” and don’t include the “my” for %data in the include file it also works. Using perl 5.8.8 on Linux