Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

%ENV in a .pm versus .cgi ?

by goofball (Acolyte)
on Jun 17, 2014 at 18:06 UTC ( #1090184=perlquestion: print w/replies, xml ) Need Help??
goofball has asked for the wisdom of the Perl Monks concerning the following question:

I have a new server and moved a module library there which was working fine on another server. The .cgi scripts "use" modules from the library, some of which depend on %ENV variables.

Problem is, on this new server the %ENV hash accessed from within the modules isn't populated with the same variables as the %ENV hash of the .cgi script that "use" them.

Any ideas as to why?

Replies are listed 'Best First'.
Re: %ENV in a .pm versus .cgi ?
by moritz (Cardinal) on Jun 17, 2014 at 18:35 UTC

    Update: I've completely replaced my answer; find the old one in readmore tags below.

    Imagine you were a scientist. You research the question whether a new web server caused some changes, or if it was the code organization (or both, or neither of these reasons).

    As a scientist, it's your duty to design and run an experiment to decide that question. And since you are a lazy scientist, you chose the simplest experiment that you can think of. Not only does that mean you have less work doing the experiment, but it also means you don't have to write so much when you submit your paper for review.

    So, which experiment could you do? A quick brainstorming:

    • Run the old code on the new webserver. If it works, the webserver is to blame.
    • Run the new code on the old webserver. If it works, the webserver is to blame.
    • Dump and inspect the contents of %ENV in the top-level CGI script on the new webserver (not in the module
    • Ask on perlmonks

    It really depends on your setup if the first two possible experiments are simple, but the third one certainly is.

    The fourth has the problem that perlmonks doesn't know your problem too well, and even if they solve the problem for you, you can't submit your paper in the end.

    Want to debug your code? Think like a scientist. Start now!

Re: %ENV in a .pm versus .cgi ?
by kennethk (Abbot) on Jun 17, 2014 at 18:20 UTC
    The question is a bit under-constrained since we have no code to review, but a few possibilities:
    • Are you testing the module and script in the same environment? If you run the script command-line, and run the module under Apache context, %ENV will and should vary.

    • Your code may be localizing some values of %ENV. This is actually a good security practice, since it's a potential exploit channel. See perlsec.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: %ENV in a .pm versus .cgi ?
by perlfan (Curate) on Jun 17, 2014 at 20:27 UTC
    Is it possible that %ENV was getting pulled from different sources, but were in fact just happened to be identical on the old server?

    One situation in which I can think that %ENV would be different in this case is if %ENV is being modified globally unbeknownst to you. Is it being changed in one of your modules?

    Another situation in which %ENV might be different (and in this case not cause side effects) is if you shell-out at any point in the CGI script or in one of your modules. In this case, anything executed as a separate shell process might be subject to a fully different %ENV (because it's a separate child process).

    As others have said, without precious information all we can do is speculate.

Re: %ENV in a .pm versus .cgi ?
by goofball (Acolyte) on Jun 23, 2014 at 14:32 UTC

    Thanks for the great ideas, all!

    So after following many a rabbit trail, it turns out the reason %ENV was different was that the cgi script was being called by a PHP exec() command! There was apparently some PHP setup on the old server that allowed the exec() to pass the environment onto the CGI script, but since I compiled everything from source on the new box, that setup was no longer available. (It worked on the old server, but probably wasn't all that secure)

    If anyone gives half a **** about PHP (which I'm not sure I do), the solution was to pass the needed ENV variables in the exec command before the cgi script:

    <?php exec(DOCUMENT_ROOT=/whatever session.cgi, $output); ?>

    Now that's done, I'm back to Perl like any respectable coder ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1090184]
Approved by Corion
[Spenser]: I installed 5.22 with opkg. I can use it with cpan. I'm actually trying to install perl-DBD-mysql, but couldn't with it. So I went back to the factory version, 5.10.
[Spenser]: So, nevermind about CPAN. I give up on the downgraded angle. I'm back to fighting to install DBD.
[shmem]: please open a SoPW. At what point is the perl-DBD-mysql installation failing?
[shmem]: do you have the mysqlclient libs on that device, for instance?
[shmem]: and btw, your original questian is an XY Problem classic ;-)
[Spenser]: yes, I have mysql client libs installed.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2018-03-17 12:48 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (224 votes). Check out past polls.