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

Current directory

Expand Messages
  • Oskar
    Hi, when I am executing perl script on Apache it seems that the current directory is not the directory where the script is located but the directory c: program
    Message 1 of 15 , Jun 16, 2003
    • 0 Attachment
      Hi,
      when I am executing perl script on Apache it seems that the current directory is not the directory where the script is located but the directory c:\program files\apache group\apache. I need current dir to be the script dir since i have some pm in the script directories.
      I have apache 1.3.27 and mod_perl 1.
      Thanks Oskar
    • Thomas Klausner
      Hi! ... See: http://perl.apache.org/docs/1.0/guide/porting.html#_INC_and_mod_perl -- #!/usr/bin/perl http://domm.zsi.at for(ref
      Message 2 of 15 , Jun 17, 2003
      • 0 Attachment
        Hi!

        On Tue, Jun 17, 2003 at 02:19:49AM +0200, Oskar wrote:

        > when I am executing perl script on Apache it seems that the current
        > directory is not the directory where the script is located but the directory
        > c:\program files\apache group\apache. I need current dir to be the script
        > dir since i have some pm in the script directories.
        > I have apache 1.3.27 and mod_perl 1.

        See:
        http://perl.apache.org/docs/1.0/guide/porting.html#_INC_and_mod_perl


        --
        #!/usr/bin/perl http://domm.zsi.at
        for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
      • Oskar
        I already read that article before posted the message here but it does not solve the problem: I am running more than one web so the adding of each particular
        Message 3 of 15 , Jun 17, 2003
        • 0 Attachment
          I already read that article before posted the message here but it does not
          solve the problem: I am running more than one web so the adding of each
          particular directory seems to me little bit dumb. Also i dont see the reason
          why the current dir should be changed to the dir different from the script
          dir.
          Any other suggestion to solve it?
          Oskar
          ----- Original Message -----
          From: "Thomas Klausner" <domm@...>
          To: <modperl@...>
          Sent: Tuesday, June 17, 2003 9:41 AM
          Subject: Re: Current directory


          > Hi!
          >
          > On Tue, Jun 17, 2003 at 02:19:49AM +0200, Oskar wrote:
          >
          > > when I am executing perl script on Apache it seems that the current
          > > directory is not the directory where the script is located but the
          directory
          > > c:\program files\apache group\apache. I need current dir to be the
          script
          > > dir since i have some pm in the script directories.
          > > I have apache 1.3.27 and mod_perl 1.
          >
          > See:
          > http://perl.apache.org/docs/1.0/guide/porting.html#_INC_and_mod_perl
          >
          >
          > --
          > #!/usr/bin/perl http://domm.zsi.at
          > for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
          >
          >
        • Randy Kobes
          ... You might try the FindBin module; from the docs, NAME FindBin - Locate directory of original perl script SYNOPSIS use FindBin; use lib
          Message 4 of 15 , Jun 17, 2003
          • 0 Attachment
            On Tue, 17 Jun 2003, Oskar wrote:

            > ----- Original Message -----
            > From: "Thomas Klausner" <domm@...>
            > To: <modperl@...>
            > Sent: Tuesday, June 17, 2003 9:41 AM
            > Subject: Re: Current directory
            >
            > > Hi!
            > >
            > > On Tue, Jun 17, 2003 at 02:19:49AM +0200, Oskar wrote:
            > >
            > > > when I am executing perl script on Apache it seems that the
            > > > current directory is not the directory where the script is
            > > > located but the directory c:\program files\apache
            > > > group\apache. I need current dir to be the script dir since
            > > > i have some pm in the script directories. I have apache
            > > > 1.3.27 and mod_perl 1.
            > >
            > > See:
            > > http://perl.apache.org/docs/1.0/guide/porting.html#_INC_and_mod_perl
            > >
            > I already read that article before posted the message here but
            > it does not solve the problem: I am running more than one web
            > so the adding of each particular directory seems to me little
            > bit dumb. Also i dont see the reason why the current dir should
            > be changed to the dir different from the script dir. Any other
            > suggestion to solve it?

            You might try the FindBin module; from the docs,

            NAME
            FindBin - Locate directory of original perl script

            SYNOPSIS
            use FindBin;
            use lib "$FindBin::Bin/../lib";
            or
            use FindBin qw($Bin);
            use lib "$Bin/../lib";

            However, note the following:

            KNOWN ISSUES
            If there are two modules using "FindBin" from different
            directories under the same interpreter, this won't work. Since
            "FindBin" uses "BEGIN" block, it'll be executed only once, and
            only the first caller will get it right. This is a problem under
            mod_perl and other persistent Perl environments, where you
            shouldn't use this module. Which also means that you should avoid
            using "FindBin" in modules that you plan to put on CPAN. The only
            way to make sure that "FindBin" will work is to force the "BEGIN"
            block to be executed again:

            delete $INC{'FindBin.pm'};
            require FindBin;

            --
            best regards,
            randy kobes
          • 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 5 of 15 , Aug 1, 2003
            • 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 6 of 15 , Aug 1, 2003
              • 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 7 of 15 , Aug 1, 2003
                • 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 8 of 15 , Aug 1, 2003
                  • 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 9 of 15 , Aug 1, 2003
                    • 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 10 of 15 , Aug 4, 2003
                      • 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 11 of 15 , Aug 4, 2003
                        • 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 12 of 15 , Aug 4, 2003
                          • 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 13 of 15 , Aug 4, 2003
                            • 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 14 of 15 , Aug 4, 2003
                              • 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 15 of 15 , Aug 4, 2003
                                • 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.