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

In reply to LibXML and %ENV in 5.18 on Windows by gweekly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.