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