Loading ...
Sorry, an error occurred while loading the content.

Current directory

Expand Messages
  • Jean-Sebastien Guay
    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,
    Message 1 of 15 , Aug 1 12:46 PM
    • 0 Attachment
      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@...
      Software Developer, Hybride         http://www.hybride.com
      Piedmont, Québec, Canada
    • Perrin Harkins
      ... There are dozens of possible answers to this. I typically put things relative to the web server root, which is described here:
      Message 2 of 15 , Aug 1 1:32 PM
      • 0 Attachment
        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
      • Jean-Sebastien Guay
        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
        Message 3 of 15 , Aug 1 1:59 PM
        • 0 Attachment
          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@...
          Software Developer, Hybride http://www.hybride.com
          Piedmont, Québec, Canada
        • Perrin Harkins
          ... You re thinking of PerlSetEnv. PerlSetVar values are retrieved differently. Take a look at this:
          Message 4 of 15 , Aug 1 2:07 PM
          • 0 Attachment
            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
          • Randy Kobes
            ... 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
            Message 5 of 15 , Aug 1 2:11 PM
            • 0 Attachment
              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
            • Jean-Sebastien Guay
              ... 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.
              Message 6 of 15 , Aug 4 6:32 AM
              • 0 Attachment
                > 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?
              • Jean-Sebastien Guay
                BTW, ... 2.98 J-S ... From: Jean-Sebastien Guay To: Randy Kobes Cc: Sent:
                Message 7 of 15 , Aug 4 6:43 AM
                • 0 Attachment
                  BTW,

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

                  J-S

                  ----- Original Message -----
                  From: "Jean-Sebastien Guay" <jean_seb@...>
                  To: "Randy Kobes" <randy@...>
                  Cc: <modperl@...>
                  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?
                  >
                  >
                  >
                  >
                • Perrin Harkins
                  ... First, check your conf file to be sure you have this turned on. There are docs related to it here:
                  Message 8 of 15 , Aug 4 8:53 AM
                  • 0 Attachment
                    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
                  • Jean-Sebastien Guay
                    Perrin, ... 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
                    Message 9 of 15 , Aug 4 9:08 AM
                    • 0 Attachment
                      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@...
                      Software Developer, Hybride         http://www.hybride.com
                      Piedmont, Québec, Canada
                       
                    • Perrin Harkins
                      ... 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
                      Message 10 of 15 , Aug 4 9:16 AM
                      • 0 Attachment
                        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
                      • Jean-Sebastien Guay
                        ... 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(),
                        Message 11 of 15 , Aug 4 11:11 AM
                        • 0 Attachment
                          > 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@...
                          Software Developer, Hybride http://www.hybride.com
                          Piedmont, Québec, Canada
                        Your message has been successfully submitted and would be delivered to recipients shortly.