Christopher Grau wrote:
Hi,

I have a fairly simple problem, but haven't had much luck finding the
solution on Google or the mailing list archives. Basically, I have this
code in httpd.conf:

<Perl>
print "Enter some value you don't want written down: ";
my $value = <STDIN>;
chomp $value;

print "value = '$value'\n";

# The code works if I do this...
# $value = "Foobar";

push @PerlSetVar, ["Foo" => $value];
</Perl>

The value in $value prints to stdout, but the variable "Foo" doesn't get
set to $value. However, if I were to set $value to something
explicitly, as in the comment above, the code works fine.

Based on that, I'm guessing the problem lies with reading from <STDIN>. What am I missing here?
This one was tricky. The reason for this misterious behavior lies in fact that Apache restarts itself on start. The <STDIN> input is prompted once and then on the restart $value gets overwritten with undef. If you check your error_log you will see several warnings and errors regarding this code. among which:

[Sun Nov 24 19:45:40 2002] [error] (25)Inappropriate ioctl for device: <Perl>: PerlSetVar takes two arguments, Perl config var and value

First of all, it's easier to debug with:

<Perl>
use Apache::PerlSections();
$Apache::Server::SaveConfig = 1;
# set @Perl* here
print STDERR Apache::PerlSections->dump();
</Perl>

notice the dump(). Remember that on start STDERR goes to the console. On the restart it goes to error_log, therefore you want to watch both, as it will be called twice.

using the dump() you will see that on restart no value is passed.

Here are two possible solution that work:

print "Enter some value you don't want written down: ";
$mytmp::value ||= <STDIN>;
chomp $mytmp::value;
print "value = '$mytmp::value'\n";
push @PerlSetVar, ["Foo" => $mytmp::value];

here, we use a global variable, so it survives the scope re-entrance. If <STDIN> may return 0, you want to do:
$mytmp::value = <STDIN> unless defined $mytmp::value;
instead

Another longer but more self-documenting solution is as follows:

if ($Apache::Server::Starting) {
print "Enter some value you don't want written down: ";
$mytmp::value = <STDIN>;
chomp $mytmp::value;
} else {
print "value = '$mytmp::value'\n";
push @PerlSetVar, ["Foo" => $mytmp::value];
}

See also:
http://perl.apache.org/docs/1.0/guide/config.html#Apache_Restarts_Twice_On_Start

p.s. for now let's keep this trickery in the archives, if the question is repeated, will add the answer to the docs.

__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com



Reply via email to