Gossamer Forum
Home : General : Perl Programming :

Includes (e.g. SSI)?

Quote Reply
Includes (e.g. SSI)?
I posted this problem in the DBMan forum, but have yet to receive a reply. I tried inserting a Server Side Include (SSI) within my html.pl, or the template by using <!--#include file="../../file.htm" -->. Unfortunately, because this is a CGI file and not an HTML file, it is not parsed and subsequently, the SSI is not run. Is there a CGI command that includes a file? I know that you can check to see if a file exists and other things, but how about including a file and how would I go about inserting it into my html.pl?

Thanks much in advance.
-Blake Kunisch
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Code:
print "<!--#include file=\"../../file.htm\" -->";

Have you tried this?

Pasha

------------------
find.virtualave.net
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Otherwise, if you were to use "html.pl", you could add a subroutine to html.pl along the lines of:

Code:
sub InsertFile {
open(FILE, "<file.htm");
$File = <FILE>;
close(FILE);

return $File;
}

Then you could call it as you would any other subroutine:
&InsertFile;

Remember to close any print qq statements before you make that call though.

ie.

Code:
print qq~ This is the text before.~;
&InsertFile;
print qq~ This is the text after~;

--Cade



[This message has been edited by Cade (edited July 23, 1999).]
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Pasha and Cade, thanks for the input, but neither way works. I've tried almost all variations of Cade's method, but nothing seems to work. I've tried absolute paths, using .txt files instead of .htm, everything. Please, if there is another way to do this let me know.

-Blake Kunisch
blake@outermost.net
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Could you post a snippet of the file, or give us an address of where we could view it? Its hard to trouble-shoot blindly. I'd be more than happy to help you if I could see where and how you're inserting thngs into the file.

--Cade Ekblad-Frank
Quote Reply
Re: Includes (e.g. SSI)? In reply to
There isn't really anything I can post that would help you out. You can take a look at my www.outermost.net/html_pl.txt html.pl and the www.outermost.net/pass.txt text file I would like to include. I tried Pasha's way, but the server doesn't parse CGI output for SSI so that won't work and I've tried your way Cade many different ways. I've tried:
Code:
sub InsertFile {
open (FILE, "<usr/local/www/net-outermos/htdocs/pass.txt");
$File = <FILE>;
close(FILE);
return $File;}
Code:
sub InsertFile {
open (FILE, "<pass.txt");
$File = <FILE>;
close(FILE);
return $File;
}
Code:
sub InsertFile {
open (FILE, "<http://www.outermost.net/pass.txt");
$File = <FILE>;
close(FILE);
return
$File;}

And all combinations without the < in front of the file name (looks like a typo to me). If you have any other questions, I can be reached quickly by ICQ: 7703534 or e-mail blake@outermost.net .

Thanks much,
Blake Kunisch

[This message has been edited by blakex (edited July 25, 1999).]
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Have you tried this?

Code:
open (FILE, "http://www.outermost.net/pass.txt");
$File = <FILE>;
close(FILE);
print "$File";

or this:

Code:
open (FILE, "http://www.outermost.net/pass.txt");
$File = <FILE>;
print "$File";
close(FILE);

There's got to be some way to insert this thing.


Pasha

------------------
find.virtualave.net
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Hi

I use this code to get the file:

Code:
sub include_file {
open(FILE, "/full/server/path/filename.html") or die("Couldn't open the file.");
@lines = <FILE>;
close(FILE);
foreach $line (@lines) {
print $line;
}
}

and this to insert it where it's wanted:

Code:
|; &include_file; print qq|



[This message has been edited by Chris Croome (edited July 26, 1999).]
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Pasha, I didn't have a chance to try your method, but I tried Chris' and it worked perfectly. Thanks a lot guys (and gals). You've all been a great help getting dbman up and running (now if only the links script would work Smile ).

-Blake Kunisch
http://www.outermost.net
Quote Reply
Re: Includes (e.g. SSI)? In reply to
For the benefit of anyone who might read this thread. In order to include a file's contents you use (or something similar to it):

Code:
sub InsertFile {
open(FILE, "</absolute/path/to/file.txt");
@File = <FILE>;
close(FILE);

print "@File\n";

This will print the contents of the file (@File) to where ever the subroutine is called. It is unecessary to to use a foreach loop to print each line of the file. Simply calling the array will make every line of the file accessible.

My previous example was a mistake after a long day of traveling (from New York to London).

--Cade Ekblad-Frank
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Although you should always check the return of open()! How about:

Code:
sub insert_file {
open (FILE, "/path/to/file.txt") or die "can't open: /path/to/file.txt ($!)";
print <FILE>;
close FILE;
}

should do the trick as well.

Cheers,

Alex
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Well, this is a thread that has certainly come in handy for me. Smile I have used Alex's suggested method to pull a file into several scripts for menu purposes. The one script I haven't yet gotten this to work on yet is Links. Here's what I'm trying to do, in effect:

Code:
$site_header = qq~
open (FILE, "../../file.txt") or die "can't open: ../../file.txt ($!)";
print <FILE>;
close FILE;

<TD VALIGN="top" ALIGN="left">
<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR><TD ALIGN="left" VALIGN="top" WIDTH="50">
etc.

I have tried it several ways in addition to what I wrote above. As you can probably guess, the file call info simply prints out to the screen. I have tried closing the print statements in the manner of Dbman and calling a separate subroutine, but that had the same effect. I'm sure there's some simple solution to this that someone more knowlegeable about Perl can provide. Wink At least, that's what I'm hoping...

Thanks in advance for any help.

Dan
Quote Reply
Re: Includes (e.g. SSI)? In reply to
That won't work, becuase you can't execute Perl code within a print statement. Probably best to do something like:

Code:
$file = "../../file.txt";
open (FILE, $file) or die "Can't open $file ($!)";
@lines = <FILE>;
close FILE;
foreach $line (@lines) {
$site_header = $line;
}
site_header .= qq~
<TD VALIGN="top" ALIGN="left">
<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD ALIGN="left" VALIGN="top" WIDTH="50">
etc.
~;

adam
Quote Reply
Re: Includes (e.g. SSI)? In reply to
is there a way of having the same result with something like
<!--#exec cgi="/counter2.cgi"-->
???
thanks
Quote Reply
Re: Includes (e.g. SSI)? In reply to
I tried almost solutions here but I always have the error : (No such file or directory)
Any help?
Thanks
Serge
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Adam,

Thanks for the resonse. I've now got the following:

Code:
$file = "../../cgi_pointer.txt";
open (FILE, $file) or die "Can't open $file ($!)";
@lines = <FILE>;
close FILE;
foreach $line (@lines) {
$site_header = $line;
}
site_header .= qq~
<TD VALIGN="top" ALIGN="left">
<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0">
blah, blah, blah...
<TABLE WIDTH="95%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD>~;

Which gives me the following error:

Error including libraries: Can't modify constant item in scalar assignment at /path_stuff/site_html_templates.pl line 67, near "<TABLE WIDTH="95%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD>~;"
(Might be a runaway multi-line ~~ string starting on line 45)

Make sure they exist, permissions are set properly, and paths are set correctly.

This is line 45:

Code:
site_header .= qq~

Any ideas?

Thanks,
Dan

p.s. Seth, you can't use SSI from within a cgi program. Sorry. That's largely why I'm trying to set up the header/menu in this manner. Smile
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Dan,

I may be wrong, but that looks like the error messge you get when you stick an email address in the print statement and forget to escape the @ sign. So have you an email address in "blah blah blah"? Smile

If you do, change it from user@domain.tld to user\@domain.tld.

adam
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Oh buggrit, I just saw a(nother) boo-boo!

I'm reassigning the variable every time I go through the foreach loop. It should be:

foreach $line (@lines) {
$site_header .= $line;
}

You use the dot in front of the equals sign to tack the next part onto the end of the current string. Can never remember what that's called. Contanecation or somefing. Anybody? Anybody? Bueller?

Try that anyway.

adam
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Hey Adam,

Good news and bad news... First, though, excellent guess! I do have an @ in the text file Smile, but I don't think that's the problem. The other 5 scripts I have set up to include that file don't have a problem with the @ unescaped, nor does Links if I simply leave the file's content in the print statement. Plus, escaping it doesn't change the error message. Frown

The good news is (I'm reaching) that my new error message answers your question:

Code:
Error including libraries: Can't modify constant item in concatenation at /blahblahblah/site_html_templates.pl
line 67, near "<TABLE WIDTH="95%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD>~;"
(Might be a runaway multi-line ~~ string starting on line 45)

Make sure they exist, permissions are set properly, and paths are set correctly.

Is concatenation the word you're looking for? Wink The only change was adding the .= to the statement (I wondered what that does!)

Dan


[This message has been edited by Dan Kaplan (edited August 18, 1999).]
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Ok, for anyone following along (not likely) I found that the problem was a rather simple one:

Code:
$file = "../../file.txt";
open (FILE, $file) or die "Can't open $file ($!)";
@lines = <FILE>;
close FILE;
foreach $line (@lines) {
$site_header .= $line;
}
site_header .= qq~
<TD VALIGN="top" ALIGN="left">
<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD ALIGN="left" VALIGN="top" WIDTH="50">
etc.
~;

The line

Code:
site_header .= qq~

should be

Code:
$site_header .= qq~

That solved the problem!!!

Dan

[This message has been edited by Dan Kaplan (edited August 16, 1999).]
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Dan,

My apologies for not coming back sooner, and for that error as well, I should have noticed it.

> Is concatenation the word you're looking for?

That's the one. I'll have forgotten it again in five minutes though! Smile

>The only change was adding the .= to the
> statement (I wondered what that does)

Well, that's what concatenation is, ratherthan replacing the variable, it appends it to the end of it. You can also use it like this:

$variable = $variable1 . $variable2;

Very handy! Smile

adam
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Adam,

Not a problem, it gave me a while to let things simmer in the back of my head. Smile It's actually kind of nice figuring out a Perl problem on my own, seeing as how I'm more of a HTML guy. I'm thinking about trying to write a couple simple little Perl scripts, so I'll definitely be putting what I've learned so far to use!

Of course, I have a much bigger problem right now... Apparently, my IP address changed over night. Frown Sort of screws up the majority of my site.

Dan
Quote Reply
Re: Includes (e.g. SSI)? In reply to
Hmm, I also noticed that, in the case of Links,

Code:
$file = "../../file.txt";

should have the full path to the file instead of what I wrote above. The relative path worked for the templates, but I didn't realize the scripts (search, add, modify, etc.) are created dynamically and are coming from a different directory level than the build routine.

Dan