Shell-Config-Generate

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

       export PATH;
     fi;

    and this:

     $config->generate_file(Shell::Guess->c_shell, 'config.csh');

    will generate a config.csh with this:

     # this is my config file
     setenv FOO 'bar';
     setenv PERL5LIB '/foo/bar/lib/perl5:/foo/bar/lib/perl5/perl5/site';
     test "$?PATH" = 0 && setenv PATH '/foo/bar/bin:/bar/foo/bin' || setenv PATH "$PATH":'/foo/bar/bin:/bar/foo/bin';

    and this:

     $config->generate_file(Shell::Guess->cmd_shell, 'config.cmd');

    will generate a config.cmd (Windows cmd.exe script) with this:

     rem this is my config file
     set FOO=bar
     set PERL5LIB=/foo/bar/lib/perl5;/foo/bar/lib/perl5/perl5/site

README  view on Meta::CPAN


    The implementation for csh depends on the external command test. As far
    as I can tell test should be available on all modern flavors of UNIX
    which are using csh. If anyone can figure out how to prepend or append
    to path type environment variable without an external command in csh,
    then a patch would be appreciated.

    The incantation for prepending and appending elements to a path on csh
    probably deserve a comment here. It looks like this:

     test "$?PATH" = 0 && setenv PATH '/foo/bar/bin:/bar/foo/bin' || setenv PATH "$PATH":'/foo/bar/bin:/bar/foo/bin';

      * one line

      The command is all on one line, and doesn't use if, which is probably
      more clear and ideomatic. This for example, might make more sense:

       if ( $?PATH == 0 ) then
         setenv PATH '/foo/bar/bin:/bar/foo/bin'
       else
         setenv PATH "$PATH":'/foo/bar/bin:/bar/foo/bin'
       endif

      However, this only works if the code interpreted using the csh source
      command or is included in a csh script inline. If you try to invoke
      this code using csh eval then it will helpfully convert it to one
      line and if does not work under csh in one line.

    There are probably more clever or prettier ways to append/prepend path
    environment variables as I am not a shell programmer. Patches welcome.

lib/Shell/Config/Generate.pm  view on Meta::CPAN

      my $name = shift @$args;
      $args = [$name, join $sep, @$args];
    }

    if($command eq 'set')
    {
      my($name, $value) = @$args;
      if($shell->is_c)
      {
        $value = _value_escape_csh($value);
        $buffer .= "setenv $name '$value';\n";
      }
      elsif($shell->is_fish)
      {
        $value = _value_escape_fish($value);
        $buffer .= "set -x $name '$value';\n";
      }
      elsif($shell->is_bourne)
      {
        $value = _value_escape_sh($value);
        $buffer .= "$name='$value';\n";

lib/Shell/Config/Generate.pm  view on Meta::CPAN

        croak 'don\'t know how to "set" with ' . $shell->name;
      }
    }

    elsif($command eq 'append_path' || $command eq 'prepend_path')
    {
      my($name, @values) = @$args;
      if($shell->is_c)
      {
        my $value = join $sep, map { _value_escape_csh($_) } @values;
        $buffer .= "test \"\$?$name\" = 0 && setenv $name '$value' || ";
        if($command eq 'prepend_path')
        { $buffer .= "setenv $name '$value$sep'\"\$$name\"" }
        else
        { $buffer .= "setenv $name \"\$$name\"'$sep$value'" }
        $buffer .= ";\n";
      }
      elsif($shell->is_bourne)
      {
        my $value = join $sep, map { _value_escape_sh($_) } @values;
        $buffer .= "if [ -n \"\$$name\" ] ; then\n";
        if($command eq 'prepend_path')
        { $buffer .= "  $name='$value$sep'\$$name;\n  export $name;\n" }
        else
        { $buffer .= "  $name=\$$name'$sep$value';\n  export $name\n" }

lib/Shell/Config/Generate.pm  view on Meta::CPAN

   export PATH;
 fi;

and this:

 $config->generate_file(Shell::Guess->c_shell, 'config.csh');

will generate a config.csh with this:

 # this is my config file
 setenv FOO 'bar';
 setenv PERL5LIB '/foo/bar/lib/perl5:/foo/bar/lib/perl5/perl5/site';
 test "$?PATH" = 0 && setenv PATH '/foo/bar/bin:/bar/foo/bin' || setenv PATH "$PATH":'/foo/bar/bin:/bar/foo/bin';

and this:

 $config->generate_file(Shell::Guess->cmd_shell, 'config.cmd');

will generate a C<config.cmd> (Windows C<cmd.exe> script) with this:

 rem this is my config file
 set FOO=bar
 set PERL5LIB=/foo/bar/lib/perl5;/foo/bar/lib/perl5/perl5/site

lib/Shell/Config/Generate.pm  view on Meta::CPAN


The implementation for C<csh> depends on the external command C<test>.
As far as I can tell C<test> should be available on all modern
flavors of UNIX which are using C<csh>.  If anyone can figure out
how to prepend or append to path type environment variable without
an external command in C<csh>, then a patch would be appreciated.

The incantation for prepending and appending elements to a path
on csh probably deserve a comment here.  It looks like this:

 test "$?PATH" = 0 && setenv PATH '/foo/bar/bin:/bar/foo/bin' || setenv PATH "$PATH":'/foo/bar/bin:/bar/foo/bin';

=over 4

=item * one line

The command is all on one line, and doesn't use if, which is
probably more clear and ideomatic.  This for example, might
make more sense:

 if ( $?PATH == 0 ) then
   setenv PATH '/foo/bar/bin:/bar/foo/bin'
 else
   setenv PATH "$PATH":'/foo/bar/bin:/bar/foo/bin'
 endif

However, this only works if the code interpreted using the csh
C<source> command or is included in a csh script inline.  If you
try to invoke this code using csh C<eval> then it will helpfully
convert it to one line and if does not work under csh in one line.

=back

There are probably more clever or prettier ways to

t/lib/TestLib.pm  view on Meta::CPAN


  return $VAR1;
}

sub bad_fish
{
  my $path = shift;
  my $dir = File::Spec->catdir(tempdir( CLEANUP => 1 ), qw( one two three ));

  require IPC::Open3;
  my $pid = IPC::Open3::open3(\*IN, \*OUT, \*ERR, $path, -c => "setenv PATH \$PATH $dir");
  waitpid $pid, $?;

  my $str = do { local $/; <ERR> };

  $? != 0;
}

1;



( run in 0.528 second using v1.01-cache-2.11-cpan-283623ac599 )