perl - sending an existing file to browser

by "Insurance Squared" <gcooke(at)insurancesquared.com>

 Date:  Mon, 4 Jun 2001 08:49:36 -0400
 To:  <hwg-techniques(at)mail.hwg.org>
 Cc:  <c-newton(at)ihug.co.nz>
 References: 
  todo: View Thread, Original
Hi Peter,

If you know the name of the files ahead of time, you can build them into a
variable, something like this:
#
# Select the correct file, based upon what was input from the form.
if (something from the form input) {
    $filename = 'file1.html';
    }
else
    {
    $filename = 'file2.html';

then just open the file, read it in, close it, then print it (to STDOUT),
something like this:
#
# open the file and read it in
open(IN, "$filename");
@lines = <IN>;
close(IN);
#
# print each line (which will send it to the user's browser)
foreach $line (@lines) {
    #SEE NOTE BELOW
    print $line;
    }

#NOTE: right there you can put in code to parse the input file.  I typically
create a template with tokens embedded in the HTML like this: (Your answer
is:$answer$), then as I print the file I substitute the text $answere$ with
the calculated answer.
# example of parsing script for embedded tokens
$line =~ s/\$answer\$/$answer/i;

I use this type of thing to produce output pages from my scripts that are
unique for each customer and it works well.  There are more elegant ways to
do this, but I find this brute force approach easy to remember and
understand..

If you don't have the filenames ahead of time, you can read the filenames
into an array like this:
#
#
$directory='/path/to/your/files/';
@filelisting = glob("$directory*.html");
#
#

then work with @filelisting to find the file you need.

HTH,
Glenn

> ------------------------------
>
> Date: Mon, 4 Jun 2001 13:38:54 +1200
> From: "peter newton" <c-newton(at)ihug.co.nz>
> Subject: perl - sending an existing file to browser.
>
> Hello All,
> I did ask this some time ago but so far nothing seems  to be working, and
> now I'm at the point of action where I need to know in a hurry. What I
want
> is to have a perl script which when accessed by a form will send one of a
> number of already existing files back to the browser depending on the form
> content.
> I've no problem with anything except how to send an EXISTING html file,
> usually I create it on the fly within the script but in this case I just
> want to select an appropriate file that already exists.
>
> I believe this can be done using the ` ` character please let me know.
>
> Many Thanks
> Peter Newton
>
> ------------------------------
>
> End of hwg-techniques-digest V1 #1200
> *************************************
>

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA