Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to be able to include one of my scripts into both other CGI scripts, and SSIs

thing is.. when including into another script, it will always print the "Content-type:text/html" header to the page.. I don't want that

and when I take it away, the SSI doesn't work

what can I do?

also, is there a way to tell perl that.. while in this script.. make my default path be "/blah/blah/blah" but when you leave this script to return to the one that called me.. return to the previous path

Replies are listed 'Best First'.
Re: Calling script from SSI or CGI
by Masem (Monsignor) on Apr 22, 2001 at 07:31 UTC
    In the script to be called, wrap as much as you can, save for printing the HTTP header, into a subroutine, call it body() for example This should be a unique function name. In that script file, your main routine should be something like:
    use strict; use CGI; my $cgi = new CGI; print $cgi->header(); body(); 1; sub body { ... }
    If you are comfortable with it, it's probably better to make this a small module at this point, though in some situations I don't think that is necessary.

    In the script that you want to call this, simply now "include 'file.pl';" at the top, where file.pl is the script above, and where you want to include the output from that script, you can use "body();" there. Of course, if you make it a module, you'll need to do some other things.

    There's a few little gotchas with variable inclusion and the like, but that's the general idea.

    And if this body() will change directories, you can easily get the current working directory at the start of the sub, then restore that when you are done with the processing.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain