views:

139

answers:

3

I want to (need to) start a sub-process from a perl script that checks certain environment variables. In one instance the environment variable needs to be there but empty.

 $ENV{"GREETING"} = "Hello World";        # Valid
 $ENV{"GREETING"} = "";                   # also valid

I can set $ENV{"GREETING"} = ""; and in that perl script $ENV{"GREETING"} is empty, but in any sub-process that environment variable is not there.

Here is some example code to demonstrate. This script, env_in.pl sets up some environment variables, ZZZ_3 is empty. It then calls env_out.pl to output the environment variables, ZZZ_3 is missing from the output.

#!/usr/bin/perl
# env_in.pl

use strict;`enter code here`
use warnings;

$ENV{ZZZ_1} = "One";
$ENV{ZZZ_2} = "Two";
$ENV{ZZZ_3} = "";
$ENV{ZZZ_4} = "Four";

my (@cmd) = ("perl", "env_out.pl");
system(@cmd) == 0 or die "system @cmd failed: $?";

Here is the env_out.pl script.

#!/usr/bin/perl

use strict;
use warnings;

print ($_," = ", $ENV{$_}, "\n") for (sort keys %ENV);

I'm using ActiveState perl version v5.8.8 on a WinXP box.

I know that this DOES work in python, but I don't have a choice about the implementation language, it has to be Perl.

+9  A: 

As far as I know, there's no such thing as an empty environment variable in Windows. Defining them to empty is the same thing as undefining them.

Your Perl scripts does indeed display a ZZZ_3 entry when run on a Unix-like system.

JB
Argh! It seems you may be right.
Pev
A: 

That seems to be some issue with your activestate perl. Testing on windows2000 + cygwin gives:

$ perl --version

This is perl, v5.10.0 built for cygwin-thread-multi-64int
(with 6 registered patches, see perl -V for more detail)

Copyright 1987-2007, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


$ perl env_in.pl | grep ZZZ
ZZZ_1 = One
ZZZ_2 = Two
ZZZ_3 =
ZZZ_4 = Four

$
hlovdal
Do Cygwin environment variables follow the same rules as the Windows ones?
JB
Possibly not it appears (which is not unreasonable since the point of cygwin is to provide a unix like environment).
hlovdal
@JB Not if he ran the script in bash. If you run the script from the plain old `cmd.exe` shell, using Cygwin `perl`, you still won't see the variable whose content was set to the empty string.
Sinan Ünür
A: 

Using both strawberry perl and cygwin perl on vista results in

ZZZ_1 = One
ZZZ_2 = Two
ZZZ_4 = Four
Nifle