PDA

View Full Version : Includes in Perl


digitalsea
06-12-03, 07:54 AM
Hey everyone,

Is there a way to call another Perl script and have it displayed in a Perl script (sort of like how you can include a file in PHP)?

If you can't include another script to pull info from, is there another way to integrate the output from both scripts on one page?


This is something that has baffled me for hours!!! :confused:

Pearl
06-12-03, 12:12 PM
It really depends on what exactly you are trying to do, and how your scripts are written. Perl Scripts can work together using for example the require(); method. But.. it's sort of hard to offer a suitable solution without some background on the scripts themselves. Are they completely independant scripts? Are "you" writing them so they work together? etc...

Perhaps someone has an idea, my coffee's still kicking in ;)
http://www.perldoc.com/
http://www.perldoc.com/perl5.8.0/pod/perlfunc.html#Alphabetical-Listing-of-Perl-Functions

digitalsea
06-12-03, 04:56 PM
Well I actually want on my site...

http://www.digital-sea.com/

for a deatiled view of an app (such as (http://www.digital-sea.com/cgi-bin/detail.cgi?ID=4074) to able to also pull a live count of visitors on the site.

I was looking and found Perl scripts that did the counting, so wanted it to show up too on the page.

Pearl
06-12-03, 08:33 PM
So.. did you just want a general visitor info on all your .cgi pages? or are you actually trying to do a visitor count per each detailed app page, such as: (4 users browsing Web Developer:Site Management etc..)

The second choice is yet harder to work out (more detailed), and unfortunately for the first choice I can't see any other way then hard coding your perl script to accept the other. Even more frustrating if both scripts aren't written by the same author.

It can be accomplished but without seeing the two scripts, I'd be mindlessly passing your through one example to the next :( Unless someone here has a quick dirty solution, and even then, without seeing the code... who knows. You can try the require(); method as I mentioned above and call the subroutine that outputs your visitor info where you want to see it, (just remember that the last statement must return true (1;) in the counter script you are calling), or simply write a small code that opens the database file where all your visitor info is being kept, whereas you could manipulate the output as well.

All the best!

digitalsea
06-12-03, 08:54 PM
Thanks for your response.

I tried that and since the script is like the one here at hotscripts.com, I have to put the include in a file that would only parse the templates, so it doesn't directy effect the CGI script.

Would it be possible to use SSI only with the ID still in place...

for instance, the page would be detailed.shtml?ID=whatever

and 'whatever' would be replaced by the number which would replace the number in the SSI call itself? Can HTML do this kind of parsing?

Pearl
06-12-03, 09:20 PM
But your app pages are generated through a .cgi script? or can you make, I assume (Links by Gossamer?) update your pages to produce static pages? If so then you can use SSI in your template design. You can't have SSI + CGI, it's one or the other, however there are some weird servers out there that let you parse SSI with CGI. Sorry you lost me on this one :\

and.. when you say you "tried that" what exactly did you try? require, or coding it in yourself? and if so what happened when you tried? Did you get an error?

DAL
06-13-03, 06:41 AM
If you goto my website I have a perl counter on there. I use the vertual include to call the script and grab the output. I also had to rename the index.html to index.shtml. if you just put in this on the index.shtml file where I wanted the output;

<!--#include virtual="/cgi-bin/counter.pl" -->

Im only starting out in perl so I may have got the wrong end of the stick to what you were asking but hope its some use to you.

digitalsea
06-13-03, 09:10 AM
Pearl,

I tried to insert basically the entire Perl script that counted visitors (only four lines - something I had from a couple years back) into the parsing script and even add.cgi itself, but without any luck. I use Links 2.0 and tried to look at the Gossamer Forums, but they don't really support it anymore...


My server does let me call a CGI with SSI, so that's why I was asking about just generating a .shtml page and then dynamically filling the ID with a form tag.

I know you can specify contents of a form (like a textfield) by using ?textfield=whatever, but could I use a hidden field in the SSI call to pull the correct ID?

If you're totally confused let me know...

Pearl
06-13-03, 01:19 PM
Oh yeah.. I'm totally confused! Happens often.. don't worry. Eventually I'll go "OH.. that's what you meant!" :D

Okay.. the reason I'm lost is because at first I thought you wanted to code in your counter and include it somehow on your scripts that you are already using. I think I'm realizing now that all you wish to do is try the SSI method. Is this right?

My server does let me call a CGI with SSI,
Firstly if your server requires you to change your extension to .shtml inorder for SSI to parse correctly on your pages, I'll bet to bottoms that you cannot parse SSI within a CGI script. Just a guess. Or else you could just easily insert your SSI tag within your generated header/footer file.

I'm not familar with the codings of Gossamer Links, so I'm not very helpful on that.. in fact I'm not being helpful at all, I feel like a dork 'cause I can't for the life of me see what you're doing.

generating a .shtml page and then dynamically filling the ID with a form tag.
Why do you want to do this? Curious. From what I can see you already have the ID number's filled in: ex: detail.cgi?ID=5038

I know you can specify contents of a form (like a textfield) by using ?textfield=whatever, but could I use a hidden field in the SSI call to pull the correct ID?
Under normal circumstances, there is a program at the other end that reads the output from the get/post (Query_String), that will manipulate what has been entered from let's say the textfield. SSI (Server Side Includes) is only a convenient method to insert things such as the time, files, or to execute a cgi program etc... where your server reads this information and if correct, it will insert whatever it is you're calling. Therefore, you can't input a hidden field (not that I know of) that will insert your new ID tag from SSI alone. However you could try to code a program that reads either the query string from your ID page that the SSI tag is inserted then manipulate the code to replace the SSI tag with whatever it is you want.

Now does that sound confusing. :eek: Just to reiterate.. all you want to do is put a counter on your cgi pages right?

digitalsea
06-13-03, 01:50 PM
Yes!!

Basically I want to include a Perl program that counts current visitors on my detail (CGI) pages.

I though I could use SSI to call the counter and then the detailed page on the same document, like

<! include virtual "path to counter">
<! include virtual "path to detail.cgi?ID= (this is where the hidden form would go) ">

then when you went to detailed.shtml?ID=whatever it will fill in the ssi ID part.

I'm guess this is more trouble then it really is...

Pearl
06-13-03, 04:08 PM
Okay.. I think I get what you mean now, although this method seems a little backwards to me? Essentially you would have a page:

detail.shtml?ID=XX
<BODY>
<!--#include virtual="counter"-->
<!--#include virtual="detail.cgi?ID=XX"-->
</BODY>

The above example doesn't make any sense (to me that is). This way, you can't define where your counter will go. It will just remain at the top? and if your counter outputs text that would look funny too. Not to mention that you would have to create .shtml pages for each ID that you have, when your cgi program already does this for you. Even if you use "echo var" I don't see a way that the ID can be inserted the way you're thinking.

Your best bet really.. is to create a subroutine for your counter... since you say it is only 4 lines of code. Insert this into one of "Links v2.0" modules, or where "Links" calls your header, create a global tag to use and then you can use it freely in your designs. Links probably has some "special" tags? something like %%HEADER%% or <<HEADER>> or something to help your design. You can create one of these yourself too. Something like:

sub mycounter {
counter stuff goes here....
$counter = "";
}

Search somewhere in your files where Links changes the output of the Header or Footer... You might be looking for something like this:

$header =~s/%%SOMETAG%%/$replacewith/;
You can add something small like this:

&mycounter;
$header =~s/%%COUNTER%%/$counter/;

Then you can insert %%COUNTER%% in your design, and it will almost be like SSI except it's coded internally. I'm not sure if we're on the same wave length here.. hope I'm getting somewhat closer to what you're trying to accomplish :D

digitalsea
06-13-03, 05:25 PM
I finally found where Links does the header of the CGI... it's actually in a completely different area (links_detailed.pl) ?!?

I think I can get it working, plus now I can customize the script even more!

i appreciate all your help... :D

SooP
08-23-04, 02:18 PM
Most webservers will pass on the query string to scripts in an SSI automatically.

For example:
http://www.blah.com/ssi.shtml?ID=xx

will pass the query string ?ID=xx to ANY scripts you have embedded in the page.

So, the SSI tags in ssi.shtml:
<!-- #exec cgi="cgi-bin/somescript.cgi" -->
<!-- #exec cgi="cgi-bin/anotherscript.cgi" -->

Become:
<!-- #exec cgi="cgi-bin/somescript.cgi?ID=xx" -->
<!-- #exec cgi="cgi-bin/anotherscript.cgi?ID=xx" -->

without any extra effort on your part.

If you have multiple embedded scripts, you might want to hardcode the querystring values that EACH script itself needs to take (in the scripts themselves) - since the query is appended to ALL embedded scripts.

As far as keeping state, perl scripts *kinda* don't have that ability... if you want a script to keep a value like a visitors counter over multiple instances, you will have to write/read the value to/from a file. Cookies would be useful too. I'll leave the setup to you.

Hope this helps.

§ooP