App-Env
view release on metacpan or search on metacpan
lib/App/Env.pm view on Meta::CPAN
my %env;
@env{@vars} = map { exists $env->{$_} ? $env->{$_} : undef } @vars;
return \%env;
}
}
#-------------------------------------------------------
sub setenv {
my $self = shift;
my $var = shift;
defined $var
or App::Env::_Util::croak( "missing variable name argument\n" );
if ( @_ ) {
$self->_envhash->{$var} = $_[0];
}
else {
delete $self->_envhash->{$var};
}
}
#-------------------------------------------------------
# return an env compatible string
sub str {
my $self = shift;
my @opts = ( 'HASH' eq ref $_[-1] ? pop : {} );
# validate type. Params::Validate doesn't do Regexp, so
# this is a bit messy.
my %opt = Params::Validate::validate(
@opts,
{
Exclude => {
callbacks => { 'type' => \&App::Env::_Util::exclude_param_check },
optional => 1,
},
AllowIllegalVariableNames => {
optional => 1,
default => !!0,
},
} );
my $include = [ @_ ? @_ : qr/.*/ ];
my @exclude
= defined( $opt{Exclude} )
? ( 'ARRAY' eq ref $opt{Exclude} ? @{ $opt{Exclude} } : ( $opt{Exclude} ) )
: ();
push @exclude, 'TERMCAP'
if List::Util::none { $_ eq 'TERMCAP' } @$include;
# exclude any variables with non-word characters
push @exclude, qr/\W/
unless $opt{AllowIllegalVariableNames};
my $env = $self->_envhash;
my @vars = grep { exists $env->{$_} } $self->_filter_env( $include, \@exclude );
return join( q{ }, map { "$_=" . App::Env::_Util::shell_escape( $env->{$_} ) } @vars );
}
#-------------------------------------------------------
# return a list of included variables, in the requested
# order, based upon a list of include and exclude specs.
# variable names passed as plain strings are not checked
# for existance in the environment.
sub _filter_env {
my ( $self, $included, $excluded ) = @_;
my @exclude = $self->_match_var( $excluded );
my %exclude = map { $_ => 1 } @exclude;
return grep { !$exclude{$_} } $self->_match_var( $included );
}
#-------------------------------------------------------
# return a list of variables which matched the specifications.
# this takes a list of scalars, coderefs, or regular expressions.
# variable names passed as plain strings are not checked
# for existance in the environment.
sub _match_var {
my ( $self, $match ) = @_;
my $env = $self->_envhash;
$match = [$match] unless 'ARRAY' eq ref $match;
my @keys;
for my $spec ( @$match ) {
next unless defined $spec;
## no critic( ControlStructures::ProhibitCascadingIfElse)
if ( !ref $spec ) {
# always return a plain name. this allows
# @values = $env->env( @names) to work.
push @keys, $spec;
}
elsif ( 'Regexp' eq ref $spec ) {
push @keys, grep { /$spec/ } keys %$env;
}
elsif ( 'CODE' eq ref $spec ) {
push @keys, grep { $spec->( $_, $env->{$_} ) } keys %$env;
}
elsif ( 'ARRAY' eq ref $spec ) {
push @keys, $self->_match_var( $spec );
}
else {
App::Env::_Util::croak( 'match specification is of unsupported type: ', ref $spec, "\n" );
}
}
return @keys;
}
#-------------------------------------------------------
lib/App/Env.pm view on Meta::CPAN
To avoid mistaking this context for L</Context 1> if the I<match specification>
is a single string, enclose it in an array, e.g.
# this is context 1
$value = $env->env( $variable_name );
# this is context 3
$hash = $env->env( [ $variable_name ] );
=back
The B<%options> hash may contain the following entries:
=over
=item Exclude
Variable names may be excluded from the list by passing a hash with
the key C<Exclude> as the last argument (valid only in L</Context 0>
and L</Context 3>). The value is either a scalar or an arrayref
composed of match specifications (as an arrayref) as described in
L</Context 3>.
=item AllowIllegalVariableNames
Not all variable names may be set with a shell command, but may be
inherited from the parent environment. This option controls whether
such variables are returned by L</env>. It defaults to true.
=back
=item setenv
# set an environmental variable
$env->setenv( $var, $value );
# delete an environmetal variable
$env->setenv( $var );
If C<$value> is present, assign it to the named environmental
variable. If it is not present, delete the variable.
B<Note:> If the environment refers to a cached environment, this will
affect all instances of the environment which share the cache.
=item module
$module = $env->module;
This returns the name of the module which was used to load the
environment. If multiple modules were used, the names are
concatenated, separated by the C<$;> (subscript separator) character.
=item str
$envstr = $env->str( @match_specifications, \%options );
This function returns a string which may be used with the *NIX B<env>
command to set the environment. The string contains space separated
C<var=value> pairs, with shell magic characters escaped.
The environment may be pared down by passing I<match specifications>
and an C<Exclude> option; see the documentation for the B<env> method,
L</Context 4>, for more information.
Because the B<TERMCAP> environment variable is often riddled with
escape characters, which are not always handled well by shells, the
B<TERMCAP> variable is I<always> excluded unless it is explicitly
included via an exact variable name match specification. For example,
$envstr = $env->str( qr/.*/, 'TERMCAP );
is the only means of getting all of the environment returned.
B<Note!> By default variables with illegal names (that is,
characters which match B<qr/\W/> are I<not> output).
To output them, set the B<AllowIllegalVariableNames> option to a true
value (it defaults to false).
The B<%options> hash may contain the following entries:
=over
=item AllowIllegalVariableNames
Not all variable names may be set with a shell command, but may be
inherited from the parent environment. This option controls whether
such variables are returned by L</str>. It defaults to false.
=back
=item system
$env->system( $command, @args );
This runs the passed command in the environment defined by B<$env>.
It has the same argument and returned value convention as the core
Perl B<system> command.
If the B<SysFatal> flag is set for this environment,
B<IPC::System::Simple::system> is called, which will cause this method
to throw an exception if the command returned a non-zero exit value.
It also avoid invoking a shell to run the command if possible.
=item exec
$env->exec( $command, @args );
This execs the passed command in the environment defined by B<$env>.
It has the same argument and returned value convention as the core
Perl B<exec> command.
=item qexec
$output = $env->qexec( $command, @args );
@lines = $env->qexec( $command, @args );
This acts like the B<qx{}> Perl operator. It executes the passed
command in the environment defined by B<$env> and returns its
(standard) output. If called in a list context the output is
split into lines.
If the B<SysFatal> flag is set for this environment,
B<IPC::System::Simple::capture> is called, which will cause this
method to throw an exception if the command returned a non-zero exit
value. It also avoid invoking a shell to run the command if possible.
( run in 1.544 second using v1.01-cache-2.11-cpan-59e3e3084b8 )