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

maybe i am recalling back to my php book or maybe i've been editing my named.conf a bit too frequently, but if i want to simply pull a snippet of code into my current script, is the best manner of doing so through:

require 'snippet.lib';

perhaps old habits die hard, but for some reason, it just feels like this should be include or something.

actually, let me give my case in point and this could stem in a different direction. i have a cgi script that generates html content (i am not using use CGI; i am just not there yet). anyway, my outside snippet.lib is really just a portion of html code that will change frequently and i want to be able to let the html dudes make their changes without freaking out over seeing so much perl code. so its going to look like:

print<<TOC; <table> <tr> <td>blah</td> </tr> </table> TOC

not really a whole lot of perl here, but just a way to keep my stuff mine and their stuff theirs.
so am i going down a road taken by all beginners trying to be crafty that ends in a dark alley? or am i getting in done in a non-optimal but okay way. suggestions are more than welcome, but CGI is just out of my reach right now guys. gimme another week of soaking it all in.

humbly -c

Replies are listed 'Best First'.
Re: using 'require' to insert code
by HyperZonk (Friar) on Jul 23, 2001 at 06:48 UTC
    If you need to keep the HTML coders happy by providing an external file that is as HTML-only as possible, perhaps you should consider just having that file in pure HTML, reading it in, and printing it. Like:
    my @html; open(HTML,"<$TheHtmlFile"); @html = <HTML>; close HTML; print @html;
    This presumes that you have already emitted the required HTTP header, of course, and will also work perfectly well when you move over to CGI.

    Update: And note syn's reply below, particularly if the HTML file is large.

    -HZ
      Well, I have something to say to main poster and a reply to HZ.. To the main poster, start using CGI.pm it's really just as simple as:
      #!/usr/bin/perl -wT use CGI; my $cgi = new CGI; print $cgi->header;
      And then just throw in CGI specific code as you learn it.. but you don't *need* any more lines than that, and to know that you can grab POST and GET data by calling $cgi->param();
      As for the sample above.. there's no reason to put possibly large html files into a memory consuming array, better to just read them in and spit them out (unless of course the data needs to be manipulated):
      open(HTML, "<$htmlfile") || die "ouch $!"; print <HTML>; close(HTML);
      -Syn0
Re: using 'require' to insert code
by wine (Scribe) on Jul 23, 2001 at 12:48 UTC

    If you want to keep your stuff yours and their stuff theirs, you might want to take a look at HTML::Template.

    CGI gives you the opportunity to create HTML though a kind of abstract interface, which is has definitely its advantages, but it's not intended to seperate code and HTML.