user2688842
user2688842

Reputation: 3

Perl include file dynamically

I have a doubt in perl , I need to get filename in command line arguments/switches/options and based on that i need to include/require/use that file in my script. So by using that getopt variable am able to get it done. However if i have used "use strict" in my script i could not get it. Please help on this

I tried as follows and its working

base.pm:

our $def_name = "me";

main.pl:

my $name = "mathlib.pm";
require $name;
print $def_name;

When i include the follwoing lines in the main.pl and base.pm,

use strict;
use warnings;

I am getting error,

Global symbol "$def_name" requires explicit package name .
Execution of main.pl aborted due to compilation errors.

Upvotes: 0

Views: 234

Answers (1)

Matthew Walton
Matthew Walton

Reputation: 9959

$def_name hasn't been exported from mathlib.pm, it's a package variable not a global. $mathlib::def_name will work, or you can use the Exporter module to export it into the namespace of any module which uses it.

Upvotes: 1

Related Questions