1

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?

4
  • Where is the file1.pl and file2.pl? Commented May 18, 2015 at 13:51
  • @serensat sorry see my edit Commented May 18, 2015 at 13:53
  • 3
    If you don't want the code in test1.pl to be run, move it to a subroutine. Commented May 18, 2015 at 13:53
  • 4
    This is a hack and you shouldn't be coding it like that. You should get both programs to use a config module that sets up values — preferably constants rather than global variables — and exports them using Exporter Commented May 18, 2015 at 13:54

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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__

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.