develooper Front page | perl.perl5.porters | Postings from December 2012

Undocumented %ENV behaviour.

Thread Next
From:
Kent Fredric
Date:
December 15, 2012 03:10
Subject:
Undocumented %ENV behaviour.
Message ID:
CAATnKFCPuWj1P5O3r=LP==_AOnHgKXAVqh5Pco8vybxLHDL0Sw@mail.gmail.com
While trying to track down a bug on Dancer w/ 5.17.6, I managed to find a
quirk in %ENV thats not documented where I'd expect it to be ( perldoc
perlvar beside %ENV ).

The only relevant documentation I can find is :
https://metacpan.org/module/RJBS/perl-5.17.6/pod/perl5173delta.pod#Defined-values-stored-in-environment-are-forced-to-byte-strings

It says "A value stored in an environment variable has always been
stringified."

However, testing this myself with a sample perl script on 5.16.2 shows this
is not the case:

--------------------------------------------------------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

my %hash;
{
    my $content = "hello";
    open my $fh, '<', \$content;
    $ENV{'foo'} = $fh;
    $hash{'foo'} = $fh;
}


print "ENV{'foo'} is " . ( ref $ENV{'foo'} ? '' :  'not ') . "a ref\n";
print "hash{'foo'} is " . ( ref $hash{'foo'} ? '' : 'not ') . "a ref\n";

--------------------------------------------

5.16.2 , %ENV and %hash behave the same,
5.17.6,  %ENV stringifies values.

So 3 changes really need to happen here IMO,

1: Document the stringification closer to  `perldoc perlvar %ENV`
2: Make the documentation about this change in perldelta clearer ( its not
obvious that stringification is a new change )
3: Optional: Create a warnings class for inserting refs into %ENV that get
stringified.

As it is, Dancer appears to have a fair amount of code dependent on ENV
being able to store references, and it took a while to track down the bug
caused by the behaviour change.

Stringification of a value may be expected behaviour when you use a hash
key, but stringification when storing as a *value* is certainly surprising,
and really aught to be noisier.

-- 
Kent

perl -e  "print substr( \"edrgmaM  SPA NOcomil.ic\\@tfrken\", \$_ * 3, 3 )
for ( 9,8,0,7,1,6,5,4,3,2 );"

http://kent-fredric.fox.geek.nz


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About