Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Win32 backticks fail when too many ENV vars

by jimbojones (Friar)
on Dec 07, 2006 at 20:55 UTC ( #588465=perlquestion: print w/replies, xml ) Need Help??

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


I need help with executing a child process on windows. I've found that if the number of ENV variables are too big, Perl will not create the child cmd.exe process.

use warnings; use strict; my $max = 69; #-- tweak this to get it to fail foreach my $i ( 1 .. $max ) { my $t = "ENV$i"; my $m = max( $i, 20); $ENV{$t} = "big string" x $m; } my @env = keys %ENV; print "# of ENV: ", scalar keys %ENV, "\n"; #-- calculate the lenght of the env list my $env_str = ''; foreach my $key ( keys %ENV ) { $env_str .= "$key=" . $ENV{$key} . " "; #-- space represents \0 inter +nal in pointer } print "Length ENV str: ", (length( $env_str) + 1), "\n"; print `set`; print "RC: ", $?, "\n"; #------------------------------------------------------------------ sub max { my ( $i, $j ) = @_; if ( $i > $j ) { return $i; } return $j; }
You may have to tweak $max to get it to fail depending on how many ENV variables are predefined on your system.

It fails like this:

# of ENV: 127 Length ENV str: 30798 RC: -1
.. but I think it's the length of the string, not the number of ENV variables, that affects whether it fails. E.g, change the max on the "big string" to 40 from 20, and it fails at 118 env variables and a length of 30785.

I looked in the source code and it indicates that the cmd process is created with CreateProcess that passes in (LPVOID)env,    /* inherit environment */. I cannot find any limit on the env string on the Microsoft site. Is there a magic number limit internal to perl?

Thanks in advance.

- j

Replies are listed 'Best First'.
Re: Win32 backticks fail when too many ENV vars
by Corion (Patriarch) on Dec 07, 2006 at 20:58 UTC
      Thanks for the update

      The following MS documentation must be wrong: it lists the max sizes as:
      The maximum individual environment variable size is 8192bytes.

      The maximum total environment variable size for all variables, which includes variable names and the equal sign, is 65,536KB

      I thought I was far away from that. Now it's clear that it's a typo.

      Regards, J

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://588465]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2022-01-20 06:26 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (56 votes). Check out past polls.