Shell::GetEnv - extract the environment from a shell after executing commands
use Shell::GetEnv; $env = Shell::GetEnv->new( $shell, $command ); $status = $env->status; $envs = $env->envs( %opts ) $env->import_envs( %opts );
Shell::GetEnv provides a facility for obtaining changes made to environmental variables as the result of running shell scripts. It does this by causing a shell to invoke a series of user provided shell commands (some of which might source scripts) and having the shell process store its environment (using a short Perl script) into a temporary file, which is parsed by Shell::Getenv.
Communications with the shell subprocess may be done via standard IPC (via a pipe), or may be done via the Perl Expect module (necessary if proper execution of the shell script requires the shell to be attached to a "real" terminal).
The new environment may be imported into the current one, or may be returned either as a hash or as a string suitable for use with the *NIX env command.
$env = Shell::GetEnv->new( $shell, @cmds, \%attrs );
Start the shell specified by $shell, run the passed commands, and retrieve the environment. Note that only shell built-in commands can actually change the shell's environment, so typically the commands source a startup file. For example:
$env = Shell::GetEnv->new( 'tcsh', 'source foo.csh' );
The supported shells are:
csh tcsh bash sh ksh zsh dash
Attributes:
startup
If true, the user's shell startup files are invoked. This flag is supported for csh, tcsh, and bash. This is emulated under ksh using its -p flag, which isn't quite the same thing.
csh
tcsh
bash
There seems to be no clean means of turning off startup file processing under the other shells.
This defaults to true.
echo
If true, put shell is put in echo mode. This is only of use when the stdout attribute is used. It defaults to false.
stdout
interactive
If true, put the shell in interactive mode. Some shells do not react well when put in interactive mode but not connected to terminals. Try using the expect option instead. This defaults to false.
expect
login
If true, invoke the shell as a login shell. Defaults to false.
tcsh and csh will only honor this option if no other command line options are passed. For these shells Shell::GetEnv will throw an exception if this option conflicts with another.
redirect
If true, redirect the output and error streams (see also the STDERR and stdout options). Defaults to true.
STDERR
verbose
If true, put the shell in verbose mode. This is only of use when the stdout attribute is used. It defaults to false.
stderr
Normally output from the shells' standard error stream is discarded. This may be set to a file name to which the stream should be written. See also the redirect option.
Normally output from the shells' standard output stream is discarded. This may be set to a file name to which the stream should be written. See also the Redirect option.
Redirect
If true, the Perl Expect module is used to communicate with the subshell. This is useful if it is necessary to simulate connection with a terminal, which may be important when setting up some enviroments.
timeout
The number of seconds to wait for a response from the shell when using Expect. It defaults to 10 seconds.
shellopts
Arbitrary options to be passed to the shell.
$env = $env->envs( [%opt] );
Return the environment. Typically the environment is returned as a hashref, but if the envstr option is true it will be returned as a string suitable for use with the *NIX env command. If no options are specified, the entire environment is returned.
envstr
The following options are recognized:
diffsonly
If true, the returned environment contains only those variables which are new or which have changed from the current environment. There is no way of indicating Variables which have been deleted.
exclude
This specifies variables to exclude from the returned environment. It may be either a single value or an array of values.
A value may be a string (for an exact match of a variable name), a regular expression created with the qr operator, or a subroutine reference. The subroutine will be passed two arguments, the variable name and its value, and should return true if the variable should be excluded, false otherwise.
If true, a string representation of the environment is returned, suitable for use with the *NIX env command. Appropriate quoting is done so that it is correclty parsed by shells.
If the zapdeleted option is also specified (and is true) variables which are present in the current environment but not in the new one are explicitly deleted by inserting -u variablename in the output string. Note, however, that not all versions of env recognize the -u option (e.g. those in Solaris or OS X). In those cases, to ensure the correct environment, use diffsonly = 0, zapdeleted => 0> and invoke env with the -i option.
zapdeleted
-u variablename
diffsonly =
-i
$status = $env->status;
Returns the invoked shell's status after executing the commands provided to the constructor. See "system" in perlfunc for instructions on how to interpret the status.
$env->import_envs( %opt )
Import the new environment into the current one. The available options are:
If true, variables which are present in the current environment but not in the new one are deleted from the current environment.
None by default.
There are other similar modules on CPAN. Shell::Source is simpler, Shell::EnvImporter is a little more heavyweight (requires Class::MethodMaker).
This module's unique features:
The YAML::Tiny module is preferred for saving the environment (because of its smaller footprint); the Data::Dumper module will be used if it is not available.
The Expect module is required only if the expect option is specified.
Diab Jerius, <djerius@cpan.org>
Copyright 2007 Smithsonian Astrophysical Observatory
This software is released under the GNU General Public License. You may find a copy at
http://www.gnu.org/licenses
To install Shell::GetEnv, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Shell::GetEnv
CPAN shell
perl -MCPAN -e shell install Shell::GetEnv
For more information on module installation, please visit the detailed CPAN module installation guide.