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

Hi all

I've been using Perl for some UNIX administration tasks, but only the very basics.

I want to learn more, and I have a web project coming up, so want to use Perl. I usually use PHP for web stuff, but I want to use Perl since I find it great for admin scripting tasks, so want to get better at it.

I've been recommended a template system like TT or Mason, but I really want to get my hands dirty with Perl without getting confused. Do people recommend I go this route? The first site coming up is only small and besides including files, I think I could do most of it with just Perl by itself.

While I'm posting, I have a technical question: I've searched the net and haven't found an equivalent function to PHP's include() - basically I need to embed a file that needs to be parsed by the Perl compiler - any ideas?



Replies are listed 'Best First'.
Re: Starting with Perl
by holli (Abbot) on Jul 26, 2005 at 06:38 UTC
    Well for learning Perl, there is a book which's name surprisingly is Learning Perl ;-) I'd start with that, followed by the camel.

    I would always use a Templating System, even for smaller projects. It saves you much debugging time. And you can learn them, especially TT, rather quickly.

    As you have some PHP experience, Embedding Perl in HTML with Mason could be interesting for you. I personally prefer TT, for which I have some printable docu in PDF format on my homepage.

    The answer to your last question:
    perldoc -f require
    Happy coding.

    holli, /regexed monk/
Re: Starting with Perl
by sk (Curate) on Jul 26, 2005 at 06:45 UTC
Re: Starting with Perl
by rnahi (Curate) on Jul 26, 2005 at 07:50 UTC
Re: Starting with Perl
by radiantmatrix (Parson) on Jul 26, 2005 at 13:58 UTC

    I highly recommend HTML::Template for small projects. It's still a templating system, and gives you the advantages of that, but it's also very light and easy to understand.

    Templates are nice because you can spend less time debugging HTML inside Perl. With a template, you can design your layout using HTML-editing tools, then replace variable chunks with template variables. If you're familiar with PHP, HTML::Template will not be difficult. Observe a small chunk of an HTML::Template template:

    <p>Hi, you are logged in as <!-- TMPL_VAR NAME=username -->, and you have <!-- TMPL_VAR NAME=new_message_count --> new messages.</p +>

    A snippet of Perl to populate the variables in the file (which we'll pretend is named template.html):

    use HTML::Template; my $template = new HTML::Template (filename=>'template.html'); $template->param( 'username' => $username, 'new_message_count' => get_new_count($username), ); $|=1; ##unbuffered output is a good idea with CGI print $template->output; ## sends to browser;

    As for an equivalent to PHP's include(), read the docs on require. One use of it is like this:

    require '';

    The above would load and process its contents.

    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re: Starting with Perl
by monarch (Priest) on Jul 26, 2005 at 13:34 UTC
    I did a stack of web administration tasks using the CGI module whilst learning Perl.

    I think it's a good approach! Now I use MASON which is much easier, personally, but it's very difficult to set up.. plus it is a nightmare to debug scripts using MASON.

    Doing the use CGI approach gives you the functionality you need (getting parameters from web forms), plus it allows you to debug your scripts from the command line if necessary.

    Most time, when spent learning a new language, is getting used to common pitfalls, and having access to a command line environment really assists in this way, because you can always just run perl -cwscript and see if there are any compilation warnings.

Re: Starting with Perl
by ^suntzu$ (Initiate) on Jul 26, 2005 at 14:20 UTC
    As one who is still learning Perl (and unlearning bad habits and techniques), I would strongly recommend using Template Toolkit for your project. Understand that TT IS Perl, not some "other language". Also, much of the syntax in TT reflects changes in the latest Perl incarnation, Perl 6. With TT you can get your hands dirty with databases, XML, web applications (including CGI) and more withouth getting lost in a myriad of details, all of which you will learn with experience wtih the language. I agree that "Learning Perl" is an excellent choice to learn the basics of the language but the "Camel" book is perhaps a bit in depth for newcomers. I would recommend moving on to "Learning Objects and References in Perl" as the logical follow up to "Learning Perl". Good luck with your endeavors and don't forget, the Perl community is one of your best sources for answers and help.
Re: Starting with Perl
by samizdat (Vicar) on Jul 26, 2005 at 12:41 UTC
    On the issue of learning Perl: It sounds like you have enough experience that you'll rapidly get beyond tutorials. I highly recommend that you get the entire O'Reilly series and keep them beside your computer: Learning Perl, Advanced Perl, Programming Perl, and Perl Cookbook. {gotta get Higher Order Perl myself!!!} I use almost every one just about every day.

    Once you know how to program, and it sounds like you do, syntax is something you look up. While perldoc and the other on-line references are great if you know what you're looking for, there are times when a linear scan is necessary and books can't be beat (yet!).

    Another thought on templating: it really depends on how well you know your web server and its configuration. You didn't say whether you're stuck with Doze or whether you're on a BSD or Linux box. I've been tweaking Apache since 1997, and it still took me several days to get everything the way I wanted it.
Re: Starting with Perl
by Your Mother (Archbishop) on Jul 26, 2005 at 14:57 UTC

    Lots of good comments already but one thing not really mentioned. Starting with CGI for your first deeper Perl project has serious dangers. You may already understand some of the issues from PHP but you really should read up on CGI security. I think I'd probably already written about 10 production CGIs at work before I wrote my first secure one. :(

    I did a Google search against thepen for cgi security and came back with lots of reading. Also be warned that CGI adds a layer of abstraction that makes learning in general more difficult and debugging specifically almost impossible for the uninitiated.

Re: Starting with Perl
by bjg (Acolyte) on Jul 26, 2005 at 09:08 UTC
    Thanks guys for the info.

    I have been reading some of the tutorials for the last two days, so I know where to go to start learning Perl, but just needed advice on what to do re: templates.

    I've looked at TT and Mason and they basically look like diff languages. I really want to use Perl though, so as to learn it better. See I do a lot more web projects than admin scripts, so want to actually use Perl, not some template.

    But thanks I got the info I needed. Will give require a go and go from there. I probably should have read properly, as it looked like it doesn't do what I wanted at first glance.

      Interesting point and it might be good to know how to include HTML within your Perl, but know there are 3 ways to do that:

      1. print statements (messy for a big HTML page)
      2. here docs
      3. CGI (lots of little 'codes' to learn)

      However, if you are going to be doing any extensive web stuff, don't discount the templating route too quickly. I use HTML::Template exclusively and love it. It's really *not* like learning a whole new language (coding conventions aren't that extensive and are pretty easy to learn—feels like HTML), and most importantly, you keep HTML and Perl (or any executable server side coding separate. This is expecially important if you are working with non-programming web page designers (like I do every day).

      In any case you will want to add CGI to your ammo belt. Take a look at a short course by the Monastery's very own Ovid.

      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        I would also recommend HTML::Template. To add a bit more check out some framework for CGI applications. Such as CGI::Application.

        HTML::Template + CGI::Application work well together - and there are many resources about both (CGI::App even has it's own Wiki with tutorials, articles FAQ ...etc)

        It wont get you too far away from "dirty Perl" - but it's a much better and cleaner approach. Not to mention it's much faster way to write code ... Actually I'm running whole web sites on it - and even under plain CGI it works reasonably well.

        Template toolkit might be another good option - but somehow I got the same feeling - it's simply not it. It's not Perl, it's not HTML. Maybe I'm wrong (probably I am since many people say it's good) as I still haven't had time to read book about it ...

      I don't think may people here will agree, but i recommend for your first web-app you don't use a templating system and just print HTML statements wherever you need them. This has 2 advantages:
      • You really see what happens right in the code
      • It gets so dirty so quickly that next time you'll want to use a templating system
      As for the inclusion of another perl file:
      You can use eval but the better way would be to create a perl module (.pm file) that you can then use.

        I second that idea. Do it without a templating system to get a strong viceral sense of why they're such a great thing. Then translate your project into whatever template system you choose. Both experiences, the raw creation and the translation from one system to another, will contribute to your mental toolbox.

        Be Appropriate && Follow Your Curiosity
      I agree that we'll all disagree on templating systems. :) I use HTML::Embperl (I'm using 1.3.6) extensively and find it very useful, particularly in regards to automatically building tables and forms from a database. It did take a while to get it set up because it really likes mod_perl and because you will want to determine which session management system to add to your Apache.

      I used to do a lot of PHP, but not any more. There are too many Perl-specific modules out there that are goldarned indispensible when you're doing anything more than just print <blah>ing a web page.
Re: Starting with Perl
by xorl (Deacon) on Jul 26, 2005 at 13:50 UTC
    I think you need to back up here just a little bit and ask what about this project requires you to use Perl or is more easily done with Perl? If your answer is I don't know, I just want to use Perl, then I think you should stick with PHP. Perl is extremely useful, but I have yet to see a web project where Perl was a better option than PHP.

    As for including files, I alwasy found that using mod_perl and ePerl (more info) worked better than just using perl from cgi-bin. You can simply include a file with Apache's SSI with this method. You of course can do it the way others have mentioned above.

Re: Starting with Perl
by stonecolddevin (Parson) on Jul 26, 2005 at 19:35 UTC
    For the PHP include() function, you can use CGI::SSI. It has an include() function exactly like the <--#INCLUDE file=...--> directive in SSI.