views:

238

answers:

3

In importing the environment from a subcommand, I want to add all environment variables exported from a bash script to a hash. When program gets run, it will set up some variables and export them. I'd like to save those variables in the Perl script for later. However I don't want to take the bash functions defined in the subcommand. Currently, I have a block like:

foreach (`program; env`)
{
    next if /^\(\)/;
    my ($a, $b) = split("=", $_);
    if( /^(\w+)=(.*)$/ ) {
        $hash{$1} = $2;    
    }
}

Is there a better way to do this? I'm not sure if matching the initial () is safe. Bonus points for handling newlines in environment variables, which I'm just closing my eyes for right now.

+1  A: 

I am assuming that the environment variables after program has executed are not same as the environment passed to it (which you can find in %ENV as explained in jeje's answer.

I am by no means knowledgeable about bash, so I am only going to address the part of the question about parsing the output of env.

#!/usr/bin/perl

use strict;
use warnings;

use autodie qw( open close );

$ENV{WACKO} = "test\nstring\nwith\nnewlines\n\n";

my %SUBENV;

open my $env_h, '-|', 'env';

my $var;

while ( my $line = <$env_h> ) {
    chomp $line;
    if ( my ($this_var, $this_val) = $line =~ /^([^=]+)=(.+)$/ ) {
        if ( $this_val =~ /^\Q()\E/ ) {
            $var = q{};
            next;
        }
        $var = $this_var;
        $SUBENV{ $var } = $this_val;
    }
    elsif ( $var ) {
        $SUBENV{ $var } .= "\n$line";
    }
}

use Data::Dumper;
print Dumper \%SUBENV;
Sinan Ünür
A: 

This should be fine for getting all of the environment variables.

for(`program; env`){
  if( /^([^=]+)=(.*)$/ ) {
    $hash{$1} = $2;    
  }
}

If you want to start with a clean slate this might work better.

for(`env -i bash -c "program; env"`){
  next if /\(\)/;
  if( /^([^=]+)=(.*)$/ ) {
    $hash{$1} = $2;    
  }
}

env -i makes it's subcommand start off with a clean slate.

It calls bash with the -c argument, and the commands to run. We need to do that because otherwise the second env wouldn't get the environment variables from the program.

Brad Gilbert
+5  A: 

What you want is there: Shell-EnvImporter

An example:

  use Shell::EnvImporter;

  # Import environment variables exported from a shell script
  my $sourcer  = Shell::EnvImporter->new(
                   file => $filename,
                 );


  my $result = $sourcer->run() or die "Run failed: $@";
jeje
It might be a good idea to include an example.
Brad Gilbert
Ok, I copy/paste from the doc
jeje
Brilliant! But I think you better delete your other answer.
innaM