Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Cannot write data struct to %ENV

by exilepanda (Pilgrim)
on Jun 11, 2015 at 09:12 UTC ( #1129992=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, Not sure if I blinded or not, I just found I can no more able to write data struct to %ENV.
package A; BEGIN { $ENV{MyExtraConfig} = { foo=>{Bar => "Blaz"}} } 1; #main use A; use Data::Dumper; print Dumper \%ENV;
But what I got is 'MyExtraConfig' => 'Hash (0x*****)' I then try print Dumper $ENV{MyExtraConfig} and now I got nothing. I even tried print keys %{$ENV{MyExtraConfig}} and still got nothing. This code can run without on my XP with perl 5.12 without problem at all, but it happen on my Win7 with perl 5.18. Any clues for where I gone wrong ?

Replies are listed 'Best First'.
Re: Cannot write data struct to %ENV
by Discipulus (Abbot) on Jun 11, 2015 at 09:25 UTC
    %ENV is meant to expose ENV to Perl. In ENV You just cannot have datastructures, only strings. Even if you can add a datastructure to the Perl %ENV hash i think is not a reliable behaviour. %ENV has also a lot of magic underlying: think about case of the keys.
    perl -MData::Dumper -e "$ENV{MyExtraConfig} = { foo=>{Bar => 'Blaz'}}; +print Dumper (\%ENV);system qq(set)"

    UPDATE: in the docs is said "As of v5.18.0, both keys and values stored in %ENV are stringified."
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Cool that you even found the relevant documentation. As I've been playing with magic recently, I think it shouldn't be too hard to make %ENV warn if you try to store a reference to it. Of course, this adds another warning, and people who use fatal warnings might break their program, but it was broken beforehand.

      Thank you very much!! Now the puzzle is solved and lesson learned. Could you elaborate more about why this is not reliable though?
        If the behaviour change between version of Perl, it is unreliable. In the ENV (of your OS) values will always be stringyfied, older Perl version had permit you to add datastructure to %ENV, but this is not logic as Env will contain only strings. So the behaviour was corrected.
        See also Environment and wikipedia too.

        L*


        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Cannot write data struct to %ENV
by Corion (Pope) on Jun 11, 2015 at 09:24 UTC

    %ENV can only store strings.

    Things that are not strings will get converted to strings ("stringified").

Re: Cannot write data struct to %ENV
by choroba (Archbishop) on Jun 11, 2015 at 09:27 UTC
    I can replicate both behaviours. 5.14.4 in cygwin keeps the structure, 5.20.0 in linux returns the stringified ref.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1129992]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2020-02-17 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (70 votes). Check out past polls.

    Notices?