root/main/trunk/package-kits/env_example/env.pl @ 29701

Revision 29689, 0.7 KB (checked in by jts21, 5 years ago)

Example of reliably sourcing a shell script for environment variables from within perl

  • Property svn:executable set to *
Line 
1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5use utf8;
6
7my $command =
8# Source the setup script to get environment variables.
9# Ensure that no output is printed.
10'. ./setup.sh >/dev/null 2>/dev/null;' .
11# While in the environment which has the new variables,
12# Use a perl command to print the environment variables as valid perl code
13'perl -e \'for (keys %ENV) { print "\$ENV{\'\\\'\'", $_, "\'\\\'\'} = \'\\\'\'", $ENV{$_}, "\'\\\'\';\n"; }\'';
14# Evaluate the perl code to set the variables in our own environment
15eval `$command`;
16# Print out the environment variables (as an example to show it works)
17for (keys %ENV) {
18    print $_, ' = ', "'$ENV{$_}'", "\n";
19}
Note: See TracBrowser for help on using the browser.