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

Hi, I have recently learnt perl, I've written scripts to go through hashs, arrays and manipulate data. I now need to write a webpage with perl and am getting stuck. I don't know where to start. I need to build a webpage that will load a CSV, which I can convert into a 2D array. Ideally the same page would allow me to click "next" and "previous", displaying each array and allowing forms for updating each record if required. I know how to do all this as a script, although not sure how to make it a webpage. Could one of the monks point me in the right direction, let me know what to google etc. Cheers Mark

Replies are listed 'Best First'.
Re: Building a webpage with Perl
by rodd (Scribe) on Apr 08, 2011 at 07:30 UTC
    There are many ways to build web apps with Perl nowadays. To me, it all depends where you're going to deploy your app, what does your server provider offer, what you're load is going to be, etc. Start with the 3 shiniest Perl web frameworks right now: Catalyst, Dancer and Mojolicious. All 3 can accommodate the tiniest app, but can grow with it in case you're app skyrockets in functionality.

    For instance, a simple Dancer program as such could get you started (from Dancer::Tutorial):

    # myapp.pl use Dancer; get '/' => sub { return 'Hello World!'; }; start;
    Install Dancer if you haven't done so already:
    $ cpan Dancer
    Then just fire off the built-in server from the command line:
    $ perl myapp.pl
    And take your browser to http://localhost:3000.

    For a full blown application with templates and configuration files, use the Dancer app starter:

    $ dancer -a myapp
Re: Building a webpage with Perl
by marto (Cardinal) on Apr 08, 2011 at 09:14 UTC
      It's certainly good to learn the basics but I think this tutorial is a bit dated now.

        IMHO it's important to understand the fundamentals of what's involved, such as getting basic scripts running, security concerns etc.

        Your post mentions frameworks and database interfaces (which may be overkill depending on what the exact requirements are), while these are great tools to use threats such as SQL injection and XSS are issues which developers should be aware of. IMHO Ovid's guide is a great introduction to CGI, user input and security (while also providing links to further reading). Lest we forget Little Bobby Tables or the recent lizamoon fiasco.

Re: Building a webpage with Perl
by ww (Archbishop) on Apr 08, 2011 at 12:33 UTC
    And while you're following up (that is, "reading and understanding") on marto's on-target response, I would suggest you also get yourself an elementary understanding of html 4.01 and CSS.

    Even using CGI you're going to need a fundamental understanding of html; without it, you can master CGI and still have no clue. Possible sources include: www.w3schools.com/html/, http://www.goer.org/HTML/introduction/diving_in/ and many others.

Re: Building a webpage with Perl
by tospo (Hermit) on Apr 08, 2011 at 08:48 UTC
    The frist thing I would look into is using a database backend instead of your CSV file. From what you describe, a simple flat file will quickly become a liability when it comes to searching and updating the data. Don't know if you are familiar with SQL already - if not, I'd check out open source solutions such as MySQL or PostgreSQL. These are all well supported in Perl, using (by now rather old-fashioned) Perl DBI or the more moden DBIx::Class (or others).
    The old-fashioned way of making a dynamic web site was Perl CGI but I would not recommend doing this anymore. Use one of the modern web frame works. Personally, I am using Catalyst and I have used CGI::Application in the past. Both are good. Catalyst comes with a steeper learning curve, so CGI::Application might be the faster way to get you off the ground.
    The thing about modern frameworks, is that they follow a so-called "Model-View-Controller" design and it is worth learning a bit about the concept first. In brief: the model contains all the "business logic" of the web app - in your case that would be what you have in your existing scripts. The Controller is the bit of the app that takes user input, then asks the model to do something with it and finally asks the View to render something - usually a html web page.
    Using such a framework means that your existing back-end (the scripts you have written) basically stay as they are and you just write some additional code to interact with it and present the results.
    The only thing you might have to do to your existng code - if you haven't already done this - is to make them proper modules that can then be "use"'ed in your web app with a clear interface to keep the Model code nicely separated from the rest of the web app.
Re: Building a webpage with Perl
by wfsp (Abbot) on Apr 11, 2011 at 11:23 UTC
    ...not sure how to make it a webpage.
    I would recommend having a look at HTML::Template. I like the idea of not having any HTML in my scripts but, as you may guess, in a template. The docs are very good and you should be able to pick it up fairly easily. If you have a reasonable grasp of arrays and hashes, as you indicate, that's a plus. Perhaps two simple examples might best demonstrate the idea.

    The first one creates a web page with a message.

    #!/usr/bin/perl use v5.12.2; use warnings; use strict; use HTML::Template; my $t = HTML::Template->new( filename => q{my_template_msg.html}, ); my $msg = q{Hello World}; $t->param(message => $msg); my $output = $t->output; say $output;
    the template (my_template_msg.html)
    <html> <head> <title>my template message</title> </head> <body> <p><TMPL_VAR message></p> </body> </html>
    and the output
    <html> <head> <title>my template message</title> </head> <body> <p>Hello World</p> </body> </html>
    The second demonstrates displaying a list (something you would be interested in)
    #!/usr/bin/perl use v5.12.2; use warnings; use strict; use HTML::Template; my $t = HTML::Template->new( filename => q{my_template_list.html}, ); my @colours = qw{red orange yellow green blue indigo violet}; # build an array of hashes (AoH) my @colour_params; for my $colour (@colours){ push @colour_params, {colour => $colour}; } $t->param(colours => \@colour_params); my $output = $t->output; say $output;
    The template (my_template_list.html)
    <html> <head> <title>my template list</title> </head> <body> <ul> <TMPL_LOOP colours> <li><TMPL_VAR colour></li> </TMPL_LOOP> </ul> </body> </html>
    and the output (whitespace trimmed for brevity)
    <html> <head> <title>my template list</title> </head> <body> <ul> <li>red</li> <li>orange</li> <li>yellow</li> <li>green</li> <li>blue</li> <li>indigo</li> <li>violet</li> </ul> </body> </html>
    The benifits I see of this approach is that your Perl script still looks like a Perl script and your HTML still looks like HTML. Mixing them up, imo, always looks like a mess.

    Get these working (holler if you get stuck) and put them somewhere safe. When you're developing something more complex you'll have something to fall back on that works which, for me at least, is an aide to restoring sanity. :-)

    HTML::Template doesn't come with perl, you'll have to install it. There are plenty of posts and tutorials on that if you're not familiar with it. Again, shout if you hit a snag.

    There are also modules that will help you with the previous/next buttons. I use the, imo, very excellent Data::Page. Perhaps another time.

    Best of luck and let us know how you get on.