Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Different ways of including files in a CGI Script

by shirkdog_perl (Beadle)
on Feb 17, 2003 at 01:35 UTC ( #235845=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Fellow Monks, Because of my hosting site not support many wonderful modules (Such as HTML::* HTTP::* and LWP::*), I have been working on code that is compatible with Perl 5.006 (Sorry, at least I have fun at perlmonk.org). My question is, I figured out a way to include an html file as a string, then setting up a loop to print the entire thing, such as a header and footer for a web page:
while (<HEAD>) { print; }
However, as I hope some of the monks will point out, the filehandlers will cost performance. I have seen this as it loads. So what I am faced with is no mod_perl and no modules. However, I am trying to make this work for now until I can get my own server to host my domain. Once again I am most humble to the knowledge of the monks.

Replies are listed 'Best First'.
Re: Different ways of including files in a CGI Script
by Coruscate (Sexton) on Feb 17, 2003 at 01:42 UTC

    You've got the basic idea on how to open up a file and print out its contents to the client. I don't understand how HTTP:: or LWP:: modules would help you with this. I see that you named the file handle HEAD, so does this mean that the file only contains the HTTP headers to be sent out??

    Anyhow, you can do the following to print out the entire contents of a file to the client:

    #!/usr/bin/perl -w use strict; print "Content-type: text/html\n\n"; open HEAD, 'file.txt' or die "Could not open file.txt: $!"; print while <HEAD>; close HEAD;

    Hrmm... 5.006 does support warnings, strict, $! for error, and file handles, right? {grin}


    BTW, I meant to mention: if you're looking for a box to work on, read up jcwren's home node for information on getting an account on the box he so generously provides for members of perlmonks. I'm not sure he'd appreciate an actual business being run from it, but if all you need is a personal account to test with, I'm sure he'd be happy to supply you with one :)


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

      Well I mentioned the modules to give an overview of the situation. Actually the HEAD file is just HTML past the first body tag. CGI.pm takes care of the header. I also have $! in place for error handling. Also I have already set up a site with jcwren. But as you noted about running a business from the server, I am looking to do more. What is funny is JCWREN's server offers more for free :-). I have thought about ways of uploading ths source code of modules to my hosting site and using the modules that way (also an idea from a fellow monk). I will be looking into that this week.

        Rosehosting have pushed some advertising Perlmonks way so deserve a plug. I have had a VS with them for 6 months and have been very impressed with the bandwidth and uptime. You can get a virtual server with full SSH root access so you can install/do more or less whatever you want for $29US a month. The bandwith they have is excellent and the average server load rarely exceeds 5% average. They take about a day to set up your server. You get a pretty functional version of Redhat 7.3, sendmail, Apache 1.3.23 http and https mod_perl (but you really need to upgrade that to 1.3.27 for security) + perl 5.6.1 They advertise MYSQL and postgres (these were not installed) but build easily from source.

        They have gone the M$ way and have a lot of services running by default that you will want to kill of but besides that it is a pretty fair deal.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Different ways of including files in a CGI Script
by tachyon (Chancellor) on Feb 17, 2003 at 03:20 UTC

    For modules that are pure Perl you can often get away with a cut and paste into your code. HTML::Template for example is a single module with no dependencies so you can do this:

    # don't need to use the module (it's pre loaded ;-) # use HTML::Template; my $template = HTML::Template->new(filename => 'test.tmpl'); # blah blah # now paste the entire HTML template code into your script package HTML::Template; $HTML::Template::VERSION = '2.6';

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks tachyon, It looks like it will work if I maintain the directory structure HTML/Template.pm I did the test script from this horrible windows machine and it worked, so I should be to put this on the server, thanks for your help.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2023-02-06 09:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?