Revision 2

Date:
2004/06/11 23:43:53
Author:
pjcj
Revision Log:
import Shell::Source 0.01
Files:

Legend:

 
Added
 
Removed
 
Modified
  • trunk/CHANGES

     
    1 Shell::Source.pm history
    2
    3 Release 0.01 - Initial release - 2nd August 2001
  • trunk/Makefile.PL

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 require 5.004;
    11
    12 use strict;
    13
    14 use ExtUtils::MakeMaker;
    15
    16 $| = 1;
    17
    18 my $Version = "0.01";
    19 my $Date = "2nd August 2001";
    20 my $Author = 'pjcj@cpan.org';
    21
    22 my @perlbug = ("perlbug", "-a", $Author,
    23 "-s", "Installation of Shell::Source $Version");
    24
    25 my $Perlbug = join " ", map { / / ? "'$_'" : $_ } @perlbug;
    26
    27
    28 open M, "MANIFEST" or die "Cannot open MANIFEST: $!";
    29 my @files = map { split } <M>;
    30 my @versions = grep { $_ ne "README" && $_ ne "Makefile.PL" } @files;
    31 close M or die "Cannot close MANIFEST: $!";
    32
    33 $ExtUtils::MakeMaker::Verbose = 0;
    34
    35 WriteMakefile
    36 (
    37 NAME => "Shell::Source",
    38 VERSION => $Version,
    39 DIR => [],
    40 PM => { map {($_ => '$(INST_LIBDIR)/' . $_)} grep { /\.pm/ } @files },
    41 dist => { COMPRESS => "gzip --best --force" },
    42 clean => { FILES => join " ", map { "$_.version" } @versions },
    43 depend => { distdir => "@files" },
    44 $] >= 5.005 ?
    45 (
    46 AUTHOR => 'Paul Johnson (pjcj@cpan.org)',
    47 ABSTRACT => "Run programs and inherit environment changes",
    48 ) : (),
    49 );
    50
    51 print <<EOM if 0;
    52
    53 --------------------------------------------------------------------------------
    54
    55 I like to have some idea of the distribution of this software. To that end I
    56 would be very grateful for mail from you. This will be used only to satisfy my
    57 curiosity and to help me make decisions which may involve changes to the
    58 module.
    59
    60 If you can run perlbug you can send me a success report with "make ok".
    61 Failure reports with "make nok" are also appreciated.
    62
    63 If you have any questions or comments, mailing list details are available in the
    64 README file, or send mail to me directly.
    65
    66 EOM
    67
    68 sub MY::postamble
    69 {
    70 qq[
    71 SET_VERSION = \$(PERL) -pi.version \\
    72 -e 's/(^\\s*(?:our\\s+)?\\\$\$VERSION = ")\\d+\\.\\d+(";)/\$\${1}$Version\$\$2/;' \\
    73 -e 's/(Version )\\d+\\.\\d+( - ).*/\$\${1}$Version\$\${2}$Date/;' \\
    74 -e 's/(^\\s*use Shell::Source(?:::\\w+)*\\s+)\\d+\\.\\d+/\$\${1}$Version/;'
    75
    76 tags : @files
    77 \t ptags @files
    78
    79 @versions : Makefile.PL
    80 \t \$(SET_VERSION) @versions
    81
    82 README : Source.pm
    83 \t TERMCAP= COLUMNS=80 pod2text Source.pm | \\
    84 \$(PERL) -n \\
    85 -e 'print if (/^NAME/ ... /^[A-Z ]+\$\$/) =~ /^\\d+\$\$/;' \\
    86 -e 'print if (/^DESCRIPTION/ ... /^[A-Z ]+\$\$/) =~ /^\\d+\$\$/;' \\
    87 > README
    88
    89 ppm : ppd pure_all
    90 \t tar cf Shell-Source.tar blib
    91 \t gzip --best --force Shell-Source.tar
    92 \t \$(PERL) -pi.bak \\
    93 -e 's/(OS NAME=")[^"]*/\$\$1MSWin32/;' \\
    94 -e 's/(ARCHITECTURE NAME=")[^"]*/\$\$1MSWin32-x86-multi-thread/;' \\
    95 -e 's/(CODEBASE HREF=")[^"]*/\$\$1Shell-Source.tar.gz/;' \\
    96 Shell-Source.ppd
    97
    98 ok :
    99 \t \@$Perlbug -okay || echo "Please send your report manually to $Author"
    100
    101 nok :
    102 \t \@$Perlbug -nokay || echo "Please send your report manually to $Author"
    103 ]
    104 }
  • trunk/MANIFEST

     
    1 MANIFEST
    2 README
    3 CHANGES
    4 TODO
    5 BUGS
    6 Makefile.PL
    7 Source.pm
    8 t/Basic.pm
    9 t/t.csh
    10 t/t.sh
    11 t/csh.t
    12 t/tcsh.t
    13 t/sh.t
    14 t/ksh.t
    15 t/zsh.t
    16 t/bash.t
  • trunk/README

     
    1 NAME
    2 Shell::Source - run programs and inherit environment changes
    3
    4 DESCRIPTION
    5 The Shell::Source allows arbitrary shell scripts, or other programs for
    6 that matter, to be run and their environment to be inherited into a Perl
    7 program.
    8
    9 Begin by creating a Shell::Source object, and specifying the shell it
    10 will use.
    11
    12 If the shell is unknown to the module, you will also need to specify how
    13 to run the shell in such a way that the output is a series of lines of
    14 the form NAME=value. For example, to run a csh script:
    15
    16 my $csh = Shell::Source->new(shell => "csh",
    17 file => "stuff.csh",
    18 run => "csh -f -c 'source [[file]]; env' |");
    19
    20 However, for known shells this is not required. Note that [[file]] will
    21 be replaced with the filename of the program you want to run.
    22
    23 Output from running the program is returned from $csh->output.
    24
    25 Changes made to the environment by running the program may be inherited
    26 by calling $csh->inherit.
    27
    28 The environment changes are available as a hash from $csh->env, or in
    29 Bourne shell syntax from $csh->shell.
    30
  • trunk/Source.pm

     
    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 use strict;
    9
    10 require 5.004;
    11
    12 package Shell::Source;
    13
    14 use vars qw($VERSION);
    15
    16 $VERSION = "0.01";
    17
    18 use Carp;
    19 use FileHandle;
    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 my $class = shift;
    34 my $self = { @_ };
    35 croak "Must specify type of shell" unless $self->{shell};
    36 $self->{run} ||= $shells->{$self->{shell}};
    37 croak "Must specify how to run unknown shell $self->{shell}"
    38 unless $self->{run};
    39 push @{$self->{ignore}}, qw( TIMEFMT PWD _ );
    40 bless $self, $class;
    41 $self->run if length $self->{file};
    42 $self
    43 }
    44
    45 sub run
    46 {
    47 my $self = shift;
    48 my $file = shift || $self->{file};
    49 croak "Must specify file to source" unless length $self->{file};
    50 (my $run = $self->{run}) =~ s/\[\[file\]\]/$self->{file}/g;
    51 my $fh = $self->{fh}
    52 = FileHandle->new($run) or croak "Can't run $self->{shell}";
    53 $self->_parse;
    54 $fh->close or croak "Can't close $self->{shell}";
    55 $self
    56 }
    57
    58 sub _parse
    59 {
    60 my $self = shift;
    61 my $fh = $self->{fh}; # FileHandle ready for reading
    62 my $env = 0; # for control of multi-line variables
    63 while (defined(my $line = <$fh>))
    64 {
    65 if ($line =~ /^(\w+)=(.*)$/)
    66 {
    67 $env = 1;
    68 if ((!defined $ENV{$1} || $ENV{$1} ne $2) &&
    69 !grep {$1 eq $_} @{$self->{ignore}})
    70 {
    71 $self->{env}{$1} = $2;
    72 }
    73 }
    74 else
    75 {
    76 push (@{$self->{output}}, $line) unless $env;
    77 }
    78 }
    79 $self
    80 }
    81
    82 sub inherit
    83 {
    84 my $self = shift;
    85 while (my ($key, $val) = each (%{$self->{env}}))
    86 {
    87 $ENV{$key} = $val;
    88 }
    89 }
    90
    91 sub shell
    92 {
    93 my $self = shift;
    94 my $shell = "";
    95 while (my ($key, $val) = each (%{$self->{env}}))
    96 {
    97 $shell .= qq($key="$val"; export $key\n);
    98 }
    99 $shell
    100 }
    101
    102 sub output
    103 {
    104 my $self = shift;
    105 join("\n", @{$self->{output}}) if defined $self->{output}
    106 }
    107
    108 sub env
    109 {
    110 my $self = shift;
    111 $self->{env}
    112 }
    113
    114 1;
    115
    116 __END__
    117
    118 =head1 NAME
    119
    120 Shell::Source - run programs and inherit environment changes
    121
    122 =head1 SYNOPSIS
    123
    124 use Shell::Source;
    125 my $csh = Shell::Source->new(shell => "csh", file => "stuff.csh");
    126 $csh->inherit;
    127 print STDERR $csh->output;
    128 print $csh->shell;
    129
    130 =head1 DESCRIPTION
    131
    132 The Shell::Source allows arbitrary shell scripts, or other programs for
    133 that matter, to be run and their environment to be inherited into a Perl
    134 program.
    135
    136 Begin by creating a Shell::Source object, and specifying the shell it
    137 will use.
    138
    139 If the shell is unknown to the module, you will also need to specify how
    140 to run the shell in such a way that the output is a series of lines of
    141 the form NAME=value. For example, to run a csh script:
    142
    143 my $csh = Shell::Source->new(shell => "csh",
    144 file => "stuff.csh",
    145 run => "csh -f -c 'source [[file]]; env' |");
    146
    147 However, for known shells this is not required. Note that [[file]] will
    148 be replaced with the filename of the program you want to run.
    149
    150 Output from running the program is returned from $csh->output.
    151
    152 Changes made to the environment by running the program may be inherited
    153 by calling $csh->inherit.
    154
    155 The environment changes are available as a hash from $csh->env, or in
    156 Bourne shell syntax from $csh->shell.
    157
    158 =head1 BUGS
    159
    160 Huh?
    161
    162 =head1 VERSION
    163
    164 Version 0.01 - 2nd August 2001
    165
    166 =head1 HISTORY
    167
    168 Created - Wednesday 26th November 1997 09:29:31 pm
    169
    170 =head1 LICENCE
    171
    172 Copyright 1997-2001, Paul Johnson (pjcj@cpan.org)
    173
    174 This software is free. It is licensed under the same terms as Perl itself.
    175
    176 The latest version of this software should be available from my homepage:
    177 http://www.pjcj.net
    178
    179 =cut
  • trunk/t/bash.t

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 use strict;
    11
    12 use lib -d "t" ? "t" : "..";
    13
    14 use Basic "bash", "t.sh";
  • trunk/t/Basic.pm

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 # Version 0.01 - 2nd August 2001
    11
    12 use strict;
    13
    14 require 5.004;
    15
    16 package Basic;
    17
    18 use vars qw($VERSION);
    19 $VERSION = "0.01";
    20
    21 use Config;
    22 use Test;
    23
    24 use Shell::Source 0.01;
    25
    26 sub import
    27 {
    28 my $class = shift;
    29 my ($shell, $script) = @_;
    30
    31 my $available;
    32 for (split /$Config{path_sep}/, $ENV{PATH})
    33 {
    34 $available = 1, last if -x "$_/$shell";
    35 }
    36
    37 unless ($available)
    38 {
    39 print "1..0 #Skipped: $shell unavailable\n";
    40 return;
    41 }
    42
    43 $script = "t/$script" if -d "t";
    44
    45 plan tests => 4;
    46
    47 my $sh = Shell::Source->new(shell => $shell, file => $script);
    48 ok $sh;
    49
    50 $sh->inherit;
    51 ok $ENV{qaz}, "qazaq";
    52 ok $sh->output, "qwerty\n";
    53 ok $sh->shell, '/qaz="qazaq"/';
    54 }
  • trunk/t/csh.t

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 use strict;
    11
    12 use lib -d "t" ? "t" : "..";
    13
    14 use Basic "csh", "t.csh";
  • trunk/t/ksh.t

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 use strict;
    11
    12 use lib -d "t" ? "t" : "..";
    13
    14 use Basic "ksh", "t.sh";
  • trunk/t/sh.t

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 use strict;
    11
    12 use lib -d "t" ? "t" : "..";
    13
    14 use Basic "sh", "t.sh";
  • trunk/t/t.csh

     
    1 setenv qaz qazaq
    2 echo qwerty
  • trunk/t/t.sh

     
    1 qaz=qazaq
    2 export qaz
    3 echo qwerty
  • trunk/t/tcsh.t

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 use strict;
    11
    12 use lib -d "t" ? "t" : "..";
    13
    14 use Basic "tcsh", "t.csh";
  • trunk/t/zsh.t

     
    1 #!/usr/local/bin/perl -w
    2
    3 # Copyright 2001, Paul Johnson (pjcj@cpan.org)
    4
    5 # This software is free. It is licensed under the same terms as Perl itself.
    6
    7 # The latest version of this software should be available from my homepage:
    8 # http://www.pjcj.net
    9
    10 use strict;
    11
    12 use lib -d "t" ? "t" : "..";
    13
    14 use Basic "zsh", "t.sh";
  • trunk/TODO

     
    1 - Add other shells.
    2 - Tests.
    3 - Documentation.