Shell-GetEnv

 view release on metacpan or  search on metacpan

lib/Shell/GetEnv.pm  view on Meta::CPAN

use Carp;

use File::Temp;
use Shell::GetEnv::Dumper;

our $VERSION = '0.10';

my $status_var = 'p5_SHELL_GETENV_STATUS';

# a compendium of shells
my %shells = (
    bash => {
        interactive => 'i',
        nostartup   => '--noprofile',
        verbose     => 'v',
        echo        => 'x',
        login       => 'l',
        save_status => qq[export $status_var=\$?],
    },

    zsh => {
        interactive => 'i',
        nostartup   => 'p',
        verbose     => 'v',
        echo        => 'x',
        login       => 'l',
        save_status => qq[export $status_var=\$?],
    },

    dash => {
        interactive => 'i',
        verbose     => 'v',
        echo        => 'x',
        login       => 'l',
        save_status => qq[export $status_var=\$?],
    },

    sh => {
        interactive => 'i',
        verbose     => 'v',
        echo        => 'x',
        login       => 'l',
        save_status => qq[$status_var=\$?; export $status_var],
    },

    ksh => {
        interactive => 'i',
        nostartup   => 'p',
        verbose     => 'v',
        echo        => 'x',
        login       => 'l',
        save_status => qq[export $status_var=\$?],
    },

    csh => {
        interactive => 'i',
        nostartup   => 'f',
        echo        => 'x',
        verbose     => 'v',
        login       => 'l',
        save_status => qq[setenv $status_var \$status],
    },

    tcsh => {
        interactive => 'i',
        nostartup   => 'f',
        echo        => 'x',
        verbose     => 'v',
        login       => 'l',
        save_status => qq[setenv $status_var \$status],
    },
);


my %Opts = (
    startup     => 1,
    login       => 0,
    debug       => 0,
    echo        => 0,
    login       => 0,
    verbose     => 0,
    interactive => 0,
    redirect    => 1,
    stderr      => undef,
    stdout      => undef,
    expect      => 0,
    timeout     => 10,
    shellopts   => undef,
);


sub new
{
    my $class = shift;
    my $shell = shift;

    croak( __PACKAGE__, "->new: unsupported shell: $shell\n" )
      unless defined $shells{$shell};

    my %opt = %{ 'HASH' eq ref( $_[-1] ) ? pop : {} };

    # now want lc, but keep backwards compat
    $opt{lc $_} = delete $opt{$_} for keys %opt;

    my @notvalid = grep { ! exists $Opts{$_} } keys %opt;
    croak( __PACKAGE__, "->new: illegal option(s): @notvalid\n" )
      if @notvalid;

    my $self = bless { %Opts, %opt,
		       cmds => [@_],
		       shell => $shell
		     } , $class;

    # needed to get correct hash key for %shells
    $self->{nostartup} = ! $self->{startup};

    $self->_getenv;

    return $self;
}

# use temporary script files and output files to get the environment
# requires that a shell have a '-i' flag to act as an interactive shell
sub _getenv
{
    my $self = shift;

    # file to hold the environment
    my $fh_e = File::Temp->new( )
      or croak( __PACKAGE__, ": unable to create temporary environment file" );



( run in 0.338 second using v1.01-cache-2.11-cpan-39bf76dae61 )