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

I am working on a perl script, here based on a if condition, i have to display differnt html pages(rather throw html pages on a browser...) one way to do is if(condition) { write all html code here } is there a way thru which i can instead call the html file like all that html code which is already been written in some xyz.html file can simply be called, like if(condition) { throw xyz.html on to the browser } if there is way, how? thanks a lot regards. rt

Replies are listed 'Best First'.
Re: calling a html file from a perl script
by HyperZonk (Friar) on Jul 21, 2001 at 05:39 UTC
    Perhaps I am missing something in this quest, but it would seem far easier, if you are using CGI.pm (which you probably should be if you are working with CGI), to use redirect.
    if (condition) { print redirect(-uri=>'http://www.mysite.com/xyz.html'); } else { print redirect(-uri=>'http://www.mysite.com/abc.html'); }

    See the documentation for CGI.pm for more details.

    Important note: the redirect must be the first thing printed to STDOUT for it to work.

    You can use the object oriented version (assuming you have done $q = new CGI;):
    if (condition) { print $q->redirect(-uri=>'http://www.mysite.com/xyz.html'); } else { print $q->redirect(-uri=>'http://www.mysite.com/abc.html'); }
Re: calling a html file from a perl script
by suaveant (Parson) on Jul 21, 2001 at 00:48 UTC
    yes... something like...
    my $file; if(case1) { $file = 'this.html'; } elsif(case2) { $file = 'that.html'; } else { $file = 'error.html'; } print "Content-Type: text/html\n\n"; { open(HTML, $file) or die $!; local $/; # this will make <HTML> just return the # whole file instead of line by line print <HTML>; close HTML; }

                    - Ant

      You don't need the local $/;
        Yes you do, unless you put the print in a while loop.

                        - Ant

Re: calling a html file from a perl script
by Agermain (Scribe) on Jul 21, 2001 at 00:59 UTC

    There's two ways I usually do something like this:

    Approach One: Use what's called a "here" document and type your HTML into the perl code, as shown below.

    if ($my_condition) { print <<EOF; <HTML><HEAD><TITLE>This code is shown if it's true</TITLE> </HEAD><BODY> <H1>My_condition is TRUE</H1> </BODY></HTML> EOF } else { <HTML><HEAD><TITLE>This code is shown if it's false</TITLE> </HEAD><BODY> <H1>My_condition is FALSE</H1> </BODY></HTML> EOF

    This is best used if you've got short documents.

    Approach Two: Use a CGI redirect. This is if you already have the HTML docs, and just want to serve them. In this case, DO NOT USE a CGI header (i.e. the line where you'd normally print "Content-type: text/html\n\n";)

    if ($mycondition) { print "Location: url_of_html_file\n\n"; } else { print "Location: url_of_another_html_file\n\n"; }

    This second approach is a little quicker and probably more along the lines of what you'd want to do.

      the second approach is a good approach, but doesn't it require a
      print "Status: 302 Moved Temporarily\n";
      at the top. Maybe not, but I've always used it that way, and seen it that way...

                      - Ant

        Apache will notice the "Location:" and change the status to 302. Inferior web servers may require additional handholding.

        -- Randal L. Schwartz, Perl hacker