What is equivalent to exporting bash to Perl?

I am converting a bash script to Perl. I am not sure what is equivalent to export .

 LOC=/tmp/1/ export LOC 

For example, for the two lines above, what would be the equivalent Perl code?

 my $LOC = '/tmp/1/'; # what should go here? 
+4
source share
3 answers
 $ENV{LOC} = "/tmp/1"; 

The %ENV content extends to the Perl script child process environment.

+8
source
+4
source

Inside bash, you can do something like this:

 EXPORT_CMD=/tmp/${0}_exports.bsh perl ... chmod +x $EXPORT_CMD $EXPORT_CMD rm $EXPORT_CMD 

Inside Perl it is:

 sub export (@) { state $exh; unless ( $exh ) { my $export_cmd_path = $ENV{EXPORT_CMD}; open( $exh, '>>', $export_cmd_path ) or die "Could not open $export_cmd_path!" ; } while ( @_ > 1 ) { my ( $name, $value ) = (( uc shift ), shift ); # If you want it visible in the current script: { no strict 'refs'; ${"::$name"} = $value; } $exh->print( qq{export $name "$value"\n} ); } } 

And then this is just a coding question:

 export LOC => '/tmp/1/'; 

The problem is that most programs cannot change the shell variables from which they were called.

0
source

All Articles