Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Module for common perl/shell/python constants

Reply
Thread Tools

Module for common perl/shell/python constants

 
 
Hallvard Breien Furuseth
Guest
Posts: n/a
 
      11-16-2012
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
 
Reply With Quote
 
 
 
 
Owen
Guest
Posts: n/a
 
      11-17-2012



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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-17-2012
Hallvard Breien Furuseth <(E-Mail Removed)> 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
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-17-2012
Hallvard Breien Furuseth <(E-Mail Removed)> 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.

 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-17-2012
Rainer Weikusat <(E-Mail Removed)> writes:
> Hallvard Breien Furuseth <(E-Mail Removed)> 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;
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      11-18-2012
J?rgen Exner wrote:
) Hallvard Breien Furuseth <(E-Mail Removed)> 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
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      11-18-2012
On Saturday, November 17, 2012 7:09:22 AM UTC-8, Rainer Weikusat wrote:
> Rainer Weikusat <(E-Mail Removed)> writes:
>
> > Hallvard Breien Furuseth <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
java.lang.NoSuchMethodError: wm.common.session.Common.getCustRptListFromMax Denny Java 1 05-01-2008 07:33 AM
How can I share common data structures and constants Sean Perl Misc 3 04-18-2008 07:42 AM
supplying constants in an extension module Torsten Mohr Python 1 01-17-2005 09:23 PM
Type constants in type module Dave Harris Python 1 11-02-2003 10:51 PM



Advertisments