-1

How can I include another Perl script in my base Perl script?

I have a primary source file test.pl and I want to include secondary sóurce file config.pl within it.

What is a standard method to achieve this in Perl?

Borodin
  • 126,100
  • 9
  • 70
  • 144
vkk05
  • 3,137
  • 11
  • 25
  • 2
    What do you mean by "include"? Do you want to execute the config.pl in the context of test.pl, i.e. should the variables set in config retain their values in test? – choroba Apr 26 '18 at 07:09
  • @choroba Yes. But don't want to use system/exec commands. – vkk05 Apr 26 '18 at 07:16
  • http://perldoc.perl.org/functions/do.html – xxfelixxx Apr 26 '18 at 07:18
  • 1
    Using `do` will work, but there are better ways. Move your config code into a library `Configuration.pm` and then `use Configuration;` in your script is a much saner way of handling code reuse. – xxfelixxx Apr 26 '18 at 07:19
  • @vinodk89: You have to be mentioned exceptions in your question clearly __don't want to use system/exec commands__ – ssr1012 Apr 26 '18 at 07:21
  • @xxfelixxx: this works!! – vkk05 Apr 26 '18 at 07:30
  • 1
    @xxfelixxx: Did you intend the OP to write a proper module, with `package Configuration` and perhaps `Exporter` functionality? It would probably be better to write this up as an an answer to make these things clear, including the need for a trailing `1;` to prevent the module from mysteriously failing to compile one day. – Borodin Apr 26 '18 at 08:06
  • 1
    [Why is it a bad idea to write configuration data in code?](https://stackoverflow.com/q/5969417) – daxim Apr 26 '18 at 08:34
  • 1
    @xxfelixxx I wrote those details up [here](https://stackoverflow.com/a/44649041/1331451). This should give OP a good starting point. – simbabque Apr 26 '18 at 09:09
  • Thanks for the links and suggestions. I was thinking along the lines of the answer @David Cross gave below, but didn't have time to write it up. – xxfelixxx Apr 26 '18 at 12:09

1 Answers1

5

(I'm guessing that the program called config.pl sets config values that you want to access in test.pl. You don't make that clear in your question.)

A simple example. If config.pl looks like this:

#!/usr/bin/perl

$some_var = 'Some value';

Then you can write test.pl to look like this:

#!/usr/bin/perl

use feature 'say';

do './config.pl';

say $some_var;

But this is a terrible idea for many reasons. Not least because it stops working when you add use strict and use warnings to either of the files (and you should aim to have use strict and use warnings in all of your Perl code).

So what's a better approach? Turn your configuration into a proper module that returns a hash (I only have a single scalar variable in my example above, but a hash gives you a way to deliver many values in a single variable). A simple approach might look like this.

A module called MyConfig.pm:

package MyConfig;

use strict;
use warnings;

use parent 'Exporter';

our @EXPORT = qw[config];

sub config {
  my %config = (
    some_var => 'Some value',
  );

  return %config;
}

1;

And a test.pl like this:

#!/usr/bin/perl

use strict;
use warnings;

use feature 'say';
use FindBin '$Bin';
use lib $Bin;

use MyConfig;

my %config = config();

say $config{some_var};

Having got that working, you can add improvements like parsing the %config hash from an external file (perhaps stored in JSON) and then allowing different configurations for different environments (development vs production, for example).

It's a little bit more work than your current approach, but it's far more flexible. And you can use strict and warnings.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97