gweekly has asked for the wisdom of the Perl Monks concerning the following question:

There seems to be a BUG in the interaction of XML::LibXML and delete $ENV{$variable}.
This seems to have appeared in 5.18.
Is this known?
Is there a fix/solution?
Have workarounds been posted?
Thank you,
-gordon


# -*- perl -*-
# badperl   19-Jan-2017 12:27 gweekly

# Run on Windows
#  for our 5.20.2:
# >...\latest520\bin\perl i:\bin\badperl
#  for 5.8.8:
# >...\stable\bin\perl i:\bin\badperl

use strict;
use warnings;
use XML::LibXML;

# Pre-condition
die "Please re-run having removed SAM from the environment\n"
    if grep { /^ SAM $ /xmsi} keys %ENV;

# Set-up
my ( $node )
    = XML::LibXML->new->parse_string(q{<xml><xyz name='SAM'/></xml>})
    ->documentElement
    ->find('child::*')
    ->get_nodelist;
my ( $attr ) = $node->attributes; # eg: name=name,value=SAM
my $name = $attr->value;

my $fixed = substr "X$name", 1; # Should be the same

# Confirm $name and $fixed appear to be as expected
print "name is '$name', fixed is '$fixed'\n";
die 'name is not SAM'  if $name ne 'SAM';
die 'fixed is not SAM' if $fixed ne 'SAM';
die 'fixed is not name' if $fixed ne $name;

delete $ENV{$name}; # Here badness happens

print defined $ENV{SAM}  ? "WRONG: ENV{SAM} is defined\n"
    :                       "OKAY: ENV{SAM} is not defined\n";
print defined $ENV{$name} ? "WRONG: ENV{\$name} is defined\n"
    :                       "OKAY: ENV{\$name} is not defined\n";
print exists $ENV{SAM}   ? "WRONG: ENV{SAM} exists\n"
    :                       "OKAY: ENV{SAM} does not exist\n";
print exists $ENV{$name}  ? "WRONG: ENV{\$name} exists\n"
    :                       "OKAY: ENV{\$name} does not exist\n";

if ( grep {$_ eq 'SAM'} keys %ENV ) {
    print "WRONG: ENV has the key 'SAM'\n";

    my $exists = exists $ENV{$fixed};
    print "Note: \$ENV{\$fixed} does " . ($exists?'':'NOT ') . "exist\n";
}

__END__

# 5.8.8
C:\Users\gweekly>...\stable\bin\perl i:\bin\badperl
name is 'SAM', fixed is 'SAM'
OKAY: ENV{SAM} is not defined
OKAY: ENV{$name} is not defined
OKAY: ENV{SAM} does not exist
OKAY: ENV{$name} does not exist

C:\Users\gweekly>c:\ActivePerl-5.14.2\bin\perl i:\bin\badperl
name is 'SAM', fixed is 'SAM'
OKAY: ENV{SAM} is not defined
OKAY: ENV{$name} is not defined
OKAY: ENV{SAM} does not exist
OKAY: ENV{$name} does not exist

C:\Users\gweekly>c:\ActivePerl5.16.3\bin\perl i:\bin\badperl
name is 'SAM', fixed is 'SAM'
OKAY: ENV{SAM} is not defined
OKAY: ENV{$name} is not defined
OKAY: ENV{SAM} does not exist
OKAY: ENV{$name} does not exist

C:\Users\gweekly>c:\ActivePerl5.18.1\bin\perl i:\bin\badperl
name is 'SAM', fixed is 'SAM'
OKAY: ENV{SAM} is not defined
OKAY: ENV{$name} is not defined
OKAY: ENV{SAM} does not exist
WRONG: ENV{$name} exists
WRONG: ENV has the key 'SAM'
Note: $ENV{$fixed} does NOT exist

C:\Users\gweekly>...\latest520\bin\perl i:\bin\badperl
name is 'SAM', fixed is 'SAM'
OKAY: ENV{SAM} is not defined
OKAY: ENV{$name} is not defined
OKAY: ENV{SAM} does not exist
WRONG: ENV{$name} exists
WRONG: ENV has the key 'SAM'
Note: $ENV{$fixed} does NOT exist

# Active-v5.24.1
C:\Users\gweekly>perl i:\bin\badperl
name is 'SAM', fixed is 'SAM'
OKAY: ENV{SAM} is not defined
OKAY: ENV{$name} is not defined
OKAY: ENV{SAM} does not exist
WRONG: ENV{$name} exists
WRONG: ENV has the key 'SAM'
Note: $ENV{$fixed} does NOT exist

Replies are listed 'Best First'.
Re: LibXML and %ENV in 5.18 on Windows
by poj (Abbot) on Jan 20, 2017 at 20:50 UTC

    I'm guessing this maybe unicode related, try adding

    my $name = $attr->getValue(); use Devel::Peek; Dump($name); $name = Encode::encode( 'latin1', $name ); Dump($name);

    or just

    my $name = $attr->getValue(); $name = substr($name,0);
    poj
      It is interesting to note that a Devel::Peek::Dump done in 5.16 also shows UTF8, but 5.16 does not have the problem that 5.18 does.
      C:\Users\gweekly>c:\ActivePerl5.16.3\bin\perl -d i:\bin\badperl
      
      Loading DB routines from perl5db.pl version 1.37
      Editor support available.
      
      Enter h or 'h h' for help, or 'perldoc perldebug' for more help.
      
      main::(i:\bin\badperl:16):          if grep { /^ SAM $ /xmsi} keys %ENV;
      
        DB<1> c 27
      main::(i:\bin\badperl:27):      my $fixed = substr "X$name", 1; # Should be the
      same
      
        DB<2> use Devel::Peek
      
        DB<3> Dump $name
      SV = PVNV(0x2cc711c) at 0x2a27314
        REFCNT = 2
        FLAGS = (PADMY,POK,pPOK,UTF8)
        IV = 0
        NV = 0
        PV = 0x2d6614c "SAM"\0 UTF8 "SAM"
        CUR = 3
        LEN = 12
      
        DB<4> q
      
      C:\Users\gweekly>c:\ActivePerl5.18.1\bin\perl -d i:\bin\badperl
      
      Loading DB routines from perl5db.pl version 1.39_10
      Editor support available.
      
      Enter h or 'h h' for help, or 'perldoc perldebug' for more help.
      
      main::(i:\bin\badperl:16):          if grep { /^ SAM $ /xmsi} keys %ENV;
      
        DB<1> c 27
      main::(i:\bin\badperl:27):      my $fixed = substr "X$name", 1; # Should be the
      same
      
        DB<2> use Devel::Peek
      
        DB<3> Dump $name
      SV = PVMG(0x2f14534) at 0x2bfd924
        REFCNT = 2
        FLAGS = (PADMY,POK,pPOK,UTF8)
        IV = 0
        NV = 0
        PV = 0x2f730d4 "SAM"\0 UTF8 "SAM"
        CUR = 3
        LEN = 12
      
        DB<4>
      
      This is very helpful. Thank you! Any idea if this has been (or should be) reported as a BUG?
Re: LibXML and %ENV in 5.18 on Windows
by gweekly (Novice) on Jan 20, 2017 at 17:36 UTC
    Note: This has been reproduced in both Strawberry Perl and our own build - it is not just in ActiveState.
Re: LibXML and %ENV in 5.18 on Windows
by stevieb (Canon) on Jan 20, 2017 at 17:48 UTC

    I'm pretty sure that this Windows, not Perl, but I could be wrong.

    In Windows, one has to close down the current command window and open a new one before environment variables are properly updated (unless you go through other hackery, but it's still not perfect).

    After you delete the env var, open another command window and see if it still exists there.

      But different versions of perl DO and DON'T exhibit this bad behavior. All in the same command window. Repeatably.

        My apologies. I overread the examples you provided.

Re: LibXML and %ENV in 5.18 on Windows
by gweekly (Novice) on Jan 25, 2017 at 22:08 UTC
    This problem has nothing to do with XML parsing.
    I've posted Windows %ENV and utf8 as a clarification with clearer text and code exposing the problem.