Module for common perl/shell/python constants

Discussion in 'Perl Misc' started by Hallvard Breien Furuseth, Nov 16, 2012.

  1. 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.

    --
    Hallvard
    Hallvard Breien Furuseth, Nov 16, 2012
    #1
    1. Advertising

  2. Hallvard Breien Furuseth

    Owen Guest

    On Friday, November 16, 2012 10:05:48 PM UTC+11, Hallvard Breien Furuseth 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.



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

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




    Owen
    Owen, Nov 17, 2012
    #2
    1. Advertising

  3. Hallvard Breien Furuseth <> 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.
    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
    Jürgen Exner, Nov 17, 2012
    #3
  4. Hallvard Breien Furuseth <> writes:
    > 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;
    --------------

    --------------
    use Shellvars qw(A PATH);

    print(A, ' ', PATH, "\n");
    --------------

    This could be done in a much more sophisticated way, as can be seen in
    constant.pm.
    Rainer Weikusat, Nov 17, 2012
    #4
  5. Rainer Weikusat <> writes:
    > Hallvard Breien Furuseth <> writes:
    >> 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;
    Rainer Weikusat, Nov 17, 2012
    #5
  6. Hallvard Breien Furuseth

    Willem Guest

    J?rgen Exner wrote:
    ) Hallvard Breien Furuseth <> 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
    Willem, Nov 18, 2012
    #6
  7. Hallvard Breien Furuseth

    C.DeRykus Guest

    On Saturday, November 17, 2012 7:09:22 AM UTC-8, Rainer Weikusat wrote:
    > Rainer Weikusat <> writes:
    >
    > > Hallvard Breien Furuseth <> writes:

    >
    > >> 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 });
    }

    --
    Charles DeRykus
    C.DeRykus, Nov 18, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denny
    Replies:
    1
    Views:
    759
  2. Samuel A. Falvo II
    Replies:
    0
    Views:
    472
    Samuel A. Falvo II
    Oct 8, 2008
  3. Sean
    Replies:
    3
    Views:
    87
    rthangam
    Apr 18, 2008
  4. John Reye
    Replies:
    28
    Views:
    1,331
    Tim Rentsch
    May 8, 2012
  5. Jason Mellone
    Replies:
    3
    Views:
    77
    Jurko Gospodnetić
    May 7, 2014
Loading...

Share This Page