in reply to How to use Perl in web pages?

The simplest of all ways is to use the CGI module and just print out your HTML:

#!/usr/bin/perl -w use strict; use CGI; my $q = CGI->new; my $name = $q->param('name'); print $q->header(); # just ignore the man behind the curtain print '<html><title>First page</title><body>'; if (defined $name) { print "<h1>Hello $name</h1>"; } else { print "<h1>Please enter your name</h1><form action=''><input name= +"name" type="text"></form>"; }; print '</html>';

In short time though, you will want to move to one of the templating systems. If you feel at home with the code-intermingled-with-HTML way of PHP, then Mason might be your thing. If you want to get away as far as possible from that, take a look at Petal, HTML::Seamstress and HTML::Template. The Template Toolkit has its own powerful programming language built in, so you can get more creative with these templates, but, again, beware of the danger of mixing code and HTML too much.

Replies are listed 'Best First'.
Re^2: How to use Perl in web pages?
by liverpole (Monsignor) on Jun 07, 2009 at 02:34 UTC
    Corion,

    Good answer; it neatly addresses the "get started quickly" feel which one gets from the OP's question.

    You'll get an error, though, with this line:

    print "<h1>Please enter your name</h1><form action=''> <input name="name" type="text"></form>";

    because of the embedded quotes.

    My favorite method for overcoming this problem (especially in CGI programming) has been to use the qq operator, eg.:

    print qq[<h1>Please enter your name</h1><form action=''> <input name="name" type="text"></form>];

    Stylistically, you'd also probably want to close the <body> tag too, with </body>, though of course it isn't a necessity.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re^2: How to use Perl in web pages?
by kitsune (Acolyte) on Jun 06, 2009 at 22:24 UTC

    Wow, that seems like quite a lot of options. To someone who is new at this, which one would you recommend trying first? I do not need it to be similar to the PHP way of embedding in HTML. It does get pretty messy that way.

      Based on my own experience, I would agree with everything hangon said, and also recommend that you first get yourself oriented with a simpler script that uses just the CGI module, as demonstrated by Corion. As you get comfortable with the basics of how things work, start using HTML::Template. You'll appreciate how it improves things relative to using only CGI.

      And then when that's working for you -- and if you want your web app to manage a wider range of activity with a minimal amount of coding, get acquainted with CGI::Application. Getting started with it is pretty easy (once you understand CGI and HTML::Template), and it really helps for keeping the code simple and manageable as the app gets bigger.

      Spend some time skimming and probing the full length of the documentation that comes with each module, and then refer back to that as needed when writing code. Each manual has a lot of information (the size of the manuals might be a bit scary at first), and a lot of helpful and useful examples. When you understand the details provided in the manual about a particular function, it's a good bet that you really do know what you're doing.

      I do not need it to be similar to the PHP way of embedding in HTML. It does get pretty messy that way.

      In the Perl world, the mantra is There is more than one way to do it. This often leaves newbies in the position of having to make difficult decisions. To avoid embedding code in the PHP style, you may want to start out with CGI::Application and HTML::Template. There are tutorials on this site will help you: A Tutorial for CGI::Application, HTML::Template Tutorial and Using HTML::Template. As you gain experience, you may want to look at other options, but you can do a lot with these modules.