Perl: How do I "Require" or "Include" another perl script file?

Discussion in 'Software Programming & Web Scripting' started by Jasoco, Feb 13, 2002.

  1. Jasoco

    Jasoco Video Gamer/Collector

    Joined:
    Jun 25, 2001
    Messages:
    492
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Kmart Employee... Big K, not Super K, but nice any
    Location:
    Doylestown PA
    Say I have this one Perl file (page.pl) and I have a separate Perl file (subs.pl) that I want to call Subroutines in.

    I'd keep the Subs in the subs.pl file and from my other PL file I would call the subroutine. But how do I "Require" or "Include" the "subs.pl" file?

    This is the only thing holding me from a total site redesign.
     
  2. martinatkinson

    martinatkinson Registered

    Joined:
    Jun 7, 2001
    Messages:
    517
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Video Production
    Location:
    Granger, IN, USA
    Hello!

    I am just learning CGI so I do not know if this is right but I think all you have to do is put the following line of code into your file underneath your #!/usr/bin/perl line:

    Code:
    require "subs.pl";
    Let me know if this works.

    Have a great day!

    Albert
     
  3. Jasoco

    Jasoco Video Gamer/Collector

    Joined:
    Jun 25, 2001
    Messages:
    492
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Kmart Employee... Big K, not Super K, but nice any
    Location:
    Doylestown PA
    Nope. Doesn't work.

    Seems the test files I created aren't written right I guess?

    test.pl
    Code:
    use CGI;
    #!/usr/bin/perl
    
    require "settings.pl";
    
    print "Content-type: text/html\n\n";
    
    &TestSub;
    settings.pl
    Code:
    use CGI;
    #!/usr/bin/perl
    
    Sub TestSub {
    print "Hello!";
    }
     
  4. martinatkinson

    martinatkinson Registered

    Joined:
    Jun 7, 2001
    Messages:
    517
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Video Production
    Location:
    Granger, IN, USA
    Hello!

    Is the file "settings.pl" in the same directory as your main CGI script? It will need to be. If this does not work try using the absolute path to "settings.pl"

    Let me know what happens!

    Albert
     
  5. Jasoco

    Jasoco Video Gamer/Collector

    Joined:
    Jun 25, 2001
    Messages:
    492
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Kmart Employee... Big K, not Super K, but nice any
    Location:
    Doylestown PA
    The Settings file is in the same directory as the Test file. The error page seems to imply the program is looking in its own directory for the files so I tired an absolute path (D:/Inetpub/jasoco/cgi-bin/settings.pl) Which seems to get passed THAT, but now I have a whole new error.

    I seem to have it Requiring right, but now I have a problem with the Required files code.

    Am I doing the Sub wrong? Or what? This whole Require part is the ONLY thing keeping me from advancing my site further and I've been racking my brain trying to figure it out!
     
  6. martinatkinson

    martinatkinson Registered

    Joined:
    Jun 7, 2001
    Messages:
    517
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Video Production
    Location:
    Granger, IN, USA
    Hello!

    Hmmm, looks like my code only works if the contents of your settings.pl file are defining variables, for example, this is a clip out of one of my scripts:

    # path to template file
    $template_file="template.html";


    And then in your main file you could do something like:

    require "config.pl";

    #program start

    $top_page=top_page($template_file);
    $bottom_page=bottom_page($template_file);

    Other then that I do not know, I am just starting to learn Perl so I guess I was not much help.

    Have a great day!

    Albert
     
  7. Jasoco

    Jasoco Video Gamer/Collector

    Joined:
    Jun 25, 2001
    Messages:
    492
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Kmart Employee... Big K, not Super K, but nice any
    Location:
    Doylestown PA
    Don't feel bad. You're the third person to try and help me with Requiring. I can't seem to find info anywhere. Noone seems to know how to do it the way I need it done. Hmmm


    Well, I guess I'll look on Perl.com and CPAN again later.

    If they don't help, can anyone recommend a Perl book?

    I wonder if my server supports PHP...? Anyone have a way I can test? I don't own the server. The only way I know I can do Perl is that I put a file up and ran it. When it worked, I knew. But I need a sample PHP file I can upload and try. My host didn't tell me anything about what I can do and what I can't.
     
  8. Jadey

    Jadey sosumi

    Joined:
    Apr 14, 2001
    Messages:
    770
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    CANADA
    I've never had to work with CGI on NT, so don't know if this will help. Try using the http path to the script for the require, like:

    require "http://www.domain.ca/cgi-bin/settings.pl";
     
  9. snydersoft

    snydersoft Registered

    Joined:
    Jun 18, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Obviously this thread is OLD and my assistance probably isn't needed. But I surfed to it while looking for something else and who knows...maybe someone else will so I am going to add my thoughts on the matter in case it can help them out.

    As for the headers problem. While the text/html header was in the test file, you will notice that it wasn't in the settings file (the included file). The error received would suggest that the proper headings aren't being sent by the settings file so I would add the text/html header line to the included file.

    When including a file, the included file still processes whatever subroutine you call and then sends the result to the original file which places the result into it's own results in that place. Therefore since the settings file is trying to print text/html to the browser, the script isn't sending the proper headers to the browser to tell it what to expect. The settings file then sends the error to the main script. So the originating script doesn't have an error in it.

    Now, once this is taken care of (if that would actually fix the problem) you should also add the following..I am going to add it on its own line so you can see exactly what it is:

    1;

    That should be the last line of ANY file that you include in any script. When you add a require the script is going to test the included file for a value. Adding that line will make the require a "true" statement and therefore allow it to run.

    Now the error that was received may suggest that that wasn't a problem. But it's safe practice to do so. Especially if a script is being programmed for use by many people on many systems. You want everything to be as portable as possible.

    Hopefully this will help someone out someday.
     
  10. SeniorSE

    SeniorSE Registered

    Joined:
    Mar 11, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm kind of new to perl, but I see an error in your code above and I know how to accomplish what you want.

    First, the hash-bang line needs to be the first line in the perl script (before the use command).

    Second, when using "use" or "require", it appears that the system does not want .pl extensions, but rather .pm extensions. So, if you have code that looks like this:

    prog.pl
    Code:
    #!/usr/bin/perl -w
    
    use newsub;
    
    &newsub;
    
    exit;
    
    newsub.pm
    Code:
    #!/usr/bin/perl
    
    sub newsub {
    
    print "hello\n";
    
    }
    
    return 1;
    exit;
    
    And, make sure that the newsub.pm file is in the same directory as the perl script you are running it with. optionally, you can figure out how to add it to your perllocal.pod config file, so it will be global to your perl installation. (I don't know how to do that, though. ;) )

    Good luck,
    Mark
     
  11. ravipatel4

    ravipatel4 Registered

    Joined:
    Jun 30, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    The problem of the header error is due to the wrong positions of lines.
    You need to add the header line first.
    Means put the line
    print "Content-type: text/html\n\n";
    before the "require" statement.
    It will definitely solve your problem.
    contact me if you have any other problem.
    You can contact me at www.liveperson.com/ravi-patel

    Good luck.
    Bye

    Ravi Patel
     
  12. skholastikos

    skholastikos Registered

    Joined:
    Mar 7, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I was also having the same problem where INC was not able to find the file to be included, even though it was in the same directory as the main.pl file. The problem was not in my use of the 'require', but in the way I was running the program.

    In terminal (on mac) I was running the program from the root directory by using the command:

    Code:
    /Users/jsgoyette/Desktop/main.pl
    However in doing so, it seems the file to be included was being searched for in the root directory. The problem was thus simply corrected by moving to the Desktop, where I had program.pl, by:

    Code:
    cd desktop
    and then by running the program with:

    Code:
    ./main.pl
    I'm not sure what all of this means, but for future newbies like me, I thought it was worth mentioning.
     

Share This Page