"require" problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    "require" problem?

    Guten Morgen,


    I am implementing a script that my client wants on their website. Within
    the script there are several "require" statements. Any time one of these
    "require" statements is executed, the script terminates.

    Any ideas how I might solve this problem?


    Cheers!
  • Gunnar Hjalmarsson

    #2
    Re: "require&q uot; problem?

    Michael wrote:[color=blue]
    > I am implementing a script that my client wants on their website.
    > Within the script there are several "require" statements. Any time
    > one of these "require" statements is executed, the script
    > terminates.
    >
    > Any ideas how I might solve this problem?[/color]

    How about installing the modules that it tries to require? Just a thought.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    • Michael

      #3
      Re: "require&q uot; problem?

      On Thu, 18 Mar 2004 13:40:10 +0000, Gunnar Hjalmarsson wrote:
      [color=blue]
      > Michael wrote:[color=green]
      >> I am implementing a script that my client wants on their website.
      >> Within the script there are several "require" statements. Any time
      >> one of these "require" statements is executed, the script
      >> terminates.
      >>
      >> Any ideas how I might solve this problem?[/color]
      >
      > How about installing the modules that it tries to require? Just a thought.[/color]

      The files are installed just where I believe they need to be. Here is the
      code that is giving me the problem:

      &require_suppor ting_libraries
      (__FILE__, __LINE__,
      "$path/file1.cfg",
      "$path/file2.cfg",
      "$path/library/file3.pl",
      "$path/html/file4.pl",
      "$path/library/file5.pl");

      sub require_support ing_libraries
      {
      local ($file, $line, @require_files) = @_; local ($require_file) ;

      foreach $require_file (@require_files )
      {
      if (-e "$require_f ile" && -r "$require_file" )
      {
      print "$require_f ile <br><br>";
      require '$require_file' ;
      }
      }
      }


      When I run the code the path and name of the first file is printed to the
      browser window. Only the first file is listed. If I comment out the
      require statement, all of the files are listed. Clearly the code is
      finding the files (as it makes it into the if code block) but when the
      require statement is executed, the code terminates.

      rats.

      Is there a problem with the syntax of the require statement? Or maybe the
      permissions of the files needs to be different?

      Any ideas will be appreciated.


      Cheers!

      Comment

      • Gunnar Hjalmarsson

        #4
        Re: &quot;require&q uot; problem?

        Michael wrote:[color=blue]
        >
        > require '$require_file' ;[/color]

        <snip>
        [color=blue]
        > Is there a problem with the syntax of the require statement?[/color]

        Yes, it tries to require a file *literally* named '$require_file' .
        Remove those quotes:

        require $require_file;
        [color=blue]
        > Or maybe the permissions of the files needs to be different?[/color]

        It seems to be checking for that.

        You posted a piece of buggy code written for a several years old Perl
        generation (Perl 4). You may want to advise your client to use a
        modern quality script instead.

        --
        Gunnar Hjalmarsson
        Email: http://www.gunnar.cc/cgi-bin/contact.pl

        Comment

        • Michael

          #5
          Re: &quot;require&q uot; problem?

          On Fri, 19 Mar 2004 06:59:07 +0000, Gunnar Hjalmarsson wrote:
          [color=blue]
          > Michael wrote:[color=green]
          >>
          >> require '$require_file' ;[/color]
          >
          > <snip>
          >[color=green]
          >> Is there a problem with the syntax of the require statement?[/color]
          >
          > Yes, it tries to require a file *literally* named '$require_file' .
          > Remove those quotes:
          >
          > require $require_file;
          >[color=green]
          >> Or maybe the permissions of the files needs to be different?[/color]
          >
          > It seems to be checking for that.
          >
          > You posted a piece of buggy code written for a several years old Perl
          > generation (Perl 4). You may want to advise your client to use a
          > modern quality script instead.[/color]

          The code originaly did not have the quotes around $require_file. Without
          the quotes the code would terminate with an error message "Internal Server
          Error". So I added them to try and fix the problem, but seem to have
          created another.

          rats.

          Comment

          • Jürgen Exner

            #6
            Re: &quot;require&q uot; problem?

            Michael wrote:
            [...][color=blue]
            > Without the quotes the code would terminate with an error message
            > "Internal Server Error".[/color]

            This is not a Perl (or perl) error message. Check the server logs for the
            real error message.

            jue


            Comment

            • Gunnar Hjalmarsson

              #7
              Re: &quot;require&q uot; problem?

              Michael wrote:[color=blue]
              > Gunnar Hjalmarsson wrote:[color=green]
              >> Michael wrote:[color=darkred]
              >>> Is there a problem with the syntax of the require statement?[/color]
              >>
              >> Yes, it tries to require a file *literally* named
              >> '$require_file' . Remove those quotes:
              >>
              >> require $require_file;[/color]
              >
              > The code originaly did not have the quotes around $require_file.
              > Without the quotes the code would terminate with an error message
              > "Internal Server Error". So I added them to try and fix the
              > problem, but seem to have created another.[/color]

              You are obviously running the script as a CGI script, which it would
              have been appropriate to let us know about in your initial message.

              To have the browser display a more meaningful error message, you can
              add this line somewhere at the beginning of the script:

              use CGI::Carp 'fatalsToBrowse r';

              --
              Gunnar Hjalmarsson
              Email: http://www.gunnar.cc/cgi-bin/contact.pl

              Comment

              • Michael

                #8
                Re: &quot;require&q uot; problem?

                On Fri, 19 Mar 2004 06:59:07 +0000, Gunnar Hjalmarsson wrote:
                [color=blue]
                > You posted a piece of buggy code written for a several years old Perl
                > generation (Perl 4). You may want to advise your client to use a
                > modern quality script instead.[/color]


                Hmm. Well, that might be a good idea. I typically perfer to write
                my own code, but the client insists on the one that he has purchased a few
                years ago and used successfully on a previous server.


                What if I replaced that mess of code with:

                use '$path/file1.cfg';
                use '$path/file2.cfg';
                use '$path/library/file3.pl';
                use '$path/html/file4.pl';
                use '$path/library/file5.pl';

                Given that I have read that

                use somemodule();

                is equivalent to:

                BEGIN {require somemodule}


                I figure that an attempt to make this code work will be less time than
                rewriting it.

                Comment

                • Gunnar Hjalmarsson

                  #9
                  Re: &quot;require&q uot; problem?

                  Michael wrote:[color=blue]
                  > On Fri, 19 Mar 2004 06:59:07 +0000, Gunnar Hjalmarsson wrote:[color=green]
                  >> You posted a piece of buggy code written for a several years old
                  >> Perl generation (Perl 4). You may want to advise your client to
                  >> use a modern quality script instead.[/color]
                  >
                  > Hmm. Well, that might be a good idea. I typically perfer to write
                  > my own code, but the client insists on the one that he has
                  > purchased a few years ago and used successfully on a previous
                  > server.
                  >
                  > What if I replaced that mess of code with:
                  >
                  > use '$path/file1.cfg';
                  > use '$path/file2.cfg';
                  > use '$path/library/file3.pl';
                  > use '$path/html/file4.pl';
                  > use '$path/library/file5.pl';[/color]

                  What happened when you tried it?

                  Honestly, if you are writing scripts for a client, you should know the
                  answer to that, or you should at least be able to figure it out
                  without asking here.

                  If you don't have access to the server's error log, did you try my
                  advice to load the CGI::Carp module and import the 'fatalsToBrowse r'
                  routine?
                  [color=blue]
                  > Given that I have read that
                  >
                  > use somemodule();
                  >
                  > is equivalent to:
                  >
                  > BEGIN {require somemodule}[/color]

                  That's only partly true, and not relevant here, since the files you
                  are trying to load aren't modules, i.e. they are not *.pm files.

                  --
                  Gunnar Hjalmarsson
                  Email: http://www.gunnar.cc/cgi-bin/contact.pl

                  Comment

                  • Joe Smith

                    #10
                    Re: &quot;require&q uot; problem?

                    Gunnar Hjalmarsson wrote:
                    [color=blue]
                    > Michael wrote:[color=green][color=darkred]
                    >>> require $require_file;[/color]
                    >>
                    >> The code originaly did not have the quotes around $require_file.
                    >> Without the quotes the code would terminate with an error message
                    >> "Internal Server Error". So I added them to try and fix the
                    >> problem, but seem to have created another.[/color]
                    >
                    > You are obviously running the script as a CGI script, which it would
                    > have been appropriate to let us know about in your initial message.
                    >
                    > To have the browser display a more meaningful error message, you can
                    > add this line somewhere at the beginning of the script:
                    >
                    > use CGI::Carp 'fatalsToBrowse r';[/color]

                    In addition to the above advice, make sure that each of your
                    require files ends with

                    1;

                    so that perl will recognize that the file was included successully.
                    -Joe

                    Comment

                    • Michael

                      #11
                      Re: &quot;require&q uot; problem?

                      On Sat, 20 Mar 2004 22:10:09 +0000, Gunnar Hjalmarsson wrote:
                      [color=blue]
                      > If you don't have access to the server's error log, did you try my
                      > advice to load the CGI::Carp module and import the 'fatalsToBrowse r'
                      > routine?[/color]


                      I tried it. With "require '$require_file' ;" I receive the following
                      error message:



                      With "require $require_file;" the script terminates without any output.


                      I have checked to make sure that the directory where the files are, are in
                      @ic. I added the directory with "use lib". I then have a "print @INC;"
                      statement that outputs all of the directories in @INC and it does print
                      the one where the files I need are. However, when I use the second
                      version of the $require statement (the one without single quotes) there is
                      no output from the script.


                      Cheers!

                      Comment

                      • Gunnar Hjalmarsson

                        #12
                        Re: &quot;require&q uot; problem?

                        Michael wrote:[color=blue]
                        > On Sat, 20 Mar 2004 22:10:09 +0000, Gunnar Hjalmarsson wrote:[color=green]
                        >> did you try my advice to load the CGI::Carp module and import the
                        >> 'fatalsToBrowse r' routine?[/color]
                        >
                        > I tried it. With "require '$require_file' ;" I receive the
                        > following error message:
                        >
                        > http://bfr.caseywest.com/archives/001059.html[/color]

                        That is not an error message.
                        [color=blue]
                        > With "require $require_file;" the script terminates without any
                        > output.
                        >
                        > I have checked to make sure that the directory where the files are,
                        > are in @ic. I added the directory with "use lib". I then have a
                        > "print @INC;" statement that outputs all of the directories in @INC
                        > and it does print the one where the files I need are. However,
                        > when I use the second version of the $require statement (the one
                        > without single quotes) there is no output from the script.[/color]

                        I have a feeling that you may be messing up the paths. If the $path
                        variable contains the *full* path to the directory, there should not
                        be a need to add any "use lib" statement. You'd better ensure that
                        $path is the full path. (As long as the $path variable contains
                        anything, but something else but the full path, a "use lib" statement
                        won't help.)

                        Also, did you see Joe's advice?

                        --
                        Gunnar Hjalmarsson
                        Email: http://www.gunnar.cc/cgi-bin/contact.pl

                        Comment

                        • Michael

                          #13
                          Re: &quot;require&q uot; problem?

                          On Sat, 20 Mar 2004 23:49:57 +0000, Gunnar Hjalmarsson wrote:

                          [color=blue][color=green]
                          >> http://bfr.caseywest.com/archives/001059.html[/color]
                          >
                          > That is not an error message.
                          >[/color]

                          Oops. It should have been:

                          Can't locate $require_file in @INC
                          [color=blue][color=green]
                          >> With "require $require_file;" the script terminates without any
                          >> output.
                          >>
                          >> I have checked to make sure that the directory where the files are,
                          >> are in @ic. I added the directory with "use lib". I then have a
                          >> "print @INC;" statement that outputs all of the directories in @INC
                          >> and it does print the one where the files I need are. However,
                          >> when I use the second version of the $require statement (the one
                          >> without single quotes) there is no output from the script.[/color]
                          >
                          > I have a feeling that you may be messing up the paths. If the $path
                          > variable contains the *full* path to the directory, there should not
                          > be a need to add any "use lib" statement. You'd better ensure that
                          > $path is the full path. (As long as the $path variable contains
                          > anything, but something else but the full path, a "use lib" statement
                          > won't help.)[/color]

                          I believe that $path contains the full path to the files. I printed the
                          path out in the loop just before each "require" statement and it is the
                          path to the files as far as I know them to be, unless the server is using
                          some kind of aliasing or symbolic links that I am unaware of.
                          [color=blue]
                          > Also, did you see Joe's advice?[/color]

                          I did see Joe's advice. Each file has the "1;" at the end.


                          Cheers!



                          Comment

                          • Gunnar Hjalmarsson

                            #14
                            Re: &quot;require&q uot; problem?

                            Another idea is to replace

                            require $require_file;

                            with

                            eval { require $require_file };
                            die $@ if $@;

                            --
                            Gunnar Hjalmarsson
                            Email: http://www.gunnar.cc/cgi-bin/contact.pl

                            Comment

                            • Michael

                              #15
                              Re: &quot;require&q uot; problem?

                              On Sun, 21 Mar 2004 00:41:26 +0000, Gunnar Hjalmarsson wrote:
                              [color=blue]
                              > Another idea is to replace
                              >
                              > require $require_file;
                              >
                              > with
                              >
                              > eval { require $require_file };
                              > die $@ if $@;[/color]

                              I get the same result with both. Script terminates with no output.

                              rats.


                              I appreciate your help, but I feel that I must have taken up more of your
                              time than this little problem of mine deserves.

                              I believe I have two options:

                              1) continue to struggle to get this script to work.

                              2) rewrite it from scratch. Of course in this option I would likely
                              rewrite it in a scripting language that I have more experience with (PHP
                              or Python)


                              I really do appreciate your efforts and your patience with my lack of Perl
                              knowledge.


                              Cheers!

                              Comment

                              Working...