Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

2 script in a page

by Anonymous Monk
on Nov 03, 2001 at 02:43 UTC ( [id://122940]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a page with a side menu and a main content, and i dont want to use frame for that. The content of the side menu depends on whether the user is a member or not. My question is: can i run 2 CGI scripts to display a page (the side menu and the main content)?? The first script is to display the side menu (with the authorization checks in it) and the other one is to display the main content. Thnks in adv

Replies are listed 'Best First'.
Re: 2 script in a page
by jwest (Friar) on Nov 03, 2001 at 03:06 UTC
    The short answer is no, you can't (easily) use two CGI's to generate the content for one page without frames. With that said...

    One way to go about this would be to develop three scripts. The first of which generates the appropriate HTML for your menu code. The second of which generates the HTML for the main content. The third script is the actual CGI that includes the HTML generated by calls to the previous two scripts, passing the appropriate parameters to them.

    CGI::FastTemplate, amongst many other modules on the CPAN, can aid you greatly in doing this sort of thing, and might be worth looking into.

    Of course, there are many other ways, most of them better, of doing this same thing. There are also several variants on this theme alone. You might consider using modules to generate the HTML code, for example. As always, TIMTOWTDI.

    Hope this helps!

    --jwest
    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
Re (tilly) 1: 2 script in a page
by tilly (Archbishop) on Nov 03, 2001 at 09:41 UTC
    What is a script? A body of Perl code that executes straight through based on some global data (eg @ARGV), right?

    What is a function? A body of Perl code that executes straight through, generally based on private data passed in through @_, right?

    While there are some technical differences in how you write them, there is no real difference in what you can do with a script and a function. However what you have written to be called as a function can trivially be rewritten as a script that just calls that function. The converse is not true, what is written as a script may take more work to write as a function.

    The upshot of this is that when you want to encapsulate a bunch of functionality, you are generally better off thinking about making it a function than making it a script. And you are better off reaching for functions than scripts.

    If you apply that principle in this case, your problem resolves itself. Can you write a program that calls two functions, one to produce the main content and another to produce the side menu? The answer is that you can. And if you put the function for the side menu into a module, you can share that content across many pages very easily.

    In fact that is basically how this site works. If you look at the page, you will see that it is broken up into various "nodelets". Filling in each nodelet is just a question of calling a function. And if you register for the site you will find out that what nodelets you have is itself configurable - and behind the scenes just represents a series of choices about which functions to call.

    But the principle remains. Whenever possible, encapsulate functionality in a function, not a script. Do that consistently and you will find that a lot of technical problems resolve themselves very directly and naturally, in fact often so smoothly that you don't even notice that you might have had a problem if you had done things a different way.

Re: 2 script in a page
by Ven'Tatsu (Deacon) on Nov 03, 2001 at 04:36 UTC
    You should be able to call another script with system and it's output should be sent to the client as part of the page. Just make sure you flush your buffers before use system.
    #!/usr/bin/perl $| = 1; print "Content-Type: text/html\n\n"; print "This is that start of the page<br>\n<br>\n<pre>\n"; system(qq(echo "This is from a system()"; env)); print "</pre>\nThis is back in the script<br>\n";
Re: 2 script in a page
by Fastolfe (Vicar) on Nov 03, 2001 at 05:48 UTC
    You might consider using SSI for this.
    <html> <body> <div style="float: left" class="sidebar"> <!--#include virtual="/cgi-bin/build-sidebar" --> </div> <!--#include virtual="/cgi-bin/build-body" --> </body></html>
    The advantage of this approach is that you aren't limiting yourself to using CGI to generate your page body. You could put a static piece of HTML content in there just as easily.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://122940]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-19 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found