如何在 Perl 脚本之间共享 ENV 变量 [英] How to Share ENV Variables Among Perl Scripts

查看:66
本文介绍了如何在 Perl 脚本之间共享 ENV 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two Perl scripts along with GIT hook script.In there i am validating the GIT work flow.Here is the scripts calling stack.

pre-push -> unpush-changes -> dependency-tree

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

There is a for loop in unpush-changes perl script that will call the dependency-tree perl script.

预推

system("unpushed-changes");
  my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// '';
  if($errorMsg eq "true"){
     print "Error occured!";
   }

unpush-changes.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;
     }

   }

dependency-tree.pl

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

在我的 dependency-tree 脚本中,如果发生错误,我已经设置了 ENV 变量,这将在 unpush-changes<中的每次迭代中检查/strong> 脚本.但它的 ENV 值为空而不是 true.如果失败,我也尝试返回一些值并尝试验证它,但似乎它也不起作用.所以我的要求是如何在所有脚本中共享全局变量.如果有更好的方法,请告诉我.

In my dependency-tree script if error occurred i have set the ENV variable and that will be check in the each iteration in the unpush-changes script.But its ENV is value empty instead of true. I also tried to return some value if failed and try to validate it also but seems its also not working.So my requirement is that how can i share a global variable across all scripts.Please let me know if there is a better approach.

推荐答案

通常,子进程从其父进程继承环境的单独副本,并且子进程所做的更改不会传播到父进程的环境.Env::Modify 提供了解决此问题的方法,实施了"shell magic"perlfaq 谈到的.

In general, child processes inherit a separate copy of the environment from their parent and changes made by the child do not propagate to the parent's environment. Env::Modify offers a workaround for this issue implementing the "shell magic" that the perlfaq talks about.

典型用法:

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"
...

这篇关于如何在 Perl 脚本之间共享 ENV 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆