Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Perl and HTML

by doo_the_dew (Initiate)
on Dec 01, 2012 at 23:40 UTC ( #1006658=perlquestion: print w/replies, xml ) Need Help??
doo_the_dew has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to build a website using perl. I have found that my I cut and past the first part of my perl generated html on every page of my website. Is there a way to put all of that stuff (starting html tag, head and title tags, etc.. in a file and then say include this file at the start of every page. (same with footer info close html tag copyright info, etc...) Also, if you have a code example of somebody who did this correctly using perl, that would be nice. Thanks

Replies are listed 'Best First'.
Re: Perl and HTML
by davido (Archbishop) on Dec 02, 2012 at 02:28 UTC

    It sounds like you're looking for a templating system. HTML::Template will allow you to set up templates that are completed via Perl. You can even use HTML::Template to "include" templates from other files. That allows you to set up a framework template, a header template, a footer template, and so on, just pulling them all together as appropriate.

    Eventually you may want to look at a more full featured framework such as Mojolicious (probably starting with a Mojolicious::Lite application). That provides templating, and a whole lot more. It helps to promote a separation of concerns (Model / View / Controller), which has become a fairly standard way of doing things, and certainly a "best practice."


Re: Perl and HTML
by 2teez (Vicar) on Dec 02, 2012 at 02:36 UTC
Re: Perl and HTML
by marquezc329 (Scribe) on Dec 02, 2012 at 05:39 UTC

    +1 for HTML::Template. I first started doing web/design related perl about a week ago and I've been using this module a ton. It's a good starting point for learning how to generate and interact with web pages with Perl.

    I would also recommend taking a look at JQuery for easy access to JQuery functionality.

Re: Perl and HTML
by monsoon (Pilgrim) on Dec 02, 2012 at 06:31 UTC

      And Mason 2 which is HTML::Mason rewritten with Moose and other modern goodies.

Re: Perl and HTML
by space_monk (Chaplain) on Dec 02, 2012 at 08:54 UTC

    The other comments here are totally correct in stating there are lots of templating modules that do the job, however if you wanted to "roll your own" there is nothing preventing you creating your own subroutine/methods to do exactly as you stated.

    Whilst not the professional way, there is no reason why you cannot create your own methods such as output_header and output_footer in a .pm file and then simply use or require that module in your other files.

    Whichever way you do it, good luck!

    A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Perl and HTML
by golux (Hermit) on Dec 02, 2012 at 23:47 UTC
    Hi doo_the_dew,

    For anything complex, Template::Toolkit is very useful, as has already been mentioned.

    If it's something simple you can of course just use a perl CGI script as long as your server is configured to support it. On my server, as an example, I created these 3 files:

    # head.html <head style="background:cyan"> <style type="text/css"> body { background: cyan; } </style> </head>


    # body.html <body> <center> <h1>Simple CGI Example for <a href=" +1006655">doo_the_dew</a> </center> <hr>
    and the file "index.cgi" (make sure it is executable if you're on a Unix/Linux server of course):
    #!/usr/bin/perl -w # Libraries use strict; use warnings; use CGI::Carp qw{ fatalsToBrowser }; use IO::File; # IMPORTANT -- CGI scripts need to provide the header print "Content-type: text/html\n\n"; # Main program insert_file("head.html"); # Insert common header insert_file("body.html"); # Insert common body start # # YOUR CODE HERE # # Subroutines sub insert_file { my ($fn) = @_; my $fh = new IO::File; open($fh, "<", $fn) or die "Can't read file '$fn' ($!)\n"; foreach (<$fh>) { print; } close $fh; }

    Hopefully that gives you something you can get up-and-running quickly. Once you need more power, you can always add Template::Toolkit, or even create your own HTML-generating libraries, as you desire.

    say  substr+lc crypt(qw $i3 SI$),4,5

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006658]
Approved by Athanasius
[erix]: but surely Hinault was Breton not french?
[erix]: s/$/ :)/
[ovedpo15]: regex question - how can I delete everything until string "here" (including "here") for all the string (not until the first pattern)? for example a/here/b/here/c will return "/c"
[LanX]: Eily is Breton?
[erix]: 'spas

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2018-07-17 13:24 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (366 votes). Check out past polls.