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

Maybe I'm touching some deep voodoo here, but how does one replace the version of %ENV that is used for child processes ? It seems like %ENV is just an ordinary hash, only with 'magic' scope, but the copy for forked processes isn't exactly what I expected :((

To put my question directly: how is %ENV copied by child processes ?

Some evil test code:
#!/usr/bin/perl package Env::Tie; use strict; use Tie::Hash; our @ISA = qw/Tie::StdHash/; sub TIEHASH { bless $_[1] , $_[0] } sub FETCH { $_[0]->{$_[1]} . '---BWUBUBWU' } # The '---BWUBUBWU' of course being # dynamic generated content package Test; use strict; sub print { print 'env home =', $ENV{HOME}, "\n" } package main; use strict; print "Hic sunt leones\n"; print 'env home =', $ENV{HOME}, "\n"; print "lets go\n"; my %self = %ENV; tie %ENV, 'Env::Tie', \%self; print "tied ENV\n"; print 'env home =', $ENV{HOME}, "\n"; print "lets try it in an other package\n"; Test::print(); print "lets fork perl\n"; system( 'perl', '-e', q{print 'env home =', $ENV{HOME}, "\n"} ); print "lets fork bash\n"; system( 'bash', '-c', q{echo $HOME} ); print "done\n";
The behaviour I'm looking for is that the forked processes use my modified ENV.

--
Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
>>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<

Replies are listed 'Best First'.
•Re: How is %ENV copied by child processes ?
by merlyn (Sage) on Apr 21, 2003 at 17:58 UTC
      I thought of that option but I wanted to it make a module that could be used transparently. Considering the number of ways a perl program can be forked this won't be do-able :((
      At least not without custom fork/system/.. routines.

      Well back to use Env; for me.
      --
      Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
      >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<
Re: How is %ENV copied by child processes ? (untie)
by tye (Sage) on Apr 21, 2003 at 20:36 UTC

    You have to make your tied %ENV update the underlying %ENV which invokes the "magic" which calls putenv which updates the "real" environment of your process (which is what will be copied to the children).

    I don't see any use for appending '---BWUBUBWU' to the end of each environment value so I'll assume this is just a silly example. If you want to tell us what you are really trying to do, then someone will likely show you how to write code that actually does that.

                    - tye
      Well, one example would be incorporating the funtionality of the Env module for arrays, but then on an other level.

      An other example would be putting code refs in @PATH which do dynamical lookups. Allowing for the same kind of hooks as supported for @INC and %INC.

      But since I understand this is not a 'perl space' operation but lives in an other dimension of the OS, I just do the same stuff with some quick hacks.
      --
      Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
      >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<