Rob Wells
Rob Wells

Reputation: 37103

What is the best way to share a config file between Perl scripts and bash scripts?

I have a system that is composed of both bash scripts and Perl scripts.

Is there a safe way to specify config values that are used by both Perl scripts and shell scripts?

We need both as there are common elements which are simple MY_VAR=toto declarations along with Perl specific config values, e.g. hashes.

BTW We don't have the Env module available in our hardened Perl build.

Upvotes: 2

Views: 418

Answers (2)

Boris Däppen
Boris Däppen

Reputation: 1196

Why don't you write a Perl script which reads and writes the config files. You could make this code so that it works as a Perl module and as a script from commandline at the same time. You can then call this code inside your Perl scripts and also inside your shell scripts to get and set those values.

You could use YAML as a format, there are several modules on CPAN supporting that. But you can choose whatever you like since you have full control and even a change later on would only affect your get&set-script.

Upvotes: 3

Zaid
Zaid

Reputation: 37146

You don't have to have Env to be able to access environment variables. The following excerpt from the Env documentation makes this plain:

Perl maintains environment variables in a special hash named %ENV. For when this access method is inconvenient, the Perl module Env allows environment variables to be treated as scalar or array variables.

%ENV makes constructing "config" hashes in Perl quite simple. In the following example, it'd probably be clearer to stick with %ENV itself:

my %config;
my @env_vars = qw/ foo bar baz quux /;
@config{@env_vars} = @ENV{@env_vars};

Upvotes: 5

Related Questions