user4284509
user4284509

Reputation:

How to use only the variable name from another file?

I want to use only the variable name from the another file.

test1.pl

use warnings;
use strict;
our $name = "hello world";
print "Helllloo\n";

test2.pl

use warnings;
use strict;
require "test.pl";
our $name;
print "$name\n";

test1.pl contain some content with many functions. I used the variable $name from the test1.pl. But Don't run test1.pl while running the test2.pl. For example when run the test2.pl it result was

Helllloo   
hello world

That the Helllloo print from the test1.pl. How can use the another file variable name only How can i do it?

Upvotes: 1

Views: 73

Answers (2)

Sinan Ünür
Sinan Ünür

Reputation: 118118

Use Const::Fast to export variables from a module:

use strict;
use warnings;

use My::Config '$NAME';

print "$NAME\n";

In My/Config.pm:

use strict;
use warnings;

package My::Config;

use Exporter 'import';
our @EXPORT = ();
our @EXPORT_OK = qw{ $NAME };

use Const::Fast;

const our $NAME => "hello world";
__PACKAGE__;
__END__

Upvotes: 0

Borodin
Borodin

Reputation: 126722

You should rewrite both test1.pl and test2.pl to use MyConfig, like this

test2.pl

use strict;
use warnings;

use MyConfig 'NAME';

print NAME, "\n";

MyConfig.pm

use strict;
use warnings;

package MyConfig;

use Exporter 'import';
our @EXPORT_OK = qw/ NAME /;

use constant NAME => "hello world";

1;

output

hello world

Upvotes: 3

Related Questions