Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl脚本之间共享ENV变量_Perl - Fatal编程技术网

如何在Perl脚本之间共享ENV变量

如何在Perl脚本之间共享ENV变量,perl,Perl,我有两个Perl脚本和GIT钩子脚本。在这里我验证GIT工作流程。下面是调用堆栈的脚本 预推->取消推送更改->依赖关系树 在unpush changes perl脚本中有一个for循环,它将调用依赖关系树perl脚本 预推 system("unpushed-changes"); my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// ''; if($errorMsg eq "true"){ print "Error occured!";

我有两个Perl脚本和GIT钩子脚本。在这里我验证GIT工作流程。下面是调用堆栈的脚本

预推->取消推送更改->依赖关系树

在unpush changes perl脚本中有一个for循环,它将调用依赖关系树perl脚本

预推

system("unpushed-changes");
  my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// '';
  if($errorMsg eq "true"){
     print "Error occured!";
   }
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw(dirname);
use Cwd  qw(abs_path);
use lib dirname(dirname abs_path $0) . '/lib'; 
use My::Utils qw(build deploy); // or use lib '/c/lib';

build();
deploy();
取消推送更改。pl

  for my $i (0 .. $#uniqueEffectedProjectsList) {
      my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// '';
      if($errorMsg ne "true"){
        my $r=system("dependency-tree $uniqueEffectedProjectsList[$i]");
     }else{
        exit 1;
     }

   }
if(system("mvn clean compile  -DskipTests")==0){
     print "successfully build"; 
     return 1;
 }else{
      $ENV{'GIT_FLOW_ERROR_MSG'} = 'true';
      print "Error occured";
      return 0;
  }
依赖关系树.pl

  for my $i (0 .. $#uniqueEffectedProjectsList) {
      my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// '';
      if($errorMsg ne "true"){
        my $r=system("dependency-tree $uniqueEffectedProjectsList[$i]");
     }else{
        exit 1;
     }

   }
if(system("mvn clean compile  -DskipTests")==0){
     print "successfully build"; 
     return 1;
 }else{
      $ENV{'GIT_FLOW_ERROR_MSG'} = 'true';
      print "Error occured";
      return 0;
  }

在我的依赖关系树脚本中,如果发生错误,我已经设置了ENV变量,并且将在取消推送更改脚本中的每次迭代中检查该变量。但是它的ENV值为空,而不是。我还尝试在失败时返回一些值,并尝试验证它,但它似乎也不起作用。因此,我的要求是如何在所有脚本中共享全局变量。请告诉我是否有更好的方法

通常,子进程从其父进程继承环境的一个单独副本,并且子进程所做的更改不会传播到父进程的环境。 为这个问题提供了一个解决方案,实现perlfaq所讨论的

典型用法:

use Env::Modify 'system',':bash';

print $ENV{FOO};                   #   ""
system("export FOO=bar");
print $ENV{FOO};                   #   "bar"
...
print $ENV{GIT_FLOW_ERROR_MSG};    #   ""
system("unpushed-changes");
print $ENV{GIT_FLOW_ERROR_MSG};    #   "true"
...

正如@mob提到的,有两种方法可以实现这一点。
Env::Modify
或作为perl
lib
。因此我选择了
lib
而不是
Env::Modify
。因为我想在每台机器上运行这个脚本,无论是否安装了
Env::Modify

我编写了Utils.pm绑定了
取消推送更改
依赖关系树
功能,并将其保存在/c/lib/My/Utils.pm

Utils.pm

package My::Utils;
use strict;
use warnings;

use Exporter qw(import); 
our @EXPORT_OK = qw(build deploy);

sub build {
 system("mvn clean compile  -DskipTests")
 //Do  other things
}

sub deploy {
 //Do  things
}

1;
然后我在我的
pre-push
hook中使用先前创建的库

预推

system("unpushed-changes");
  my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// '';
  if($errorMsg eq "true"){
     print "Error occured!";
   }
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw(dirname);
use Cwd  qw(abs_path);
use lib dirname(dirname abs_path $0) . '/lib'; 
use My::Utils qw(build deploy); // or use lib '/c/lib';

build();
deploy();

不再需要担心
ENV
变量

看到了吗?现在可以用@mob来做了,我不知道这个模块。我会重新回答这个问题,这样你就可以回答了。然后,我们应该用这个问题的链接来结束另一个相关问题。如果所有的
系统
调用都只是调用其他perl脚本,那么更好的方法是将perl脚本转换为函数/库,并从单个perl进程调用所有内容。在进程之间共享环境变量是过分的。对于这个问题,这无疑是一条出路。