File Coverage

blib/lib/Shell/Source.pm
Criterion Covered Total %
statement 56 58 96.5
branch 12 20 60.0
condition 7 12 58.3
subroutine 10 11 90.9
pod 0 6 0.0
total 85 107 79.4


line stmt bran cond sub pod time code
1             # Copyright 1997-2001, Paul Johnson (pjcj@cpan.org)
2              
3             # This software is free. It is licensed under the same terms as Perl itself.
4              
5             # The latest version of this software should be available from my homepage:
6             # http://www.pjcj.net
7              
8 6     6   55868 use strict;
  6         16  
  6         342  
9              
10             require 5.004;
11              
12             package Shell::Source;
13              
14 6     6   33 use vars qw($VERSION);
  6         13  
  6         296  
15              
16             $VERSION = "0.01";
17              
18 6     6   32 use Carp;
  6         15  
  6         358  
19 6     6   5377 use FileHandle;
  6         81398  
  6         37  
20              
21             my $shells =
22             {
23             csh => "csh -f -c 'source [[file]]; env' |",
24             tcsh => "tcsh -f -c 'source [[file]]; env' |",
25             sh => "sh -c '. [[file]]; env' |",
26             ksh => "ksh -c '. [[file]]; env' |",
27             zsh => "zsh -c '. [[file]]; env' |",
28             bash => "bash -norc -noprofile -c '. [[file]]; env' |",
29             };
30              
31             sub new
32             {
33 2     2 0 2935 my $class = shift;
34 2         11 my $self = { @_ };
35 2 50       15 croak "Must specify type of shell" unless $self->{shell};
36 2   33     25 $self->{run} ||= $shells->{$self->{shell}};
37 2 50       7 croak "Must specify how to run unknown shell $self->{shell}"
38             unless $self->{run};
39 2         4 push @{$self->{ignore}}, qw( TIMEFMT PWD _ );
  2         9  
40 2         9 bless $self, $class;
41 2 50       21 $self->run if length $self->{file};
42 2         29 $self
43             }
44              
45             sub run
46             {
47 2     2 0 7 my $self = shift;
48 2   33     14 my $file = shift || $self->{file};
49 2 50       19 croak "Must specify file to source" unless length $self->{file};
50 2         21 (my $run = $self->{run}) =~ s/\[\[file\]\]/$self->{file}/g;
51 2 50       12 my $fh = $self->{fh}
52             = FileHandle->new($run) or croak "Can't run $self->{shell}";
53 2         19704 $self->_parse;
54 2 50       55 $fh->close or croak "Can't close $self->{shell}";
55 2         131 $self
56             }
57              
58             sub _parse
59             {
60 2     2   30 my $self = shift;
61 2         19 my $fh = $self->{fh}; # FileHandle ready for reading
62 2         12 my $env = 0; # for control of multi-line variables
63 2         9557 while (defined(my $line = <$fh>))
64             {
65 52 100       362 if ($line =~ /^(\w+)=(.*)$/)
66             {
67 50         124 $env = 1;
68 50 100 66     735 if ((!defined $ENV{$1} || $ENV{$1} ne $2) &&
  12   100     370  
69 4         31 !grep {$1 eq $_} @{$self->{ignore}})
70             {
71 3         48 $self->{env}{$1} = $2;
72             }
73             }
74             else
75             {
76 2 50       14 push (@{$self->{output}}, $line) unless $env;
  2         7822  
77             }
78             }
79             $self
80 2         8 }
81              
82             sub inherit
83             {
84 2     2 0 719 my $self = shift;
85 2         234 while (my ($key, $val) = each (%{$self->{env}}))
  5         39  
86             {
87 3         52 $ENV{$key} = $val;
88             }
89             }
90              
91             sub shell
92             {
93 2     2 0 6 my $self = shift;
94 2         10 my $shell = "";
95 2         4 while (my ($key, $val) = each (%{$self->{env}}))
  5         110  
96             {
97 3         14 $shell .= qq($key="$val"; export $key\n);
98             }
99             $shell
100 2         10 }
101              
102             sub output
103             {
104 2     2 0 354 my $self = shift;
105 2 50       15 join("\n", @{$self->{output}}) if defined $self->{output}
  2         18  
106             }
107              
108             sub env
109             {
110 0     0 0   my $self = shift;
111 0           $self->{env}
112             }
113              
114             1;
115              
116             __END__