|
NAMEShell::EnvImporter - Perl extension for importing environment variable changes from external commands or shell scriptsSYNOPSISuse Shell::EnvImporter; # Import environment variables exported from a shell script my $sourcer = Shell::EnvImporter->new( file => $filename, ); # Import environment variables exported from a shell command my $runner = Shell::EnvImporter->new( command => $shell_command, ); # Exert more control over the process my $importer = Shell::EnvImporter->new( shell => $shellname, command => $command, # -- OR -- file => $file, auto_run => 0, auto_import => 0, import_modified => 1, import_added => 1, import_removed => 1, # -- OR -- import_filter => $coderef ); my $result = $importer->run() or die "Run failed: $@"; # Manual import by policy $importer->env_import(); # -- OR -- # Manual import by filter $importer->env_import_filtered(); # Restore environment $importer->restore_env or die "Restore failed: $@"; DESCRIPTIONShell::EnvImporter allows you to import environment variable changes exported by an external script or command into the current environment. The process happens in (up to) three stages:Execution - saves a backup copy of the current environment (%ENV) - creates a shell script that sources the specified file (or runs the specified command) and prints out the environment - runs the shell script in a separate process - parses the output to determine success or failure and, on success, any changes to the environment Importation - imports variable changes by policy or filter. Restoration - restores the environment (%ENV) to pre-run state If 'auto_run' is true (the default), execution is kicked off automatically by the constructor. If 'auto_import' is true (the default), importation is kicked off automatically after execution. Restoration never happens automatically; you must call the restore() method explicitly. CONTROLLING IMPORTSImports are controlled by two factors:- the shell ignore list - the import policy or the import filter The shell ignore list is a list of variables to ignore, maintained by the shell object. These are generally variables that are changed automatically by the shell (e.g. SHLVL and PWD), providing little information to a noninteractive shell. The ignore list can be modified using the shell object's ignore() and ignore_*() methods; see the Shell::EnvImporter::Shell documentation for details. The ignore list overrides the import policy or the import filter (whichever is in effect). An import policy indicates what action to take for each kind of environment change. If import_added is true, new variables added to the environment will be imported. If import_modified is true, variables whose value is changed will be imported. If import_removed is true, variables unset by the external script or command will be removed from the environment. The default policy is to import added and modified variables but not removed variables. An import filter provides more control over importation. The import filter is a reference to a function that will be called with the variable name, the new value, and the type of change ('added', 'modified' or 'removed'); if it returns a true value, the variable will be imported. The import filter, if provided, overrides the import policy. CONSTRUCTOR
METHODS
DATA MEMBERS
EXAMPLES- Command Import# Import environment variables set by a shell command my $importer = Shell::EnvImporter->new( command => 'ssh-agent' ) or die $@; - "Sourced" File Import# Import environment variables exported by a configuration file my $importer = Shell::EnvImporter->new( file => "$ENV{'HOME'}/.profile" ) or die $@; - Policy import - modified only, bash scriptmy $importer = Shell::EnvImporter->new( file => '/etc/bashrc', shell => 'bash', import_modified => 1, import_added => 0, import_removed => 0, ); - Import a specific variablemy $file = '/etc/mydaemon.conf'; my $importer = Shell::EnvImporter->new( file => $file, shell => 'bash', auto_import => 0, ); my $result = $importer->env_import('MAX_CLIENTS'); if ($result->succeeded) { print "Max clients: $ENV{'MAX_CLIENTS'}\n"; } else { die("Error: Source of '$file' failed: ", $result->stderr, "\n"); } - Filtered import - all 'DB*' vars whose value references my homedirmy $file = '/etc/mydaemon.conf'; my $filter = sub { my($var, $value, $change) = @_; return ($var =~ /^DB/ and $value =~ /$ENV{HOME}/); }; my $importer = Shell::EnvImporter->new( file => $file, shell => 'bash', import_filter => $filter, ); print "Imported: ", join(", ", $importer->result->imported), "\n"; - Unexported Variables in Bourne-like shells# Get the default system font from /etc/sysconfig/i18n (a /bin/sh # script). Note that the variable is NOT exported, only set, so we # use the 'set' command to print the environment. my $sourcer = Shell::EnvImporter->new( file => '/etc/sysconfig/i18n', ) or die $@; $sourcer->shellobj->envcmd('set'); $sourcer->run(); print "System font: $ENV{SYSFONT}\n"; SEE ALSOShell::EnvImporter::Result Shell::EnvImporter::ShellAUTHORDavid Faraldo, <dfaraldo@cpan.org>COPYRIGHT AND LICENSECopyright (C) 2005-2006 by Dave Faraldo This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. No warranty is expressed or implied.
Visit the GSP FreeBSD Man Page Interface. |