How to Share ENV Variables Among Perl Scripts

后端 未结 2 1048
借酒劲吻你
借酒劲吻你 2020-12-21 04:15

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

相关标签:
2条回答
  • 2020-12-21 04:22

    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

    0 讨论(0)
  • 2020-12-21 04:35

    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"
    ...
    
    0 讨论(0)
提交回复
热议问题