English 中文(简体)
How can I learn DOCUMENT_ROOT in startup.pl under mod_perl2?
原标题:

I want to learn DOCUMENT_ROOT in startup.pl, but the best I can do is to learn server_root:

use Apache2::ServerUtil ();
$server_root = Apache2::ServerUtil::server_root();

which is quite useless. I can set an environment variable with

SetPerlEnv DOCUMENT_ROOT /path/to/www

but I don t like extra configuration if possible.

Is there a way to get DOCUMENT_ROOT by other means?

最佳回答

See Apache2::Directive. For example, on my development system:

use Apache2::Directive ();
my $tree = Apache2::Directive::conftree();
my $vhost = $tree->lookup(VirtualHost =>  unur.localdomain:8080 );

File::Slurp::write_file "C:/bzzzt.txt", [ $vhost->{DocumentRoot}, "
" ];

created a file C:/bzzzt.txt with the contents "E:/srv/unur/deploy/htdocs" after I discovered that I had to specify my virtual hosts using

<VirtualHost unur.localdomain:8080>
...
</VirtualHost>

<VirtualHost qtau.localdomain:8080>
...
</VirtualHost>

rather than <VirtualHost *:8080>. Otherwise, each <VirtualHost *:8080> section was overwriting the previous one.

This is annoying. I would have thought each VirtualHost entry would have been keyed by the ServerName used.

As for if there is an easier way, I am afraid there isn t if you want to do this in startup.pl. However, I am not sure if it is necessary to do it in startup.pl. You can find out the document root while processing a request as well using Apache2::RequestUtil::document_root.

If you are running Registry scripts, and want to change to DOCUMENT_ROOT, then you should be able to add:

chdir $ENV{DOCUMENT_ROOT} 
    or die "Cannot chdir to  $ENV{DOCUMENT_ROOT} : $!";

to the script instead of having to mess around with startup.pl and handlers etc.

问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签