How to export a shell variable within a Perl script?

前端 未结 6 644
天涯浪人
天涯浪人 2020-12-19 10:37

I have a shell script, with a list of shell variables, which is executed before entering a programming environment.

I want to use a Perl script to enter the programm

6条回答
  •  悲哀的现实
    2020-12-19 11:25

    But yes! It may be!

    #!/usr/bin/perl -w
    
    use strict;
    
    my $perldumpenv='perl -MData::Dumper -e '."'".
        '\$Data::Dumper::Terse=1;print Dumper(\%ENV);'."'";
    
    eval '%ENV=('.$1.')' if `bash -c "
            source environment_defaults.sh >/dev/null;
            $perldumpenv"`
        =~ /^\s*\{(.*)\}\s*$/mxs;
    
    system("obe");
    

    A little overkill as this runs a fork to a Perl-only-to-dump environment, but running Perl bind environment in a safe manner and Data::Dumper is the library to use for sending/storing and retrieving Perl variables.

    Nota: This don't care about security considerations: you have to trust environment_defaults.sh at same level as the main Perl script!!!

提交回复
热议问题