Module for common perl/shell/python constants

  • Thread starter Hallvard Breien Furuseth
  • Start date
H

Hallvard Breien Furuseth

Is there module which lets me write a single config file with
integer/string constants which will be used by both Perl, Bash and
Python? Preferably Perl would turn them into compile-time constants.

E.g. I'd write "use Shellvars qw(foo.sh); where foo.sh
would contain
FOO = 3
BAR = "baz"
Hopefully
BAD = "oops
would fail rather than produce some arbitrary strange result.
 
O

Owen

Is there module which lets me write a single config file with

integer/string constants which will be used by both Perl, Bash and

Python? Preferably Perl would turn them into compile-time constants.



E.g. I'd write "use Shellvars qw(foo.sh); where foo.sh

would contain

FOO = 3

BAR = "baz"

Hopefully

BAD = "oops

would fail rather than produce some arbitrary strange result.


Not sure exactly what you are after, but this may be a start, for perl anyway.

http://perldoc.perl.org/constant.html




Owen
 
J

Jürgen Exner

Hallvard Breien Furuseth said:
Is there module which lets me write a single config file with
integer/string constants which will be used by both Perl, Bash and
Python? Preferably Perl would turn them into compile-time constants.

E.g. I'd write "use Shellvars qw(foo.sh); where foo.sh
would contain
FOO = 3
BAR = "baz"
Hopefully
BAD = "oops
would fail rather than produce some arbitrary strange result.

If you need configurations that are compatible across multiple languages
then typically the greatest common divisor is XML.
If the constants have to be compile-time (you may want to double-check
the reasoning for this requirement), then write a little script which
semi-automatically converts this master XML into whatever format is best
for language y.

jue
 
R

Rainer Weikusat

Hallvard Breien Furuseth said:
Is there module which lets me write a single config file with
integer/string constants which will be used by both Perl, Bash and
Python? Preferably Perl would turn them into compile-time
constants.

The obvious solution for that would be a shell script which sets some
environment variables and invoking the Perl and Python code via wrapper
scripts configuring the environment first

A (somewhat crude) way to turn some environment variables into a Perl
constants could be:

--------------
package Shellvars;

sub import
{
my $caller;

$caller = caller();
shift;

for (@_) {
my $v = $ENV{$_};
*{$caller."::$_"} = sub () { $v; };
}
}

1;
 
R

Rainer Weikusat

Rainer Weikusat said:
Hallvard Breien Furuseth said:
Is there module which lets me write a single config file with
integer/string constants which will be used by both Perl, Bash and
Python? Preferably Perl would turn them into compile-time
constants.

[...]


A (somewhat crude) way to turn some environment variables into a Perl
constants could be:

--------------
package Shellvars;

sub import
[...]

This could be done in a much more sophisticated way, as can be seen in
constant.pm.

OTOH, one doesn't have to rewrite it if it could also be reused :)

-----------------
package Shellvars2;

use constant;

sub import
{
shift;

@_ = ('MarmiteMermaid', { map { $_, $ENV{$_} } @_});
goto &constant::import;
}

1;
 
W

Willem

J?rgen Exner wrote:
)>Is there module which lets me write a single config file with
)>integer/string constants which will be used by both Perl, Bash and
)>Python? Preferably Perl would turn them into compile-time constants.
)>
)>E.g. I'd write "use Shellvars qw(foo.sh); where foo.sh
)>would contain
)> FOO = 3
)> BAR = "baz"
)>Hopefully
)> BAD = "oops
)>would fail rather than produce some arbitrary strange result.
)
) If you need configurations that are compatible across multiple languages
) then typically the greatest common divisor is XML.

XML is most used, yes, but it's a terrible choice. Use YAML, or JSON, or
INI, or something else that is actually designed for this task.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
C

C.DeRykus

Rainer Weikusat said:
Is there module which lets me write a single config file with
integer/string constants which will be used by both Perl, Bash and
Python? Preferably Perl would turn them into compile-time
constants.


[...]





A (somewhat crude) way to turn some environment variables into a Perl
constants could be:
--------------

package Shellvars;
sub import


[...]



This could be done in a much more sophisticated way, as can be seen in
constant.pm.



OTOH, one doesn't have to rewrite it if it could also be reused :)



-----------------

package Shellvars2;



use constant;



sub import

{

shift;



@_ = ('MarmiteMermaid', { map { $_, $ENV{$_} } @_});

goto &constant::import;

}



1;


Not a proper module but in simple cases,
you could just pass in with @ARGV and constantify in a BEGIN:

BEGIN {
require constant;
constant->import({map{$_,$ENV{$_}} @ARGV });
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top