This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[PATCH 3/3] Provide wrappers for IN6ADDR_ANY and IN6ADDR_LOOPBACK
[perl5.git] / lib / dotsh.pl
CommitLineData
0111154e
Z
1warn "Legacy library @{[(caller(0))[6]]} will be removed from the Perl core distribution in the next major release. Please install it from the CPAN distribution Perl4::CoreLibs. It is being used at @{[(caller)[1]]}, line @{[(caller)[2]]}.\n";
2
a0d0e21e
LW
3#
4# @(#)dotsh.pl 03/19/94
5#
a6d71656
GS
6# This library is no longer being maintained, and is included for backward
7# compatibility with Perl 4 programs which may require it.
6d10188c
S
8# This legacy library is deprecated and will be removed in a future
9# release of perl.
a6d71656
GS
10#
11# In particular, this should not be used as an example of modern Perl
12# programming techniques.
13#
a0d0e21e
LW
14# Author: Charles Collins
15#
16# Description:
17# This routine takes a shell script and 'dots' it into the current perl
18# environment. This makes it possible to use existing system scripts
19# to alter environment variables on the fly.
20#
21# Usage:
22# &dotsh ('ShellScript', 'DependentVariable(s)');
23#
24# where
25#
26# 'ShellScript' is the full name of the shell script to be dotted
27#
28# 'DependentVariable(s)' is an optional list of shell variables in the
29# form VARIABLE=VALUE,VARIABLE=VALUE,... that 'ShellScript' is
30# dependent upon. These variables MUST be defined using shell syntax.
31#
32# Example:
2359510d
SD
33# &dotsh ('/foo/bar', 'arg1');
34# &dotsh ('/foo/bar');
35# &dotsh ('/foo/bar arg1 ... argN');
a0d0e21e 36#
6d10188c 37
a0d0e21e
LW
38sub dotsh {
39 local(@sh) = @_;
90758174
AD
40 local($tmp,$key,$shell,$command,$args,$vars) = '';
41 local(*dotsh);
42 undef *dotsh;
a0d0e21e
LW
43 $dotsh = shift(@sh);
44 @dotsh = split (/\s/, $dotsh);
45 $command = shift (@dotsh);
46 $args = join (" ", @dotsh);
47 $vars = join ("\n", @sh);
48 open (_SH_ENV, "$command") || die "Could not open $dotsh!\n";
49 chop($_ = <_SH_ENV>);
50 $shell = "$1 -c" if ($_ =~ /^\#\!\s*(\S+(\/sh|\/ksh|\/zsh|\/csh))\s*$/);
51 close (_SH_ENV);
52 if (!$shell) {
90758174 53 if ($ENV{'SHELL'} =~ /\/sh$|\/ksh$|\/zsh$|\/bash$|\/csh$/) {
a0d0e21e
LW
54 $shell = "$ENV{'SHELL'} -c";
55 } else {
56 print "SHELL not recognized!\nUsing /bin/sh...\n";
57 $shell = "/bin/sh -c";
58 }
59 }
60 if (length($vars) > 0) {
2359510d 61 open (_SH_ENV, "$shell \"$vars && . $command $args && set \" |") || die;
a0d0e21e 62 } else {
2359510d 63 open (_SH_ENV, "$shell \". $command $args && set \" |") || die;
a0d0e21e
LW
64 }
65
a0d0e21e
LW
66 while (<_SH_ENV>) {
67 chop;
3730ca99 68 m/^([^=]*)=(.*)/s;
69 $ENV{$1} = $2;
a0d0e21e
LW
70 }
71 close (_SH_ENV);
a0d0e21e 72
4633a7c4 73 foreach $key (keys(%ENV)) {
a0d0e21e
LW
74 $tmp .= "\$$key = \$ENV{'$key'};" if $key =~ /^[A-Za-z]\w*$/;
75 }
76 eval $tmp;
77}
781;