Zoidberg
view release on metacpan or search on metacpan
lib/Zoidberg/Fish/Commands.pm view on Meta::CPAN
$self->{shell}->exit;
}
=item eval I<cmd>
Eval I<cmd> like a shell command. Main use of this is to
run code stored in variables.
=cut
sub eval {
my $self = shift;
$$self{shell}->shell(@_);
}
=item export I<var>=I<value>
Set the environment variable I<var> to I<value>.
TODO explain how export moved varraibles between the perl namespace and the environment
=cut
sub export { # TODO if arg == 1 and not hash then export var from zoid::eval to env :D
my $self = shift;
my ($opt, $args, $vals) = getopt 'unexport,n print,p *', @_;
my $class = $$self{shell}{settings}{perl}{namespace};
no strict 'refs';
if ($$opt{unexport}) {
for (@$args) {
s/^([\$\@]?)//;
next unless exists $ENV{$_};
if ($1 eq '@') { @{$class.'::'.$_} = split ':', delete $ENV{$_} }
else { ${$class.'::'.$_} = delete $ENV{$_} }
}
}
elsif ($$opt{print}) {
output [ map {
my $val = $ENV{$_};
$val =~ s/'/\\'/g;
"export $_='$val'";
} sort keys %ENV ];
}
else { # really export
for (@$args) {
s/^([\$\@]?)//;
if ($1 eq '@') { # arrays
my @env = defined($$vals{$_}) ? (@{$$vals{$_}}) :
defined(*{$class.'::'.$_}{ARRAY}) ? (@{$class.'::'.$_}) : () ;
$ENV{$_} = join ':', @env if @env;
}
else { # scalars
my $env = defined($$vals{$_}) ? $$vals{$_} :
defined(${$class.'::'.$_}) ? ${$class.'::'.$_} : undef ;
$ENV{$_} = $env if defined $env;
}
}
}
}
=item setenv I<var> I<value>
Like B<export>, but with a slightly different syntax.
=cut
sub setenv {
shift;
my $var = shift;
$ENV{$var} = join ' ', @_;
}
=item unsetenv I<var>
Set I<var> to undefined.
=cut
sub unsetenv {
my $self = shift;
delete $ENV{$_} for @_;
}
=item set [+-][abCefnmnuvx]
=item set [+o|-o] I<option>
Set or unset a shell option. Although sometimes confusing
a '+' switch unsets the option, while the '-' switch sets it.
Short options correspond to the following names:
a => allexport *
b => notify
C => noclobber
e => errexit *
f => noglob
m => monitor *
n => noexec *
u => nounset *
v => verbose
x => xtrace *
*) Not yet supported by the rest of the shell
See L<zoiduser> for a description what these and other options do.
FIXME takes also hash arguments
=cut
sub set {
my $self = shift;
unless (@_) { error 'should print out all shell vars, but we don\'t have these' }
my ($opts, $keys, $vals) = getopt
'allexport,a notify,b noclobber,C errexit,e
noglob,f monitor,m noexec,n nounset,u
verbose,v xtrace,x -o@ +o@ *', @_;
# other posix options: ignoreeof, nolog & vi - bash knows a bit more
my %settings;
if (%$opts) {
$settings{$_} = $$opts{$_}
for grep {$_ !~ /^[+-]/} @{$$opts{_opts}};
if ($$opts{'-o'}) { $settings{$_} = 1 for @{$$opts{'-o'}} }
if ($$opts{'+o'}) { $settings{$_} = 0 for @{$$opts{'+o'}} }
}
for (@$keys) { $settings{$_} = defined($$vals{$_}) ? delete($$vals{$_}) : 1 }
for my $opt (keys %settings) {
if ($opt =~ m#/#) {
my ($hash, $key, $path) = path2hashref($$self{shell}{settings}, $opt);
error "$path: no such hash in settings" unless $hash;
$$hash{$key} = $settings{$opt};
}
else { $$self{shell}{settings}{$opt} = $settings{$opt} }
}
}
( run in 1.262 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )