You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Jean-Sebastien Guay <je...@hybride.com> on 2003/08/01 21:46:20 UTC

Current directory

Hello again,

I have another problem trying to get one of my Perl modules to load in my startup.pl script for the first time. In a couple places in my scripts, I assume that the current directory is the one in which the current script is being run. So for example, if my DocumentRoot is D:/htdocs/ and someone requests http://myhostname/script.cgi, if I need to use some files, my current directory is D:/htdocs.

The two things I currently do this for are
a) configuration file, which is loaded on startup by the module I am trying to get loaded in my startup.pl script
b) templates (for template-toolkit), which I specify to be in the directory 'templates' relative to the location where the scripts are running, meaning they are in D:/htdocs/templates.

I see only disadvantages to having to specify absolute paths in both these cases. For one, I have another web server running on port 8080, which I use to test my scripts on, and whose DocumentRoot is D:/htdocs-dev. So if I had to manually change the paths each time I copied files over from the development DocumentRoot to the production one, I would go crazy.

Is there a way to guarantee that the current directory will be the correct one when I need it to? Or does anyone have another suggestion? 

Thanks!

J-S

_______________________________________________
Jean-Sébastien Guay                  jean_seb@hybride.com
Software Developer, Hybride         http://www.hybride.com
Piedmont, Québec, Canada

Re: Current directory

Posted by Jean-Sebastien Guay <je...@hybride.com>.
> On Mon, 2003-08-04 at 12:08, Jean-Sebastien Guay wrote:
> > Doesn't all this require that I actually get a running Apache server
> > first? The error message shows up when I try to start up the Apache
> > service!
>
> What?  That shouldn't happen unless something is calling CGI->new in
> your startup, which is a bad thing to do.  Maybe some script you're
> loading is doing that.  Try to figure out what's doing it.

You're absolutely right... Shame on me. The first homegrown module I was
importing did exactly that. It would try to get the user cookie right on
use(), which of course isn't very good practice. So I've modified it to get
it on first call of a certain method, and to return that same value on
subsequent calls.

Geez...

Now I have to transition all my scripts and other modules to mod_perl. Where
could I find examples of real-life conversion between normal CGI and
mod_perl? (Other than the perl.apache.org docs, which even though they're
great, lack real examples of what needs to be done for the really ugly types
of code...)

Thanks for bearing with me through this...

J-S

_______________________________________________
Jean-Sébastien Guay                  jean_seb@hybride.com
Software Developer, Hybride         http://www.hybride.com
Piedmont, Québec, Canada



Re: Current directory

Posted by Perrin Harkins <pe...@elem.com>.
On Mon, 2003-08-04 at 12:08, Jean-Sebastien Guay wrote:
> Doesn't all this require that I actually get a running Apache server
> first? The error message shows up when I try to start up the Apache
> service!

What?  That shouldn't happen unless something is calling CGI->new in
your startup, which is a bad thing to do.  Maybe some script you're
loading is doing that.  Try to figure out what's doing it.

>  As I understand it, the _startup.pl script tries to require() CGI.pm,
> which gives the error message.

What happens if you comment out CGI.pm from your startup.pl?

- Perrin

Re: Current directory

Posted by Jean-Sebastien Guay <je...@hybride.com>.
Perrin,

> First, check your conf file to be sure you have this turned on.  There
> are docs related to it here:
> http://perl.apache.org/docs/2.0/user/config/config.html#C_GlobalRequest_

The docs also state that unless it's explicitly turned _off_, it's on by default. But I turned it on anyways before I sent my last message. Here's the relevant section of my httpd.conf:

________________ begin ________________

LoadModule perl_module modules/mod_perl.so
LoadFile "D:/Perl/bin/perl58.dll"

PerlModule Apache2
PerlSetEnv SCRIPT_ROOT "D:/htdocs"
PerlRequire "D:/htdocs/_startup.pl"

  <Files ~ "\.cgi$">
     SetHandler perl-script
     PerlResponseHandler ModPerl::Registry
     Options +ExecCGI
     PerlOptions +ParseHeaders +GlobalRequest
  </Files>
________________ end ________________

And here's my _startup.pl script:

________________ begin ________________

#-------------------------------------------------------------------------------
# Default module inclusions as per the Apache
# mod_perl installation instructions
use Apache2 ();
use ModPerl::Util ();
use Apache::RequestRec ();
use Apache::RequestIO ();
use Apache::RequestUtil ();
use Apache::Server ();
use Apache::ServerUtil ();
use Apache::Connection ();
use Apache::Log ();
use Apache::Const -compile => ':common';
use APR::Const -compile => ':common';
use APR::Table ();
use Apache::compat ();
use ModPerl::Registry ();
use CGI ();

#-------------------------------------------------------------------------------
# General modules
use Time::localtime;
use Data::Dumper;
use Date::Calc qw(:all);

#-------------------------------------------------------------------------------
# Hybride modules

# Add the top-level directory for the modules into the module search path.
use lib qw(D:/htdocs);
# ... ... The modules I want to pre-load are currently all commented out, until  
# ... ... I get a running server to test them one at a time...

________________ end ________________

> You can test it by writing a little handler (or Registry script) that
> calls Apache->request().  That should return an Apache::RequestRec
> object if all is well.  If it doesn't, you may have an old mod_perl 2
> build or you may have found a bug.
> 
> If you can't get that to work, there is also the direct approach which
> is to pass a RequestRec object to CGI.pm when you call the new()
> method.  Registry scripts get a RequestRec object passed to them as
> their first param, so you can just shift it into a variable (usually
> called $r) and pass it as CGI->new($r).

Doesn't all this require that I actually get a running Apache server first? The error message shows up when I try to start up the Apache service! As I understand it, the _startup.pl script tries to require() CGI.pm, which gives the error message. So I never get a running Apache instance to run any scripts in.

If there's anything else I can try, or if you see anything in the above config files, please let me know. Thanks for your patience on this...

J-S

_______________________________________________
Jean-Sébastien Guay                  jean_seb@hybride.com
Software Developer, Hybride         http://www.hybride.com
Piedmont, Québec, Canada

Re: Current directory

Posted by Perrin Harkins <pe...@elem.com>.
On Mon, 2003-08-04 at 09:32, Jean-Sebastien Guay wrote:
> But I still get the same message...
> 
> > [Mon Aug 04 09:31:57 2003] [error] Global $r object is not available. Set:
> >         PerlOptions +GlobalRequest
> > in httpd.conf at D:/Perl/lib/CGI.pm line 307.
> > Compilation failed in require at D:/htdocs/_startup.pl line 33.
> 
> Is there anything else I can check other than the CGI.pm version?

First, check your conf file to be sure you have this turned on.  There
are docs related to it here:
http://perl.apache.org/docs/2.0/user/config/config.html#C_GlobalRequest_

You can test it by writing a little handler (or Registry script) that
calls Apache->request().  That should return an Apache::RequestRec
object if all is well.  If it doesn't, you may have an old mod_perl 2
build or you may have found a bug.

If you can't get that to work, there is also the direct approach which
is to pass a RequestRec object to CGI.pm when you call the new()
method.  Registry scripts get a RequestRec object passed to them as
their first param, so you can just shift it into a variable (usually
called $r) and pass it as CGI->new($r).

- Perrin

Re: Current directory

Posted by Jean-Sebastien Guay <je...@hybride.com>.
BTW,

> perl -MCGI -e "print $CGI::VERSION;"
2.98

J-S

----- Original Message ----- 
From: "Jean-Sebastien Guay" <je...@hybride.com>
To: "Randy Kobes" <ra...@theoryx5.uwinnipeg.ca>
Cc: <mo...@perl.apache.org>
Sent: Monday, August 04, 2003 9:32 AM
Subject: Re: Current directory


> > One way is to configure the CPAN module:
> >    C:\> perl -MCPAN -e shell
> > which, the first time you invoke it, will lead you through
> > a dialogue. You can accept most of the defaults, except
> > for the list of CPAN mirrors to use. Then, at the
> > CPAN.pm shell prompt, you can say
> >   cpan> install CGI
>
> Thanks for answering Randy, but I got a working PPM module of CGI for
HP-UX
> and modified the module's description files to make PPM think it's for
> Win32. Since it's just Perl code, and not XS, should work. If anyone wants
> this PPM, I can make it available. Though it would be more useful on a
known
> repository.
>
> But I still get the same message...
>
> > [Mon Aug 04 09:31:57 2003] [error] Global $r object is not available.
Set:
> >         PerlOptions +GlobalRequest
> > in httpd.conf at D:/Perl/lib/CGI.pm line 307.
> > Compilation failed in require at D:/htdocs/_startup.pl line 33.
>
> Is there anything else I can check other than the CGI.pm version?
>
>
>
>



Re: Current directory

Posted by Jean-Sebastien Guay <je...@hybride.com>.
> One way is to configure the CPAN module:
>    C:\> perl -MCPAN -e shell
> which, the first time you invoke it, will lead you through
> a dialogue. You can accept most of the defaults, except
> for the list of CPAN mirrors to use. Then, at the
> CPAN.pm shell prompt, you can say
>   cpan> install CGI

Thanks for answering Randy, but I got a working PPM module of CGI for HP-UX
and modified the module's description files to make PPM think it's for
Win32. Since it's just Perl code, and not XS, should work. If anyone wants
this PPM, I can make it available. Though it would be more useful on a known
repository.

But I still get the same message...

> [Mon Aug 04 09:31:57 2003] [error] Global $r object is not available. Set:
>         PerlOptions +GlobalRequest
> in httpd.conf at D:/Perl/lib/CGI.pm line 307.
> Compilation failed in require at D:/htdocs/_startup.pl line 33.

Is there anything else I can check other than the CGI.pm version?



Re: Current directory

Posted by Randy Kobes <ra...@theoryx5.uwinnipeg.ca>.

On Fri, 1 Aug 2003, Jean-Sebastien Guay wrote:

> Hi Perrin,
>
> Thanks for both your answers. Indeed, for the other question, I was using
> CGI 2.91 instead of 2.93 (because that one isn't yet available for Perl 5.8
> via PPM). I'll find a way to upgrade it.

One way is to configure the CPAN module:
   C:\> perl -MCPAN -e shell
which, the first time you invoke it, will lead you through
a dialogue. You can accept most of the defaults, except
for the list of CPAN mirrors to use. Then, at the
CPAN.pm shell prompt, you can say
  cpan> install CGI

Before doing this, you'll need Microsoft's nmake utility;
a link on where to get it appears in the Win32 configuration
page at http://perl.apache.org/.

best regards,
randy


Re: Current directory

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2003-08-01 at 16:59, Jean-Sebastien Guay wrote:
> Unfortunately, this doesn't seem to work. Even if I put the PerlSetVar
> statement before my PerlRequire statement like so:
> 
> PerlSetVar SCRIPT_ROOT "D:/htdocs"
> PerlRequire "D:/htdocs/_startup.pl"
> 
> the module, which is then loaded from _startup.pl, sees only undef when I
> try to print $ENV{SCRIPT_ROOT};

You're thinking of PerlSetEnv.  PerlSetVar values are retrieved
differently.  Take a look at this:
http://perl.apache.org/docs/1.0/guide/config.html#PerlSetEnv_and_PerlPassEnv

Note that you can also just do this:
<Perl>
  $MyConfig::SCRIPT_ROOT = 'foo';
</Perl>

And then in your module:
my $root = $MyConfig::SCRIPT_ROOT;

- Perrin

Re: Current directory

Posted by Jean-Sebastien Guay <je...@hybride.com>.
Hi Perrin,

Thanks for both your answers. Indeed, for the other question, I was using
CGI 2.91 instead of 2.93 (because that one isn't yet available for Perl 5.8
via PPM). I'll find a way to upgrade it.

> There are dozens of possible answers to this.
...
> There are also common approaches like passing some kind of application
> root either in an environment variable or in httpd.conf with a
> PerlSetVar.

Unfortunately, this doesn't seem to work. Even if I put the PerlSetVar
statement before my PerlRequire statement like so:

PerlSetVar SCRIPT_ROOT "D:/htdocs"
PerlRequire "D:/htdocs/_startup.pl"

the module, which is then loaded from _startup.pl, sees only undef when I
try to print $ENV{SCRIPT_ROOT}; ... I also tried to do this right before
use()ing my module:

$ENV{SCRIPT_ROOT} = 'D:/htdocs';

and it gives the same result. Could the problem come from the fact that
_startup.pl is trying to load the module before Apache is actually finished
loading, so the environment is not in a valid state at that point? What else
could cause this?

Thanks,

J-S

_______________________________________________
Jean-Sébastien Guay                  jean_seb@hybride.com
Software Developer, Hybride         http://www.hybride.com
Piedmont, Québec, Canada



Re: Current directory

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2003-08-01 at 15:46, Jean-Sebastien Guay wrote:
> I see only disadvantages to having to specify absolute paths in both
> these cases. For one, I have another web server running on port 8080,
> which I use to test my scripts on, and whose DocumentRoot is
> D:/htdocs-dev. So if I had to manually change the paths each time I
> copied files over from the development DocumentRoot to the production
> one, I would go crazy.

There are dozens of possible answers to this.  I typically put things
relative to the web server root, which is described here:
http://perl.apache.org/docs/1.0/api/Apache.html#Apache_E_gt_server_root_relative____relative_path___

Another approach would be to look up the directory that your script is
in and either chdir to that or pass it to your modules and have them use
it.

There are also common approaches like passing some kind of application
root either in an environment variable or in httpd.conf with a
PerlSetVar.

- Perrin