how to source an environment file

Discussion in 'Perl Misc' started by dcruncher4@aim.com, Nov 9, 2006.

  1. Guest

    I want my perl script to read a file containing environment variables
    and source it back
    to the script. That is, when the script is executed, these variables
    are not defined
    in enviroment. Once the script starts, variables defined in that env
    file is sourced
    in. In korn shell we can do
    dot(.) scriptname

    How do we do the same in perl.

    I found a crude approach. I do
    system(". scriptname; env > /tmp/env.$$")
    then I open /tmp/env.$$ file and store all env defined there
    in $ENV{envvar}.

    There should be a better way of doing it, shouldn't it?

    thanks.
     
    , Nov 9, 2006
    #1
    1. Advertising

  2. Dr.Ruud Guest

    schreef:
    > I want my perl script to read a file containing environment variables
    > and source it back
    > to the script. That is, when the script is executed, these variables
    > are not defined
    > in enviroment. Once the script starts, variables defined in that env
    > file is sourced
    > in. In korn shell we can do
    > dot(.) scriptname
    >
    > How do we do the same in perl.
    >
    > I found a crude approach. I do
    > system(". scriptname; env > /tmp/env.$$")
    > then I open /tmp/env.$$ file and store all env defined there
    > in $ENV{envvar}.
    >
    > There should be a better way of doing it, shouldn't it?


    If you make that file look like
    VAR1=test-A
    VAR2="test B"
    etc.

    and then split per line on the first "=", you can update %ENV more
    directly.

    Or make it a shell-script that calls your perl-script last:

    #!/bin/sh
    VAR1=test-A
    VAR2="test B"
    myscript.pl

    (untested)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Nov 9, 2006
    #2
    1. Advertising

  3. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > I want my perl script to read a file containing environment variables
    > and source it back
    > to the script. That is, when the script is executed, these variables
    > are not defined
    > in enviroment. Once the script starts, variables defined in that env
    > file is sourced
    > in. In korn shell we can do
    > dot(.) scriptname
    >
    > How do we do the same in perl.
    >
    > I found a crude approach. I do
    > system(". scriptname; env > /tmp/env.$$")
    > then I open /tmp/env.$$ file and store all env defined there
    > in $ENV{envvar}.
    >
    > There should be a better way of doing it, shouldn't it?


    You can use less shell and more Perl. You can also avoid the temp file.

    my $env_dump = qx(
    . scriptname;
    perl -MData::Dumper -e'print Dumper \\ %ENV'
    );

    %ENV = %{ our $VAR1; eval $env_dump };

    Anno
     
    -berlin.de, Nov 9, 2006
    #3
  4. On Nov 9, 2:52 pm, wrote:
    > I want my perl script to read a file containing environment variables
    > and source it back
    > to the script. That is, when the script is executed, these variables
    > are not defined
    > in enviroment. Once the script starts, variables defined in that env
    > file is sourced
    > in. In korn shell we can do
    > dot(.) scriptname
    >
    > How do we do the same in perl.


    I always find this question ammuzing. The real, but totally unhelpful,
    answer is that the nearest thing in Perl to Unix shells' "source"
    command is do(FILE). Of course that expect the file to be in Perl just
    as ksh expected it to be in ksh (and bash would expect it in bash and
    sh would ....)

    > I found a crude approach. I do
    > system(". scriptname; env > /tmp/env.$$")
    > then I open /tmp/env.$$ file and store all env defined there
    > in $ENV{envvar}.


    I think you can avoid the need for a temporary file with a pipe open.

    open(my $env_fh, '|-','. scriptname; env') or die $!;

    > There should be a better way of doing it, shouldn't it?


    Not really. If you want to execute a script written in another language
    then somehow or other you are going to have to fire up and interpreter
    for that laguage.

    You can also exec a ksh interpreter and have that exec the perl
    interpreter again. You do, of course, need to have an argument on your
    second invocation of your script to tell it not to do this again!

    Somthing like...

    exec(". scriptname; exec $^X $0 -not_again ". join " ", map { quotemeta
    } @ARGV );
     
    Brian McCauley, Nov 9, 2006
    #4
  5. wrote:
    > I want my perl script to read a file containing environment variables
    > and source it back
    > to the script. That is, when the script is executed, these variables
    > are not defined
    > in enviroment. Once the script starts, variables defined in that env
    > file is sourced
    > in. In korn shell we can do
    > dot(.) scriptname
    >
    > How do we do the same in perl.
    >
    > I found a crude approach. I do
    > system(". scriptname; env > /tmp/env.$$")
    > then I open /tmp/env.$$ file and store all env defined there
    > in $ENV{envvar}.
    >


    scriptname:
    export DUM1=foo
    ...

    perl -le 'BEGIN{ system(". /path/to/scriptname")== 0 or die "source
    failed: $?}; use Env; print $ENV{DUM1};'


    --
    Charles DeRykus
     
    Charles DeRykus, Nov 10, 2006
    #5
  6. -berlin.de Guest

    Charles DeRykus <> wrote in comp.lang.perl.misc:
    > wrote:
    > > I want my perl script to read a file containing environment variables
    > > and source it back
    > > to the script. That is, when the script is executed, these variables
    > > are not defined
    > > in enviroment. Once the script starts, variables defined in that env
    > > file is sourced
    > > in. In korn shell we can do
    > > dot(.) scriptname
    > >
    > > How do we do the same in perl.
    > >
    > > I found a crude approach. I do
    > > system(". scriptname; env > /tmp/env.$$")
    > > then I open /tmp/env.$$ file and store all env defined there
    > > in $ENV{envvar}.
    > >

    >
    > scriptname:
    > export DUM1=foo
    > ..
    >
    > perl -le 'BEGIN{ system(". /path/to/scriptname")== 0 or die "source
    > failed: $?}; use Env; print $ENV{DUM1};'


    Did you test that?

    Environment variables from .../scriptname will not become available. What
    is "use Env" supposed to do?

    Anno
     
    -berlin.de, Nov 10, 2006
    #6
  7. -berlin.de wrote:
    > Charles DeRykus <> wrote in comp.lang.perl.misc:
    >> ...
    >>>

    >> scriptname:
    >> export DUM1=foo
    >> ..
    >>
    >> perl -le 'BEGIN{ system(". /path/to/scriptname")== 0 or die "source
    >> failed: $?}; use Env; print $ENV{DUM1};'

    >
    > Did you test that?
    >
    > Environment variables from .../scriptname will not become available. What
    > is "use Env" supposed to do?



    Oops, sorry. A comedy of errors works well though if it's late, you
    source scriptname before perl comes on the scene, you engage in some
    shell sorcery , etc.



    --
    Charles DeRykus
     
    Charles DeRykus, Nov 10, 2006
    #7
  8. Dan Mercer Guest

    <> wrote in message news:...
    : I want my perl script to read a file containing environment variables
    : and source it back
    : to the script. That is, when the script is executed, these variables
    : are not defined
    : in enviroment. Once the script starts, variables defined in that env
    : file is sourced
    : in. In korn shell we can do
    : dot(.) scriptname
    :
    : How do we do the same in perl.
    :
    : I found a crude approach. I do
    : system(". scriptname; env > /tmp/env.$$")
    : then I open /tmp/env.$$ file and store all env defined there
    : in $ENV{envvar}.
    :
    : There should be a better way of doing it, shouldn't it?
    :
    : thanks.
    :

    Here's the source function from a module I wrote:

    #!/usr/bin/perl -w
    # Copyright Singing Pig Consulting 2005-
    #
    # Author Dan Mercer (Singing Pig Consulting)
    # Date Created: 09/11/04 at 01:48:42 PM

    use strict;
    use warnings;

    sub source
    {
    # Format: source file[,shell]
    # : modifies the current environment

    my ($srcfile, $shell, $legal, $srccmd, $pipeline, $perlcmd);
    # filehandles
    my ($hostsfh, $pipefh);
    my $me = (caller(0))[3];

    $srcfile = shift or die "$me: Missing source file argument\n";
    $shell = shift or $shell = $ENV{SHELL} or $shell = "sh";
    die "Unknown shell '$shell'\n" unless ($shell =~ /sh$/);

    $srcfile = cwd() . "/" . $srcfile unless($srcfile =~ m{/});
    # get full pathname
    unless ($shell =~ m{^/})
    {
    my @path = split(/:/, $ENV{PATH});
    foreach (@path)
    {
    next unless (-x "$_/$shell");
    $shell = "$_/$shell";
    last;
    }
    die "$me: unrecognized shell '$shell'\n" unless ($shell =~ m{^/});
    }

    # make sure it's a legal shell
    $legal = 0;
    if (open($hostsfh,"<","/etc/shells"))
    {
    while (<$hostsfh>)
    {
    chomp;
    if ($_ eq $shell)
    {
    $legal = 1;
    last;
    }
    }
    close $hostsfh;
    }
    else
    {
    $legal = 1;
    }

    die "$me: illegal shell '$shell'\n" unless ($legal);
    # determine source command
    $srccmd = ($shell =~ /csh$/) ? "source" : ".";

    # set up pipeline to source file then have perl print it out
    # using NUL's as file and record separators
    $perlcmd = "$^X -l0 -e '" . '\$,=\$\\;print %ENV' . "'";
    $pipeline = "$shell -c \"$srccmd $srcfile;$perlcmd\"";
    open ($pipefh, "$pipeline |") or die "$me: pipeline failed - $!\n";

    my @env = %ENV;

    local $/ = "\0"; # IRS to NUL - use local so WHHSH
    my @newenv = <$pipefh>;
    chomp @newenv;
    push @env,@newenv;
    close $pipefh;

    %ENV = @env;
    }
    __DATA__

    Dan Mercer
     
    Dan Mercer, Nov 11, 2006
    #8
  9. Dan Mercer Guest

    <> wrote in message news:...
    : I want my perl script to read a file containing environment variables
    : and source it back
    : to the script. That is, when the script is executed, these variables
    : are not defined
    : in enviroment. Once the script starts, variables defined in that env
    : file is sourced
    : in. In korn shell we can do
    : dot(.) scriptname
    :
    : How do we do the same in perl.
    :
    : I found a crude approach. I do
    : system(". scriptname; env > /tmp/env.$$")
    : then I open /tmp/env.$$ file and store all env defined there
    : in $ENV{envvar}.
    :
    : There should be a better way of doing it, shouldn't it?
    :
    : thanks.
    :

    Here's the source function from a module I wrote:

    #!/usr/bin/perl -w
    # Copyright Singing Pig Consulting 2005-
    #
    # Author Dan Mercer (Singing Pig Consulting)
    # Date Created: 09/11/04 at 01:48:42 PM

    use strict;
    use warnings;

    sub source
    {
    # Format: source file[,shell]
    # : modifies the current environment

    my ($srcfile, $shell, $legal, $srccmd, $pipeline, $perlcmd);
    # filehandles
    my ($hostsfh, $pipefh);
    my $me = (caller(0))[3];

    $srcfile = shift or die "$me: Missing source file argument\n";
    $shell = shift or $shell = $ENV{SHELL} or $shell = "sh";
    die "Unknown shell '$shell'\n" unless ($shell =~ /sh$/);

    $srcfile = cwd() . "/" . $srcfile unless($srcfile =~ m{/});
    # get full pathname
    unless ($shell =~ m{^/})
    {
    my @path = split(/:/, $ENV{PATH});
    foreach (@path)
    {
    next unless (-x "$_/$shell");
    $shell = "$_/$shell";
    last;
    }
    die "$me: unrecognized shell '$shell'\n" unless ($shell =~ m{^/});
    }

    # make sure it's a legal shell
    $legal = 0;
    if (open($hostsfh,"<","/etc/shells"))
    {
    while (<$hostsfh>)
    {
    chomp;
    if ($_ eq $shell)
    {
    $legal = 1;
    last;
    }
    }
    close $hostsfh;
    }
    else
    {
    $legal = 1;
    }

    die "$me: illegal shell '$shell'\n" unless ($legal);
    # determine source command
    $srccmd = ($shell =~ /csh$/) ? "source" : ".";

    # set up pipeline to source file then have perl print it out
    # using NUL's as file and record separators
    $perlcmd = "$^X -l0 -e '" . '\$,=\$\\;print %ENV' . "'";
    $pipeline = "$shell -c \"$srccmd $srcfile;$perlcmd\"";
    open ($pipefh, "$pipeline |") or die "$me: pipeline failed - $!\n";

    my @env = %ENV;

    local $/ = "\0"; # IRS to NUL - use local so WHHSH
    my @newenv = <$pipefh>;
    chomp @newenv;
    push @env,@newenv;
    close $pipefh;

    %ENV = @env;
    }
    __DATA__

    Dan Mercer
     
    Dan Mercer, Nov 11, 2006
    #9
  10. Dan Mercer Guest

    <> wrote in message news:...
    : I want my perl script to read a file containing environment variables
    : and source it back
    : to the script. That is, when the script is executed, these variables
    : are not defined
    : in enviroment. Once the script starts, variables defined in that env
    : file is sourced
    : in. In korn shell we can do
    : dot(.) scriptname
    :
    : How do we do the same in perl.
    :
    : I found a crude approach. I do
    : system(". scriptname; env > /tmp/env.$$")
    : then I open /tmp/env.$$ file and store all env defined there
    : in $ENV{envvar}.
    :
    : There should be a better way of doing it, shouldn't it?
    :
    : thanks.
    :

    Here's the source function from a module I wrote:

    #!/usr/bin/perl -w
    # Copyright Singing Pig Consulting 2005-
    #
    # Author Dan Mercer (Singing Pig Consulting)
    # Date Created: 09/11/04 at 01:48:42 PM

    use strict;
    use warnings;

    sub source
    {
    # Format: source file[,shell]
    # : modifies the current environment

    my ($srcfile, $shell, $legal, $srccmd, $pipeline, $perlcmd);
    # filehandles
    my ($hostsfh, $pipefh);
    my $me = (caller(0))[3];

    $srcfile = shift or die "$me: Missing source file argument\n";
    $shell = shift or $shell = $ENV{SHELL} or $shell = "sh";
    die "Unknown shell '$shell'\n" unless ($shell =~ /sh$/);

    $srcfile = cwd() . "/" . $srcfile unless($srcfile =~ m{/});
    # get full pathname
    unless ($shell =~ m{^/})
    {
    my @path = split(/:/, $ENV{PATH});
    foreach (@path)
    {
    next unless (-x "$_/$shell");
    $shell = "$_/$shell";
    last;
    }
    die "$me: unrecognized shell '$shell'\n" unless ($shell =~ m{^/});
    }

    # make sure it's a legal shell
    $legal = 0;
    if (open($hostsfh,"<","/etc/shells"))
    {
    while (<$hostsfh>)
    {
    chomp;
    if ($_ eq $shell)
    {
    $legal = 1;
    last;
    }
    }
    close $hostsfh;
    }
    else
    {
    $legal = 1;
    }

    die "$me: illegal shell '$shell'\n" unless ($legal);
    # determine source command
    $srccmd = ($shell =~ /csh$/) ? "source" : ".";

    # set up pipeline to source file then have perl print it out
    # using NUL's as file and record separators
    $perlcmd = "$^X -l0 -e '" . '\$,=\$\\;print %ENV' . "'";
    $pipeline = "$shell -c \"$srccmd $srcfile;$perlcmd\"";
    open ($pipefh, "$pipeline |") or die "$me: pipeline failed - $!\n";

    my @env = %ENV;

    local $/ = "\0"; # IRS to NUL - use local so WHHSH
    my @newenv = <$pipefh>;
    chomp @newenv;
    push @env,@newenv;
    close $pipefh;

    %ENV = @env;
    }
    __DATA__

    Dan Mercer
     
    Dan Mercer, Nov 12, 2006
    #10
    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. ~~~ .NET Ed ~~~
    Replies:
    1
    Views:
    4,226
    Cowboy \(Gregory A. Beamer\)
    Feb 26, 2005
  2. Matt C
    Replies:
    2
    Views:
    374
    Scott Ellsworth
    Aug 3, 2004
  3. Cloud Burst
    Replies:
    11
    Views:
    1,134
  4. Replies:
    0
    Views:
    321
  5. Replies:
    5
    Views:
    702
Loading...

Share This Page