Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Share ENV Variables Among Perl Scripts

Tags:

perl

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

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

pre-push

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;
  }

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.

like image 939
gihan-maduranga Avatar asked Dec 18 '22 06:12

gihan-maduranga


2 Answers

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.

Typical usage:

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"
...
like image 189
mob Avatar answered Jan 01 '23 19:01

mob


As @mob mentioned there are two ways to achieve this.Env::Modify or as a perl lib.So i have chosen lib over Env::Modify.because i want to run this script in every machine either explicitly Env::Modify package is installed or not.

I have written Utils.pm bundling both unpush-changes and dependency-tree functionalities and i saved it under /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;

Then i used previously created library in my pre-push hook.

pre-push

#!/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();

No longer needs to worry about the ENV variables.Reference

like image 40
gihan-maduranga Avatar answered Jan 01 '23 19:01

gihan-maduranga