From: "Mark Haney" <ma...@abemblem.com>
Subject: Re: Template toolkit issue [SOLVED]


On 05/02/2012 03:53 PM, Octavian Rasnita wrote:


Perl is a programming language, not a language which is used only for
web programming, so why should you think that Template Toolkit includes
CGI? Nowadays CGI is very seldom used, so others might be thinking that
it should include PSGI or another interface for web apps...

Of course, Template Toolkit is mostly used for creating HTML content,
but it can be used for generating any other type of content, so it
doesn't have any relation with HTML either.

Octavian






I'm really quite intrigued by the assertion that CGI is very seldom used. Based on this entire thread, the Template Toolkit is used entirely for building websites in perl. And unless I'm mistaken, what other way than CGI could perl possibly be used in this manner? Granted, I'm no expert on perl and web programming, I think I've made that clear.


OK, sorry for not beeing clear enough. I'll try again.

As you already read and probably knew, CGI has a double meaning.
First, it is an interface which is used by all programming languages for the web in order to allow the browsers to submit forms to the web servers.
And second, it is a Perl module that does a few things:
- parses the query strings from URLS (those ?var1=val1&var2=val2)
- parses the content of POST requests (for getting the POSTed data, or upload files...)
- generate HTTP headers
- create HTML elements by using many functions that have the same name as the HTML elements.

The module CGI is a great module which does many helpful things, but it is very seldom used these days, and even when it is used, not everybody likes all its features. The most important feature of this module is the fact that it can parse the query strings from URLS and the POSTed content, but many programmers don't like to use its methods to generate HTTP headers, and prefer to just print them directly, like:

print "Content-Type: text/html\n\n";

Even more programmers don't like to use its features that allow generating HTML elements with CGI.pm's methods, but prefer to use a templating system like Template-Toolkit to do that, or even just print the HTML content as a block of text (which is ugly).

But fewer and fewer sites use CGI.pm, because when using the standard Common Gateway Interface, after the web server receives the request, it creates a new process, and run Perl which compiles the CGI script and executes it, and then return the result, after which the process is closed. And these steps are done again and again on each request, so it works very slow.

There are better alternatives.

Instead of loading the script and compile it on each request, the script can be loaded once, compiled, and the compiled version kept available in the memory, and on subsequent requests the compiled program should just receive the requests and return the responses which works much faster.
In order to do that, you need to use either mod_perl or FastCGI.

mod_perl is an Apache module, and Apache is a huge web server that consumes a lot of resources, but this Perl module can work only under Apache, so it is not very flexible. If Apache is OK for you, mod_perl is great but it is more complicated to set it up and to work with it than by using CGI.pm. FastCGI is an interface similar with mod_perl but which works with more web servers including Apache, so it is more flexible, but it might be a little more complicated to set up than mod_perl, but this depends on each one's experience.

If your site is targetted to a very small number of users and if you don't require a very high speed, then CGI.pm is a good solution if the page is not very complicated. If the page contains very many elements, and if you need to use different modules like a templating system, then the speed of page rendering will decrease even more, because it is obviously that a page that just prints an HTML content directly is displayed faster than one that needs to load a template, and let that template generate the content.

This is why most of the times when a page is complex enough and uses a templating system, and accesses a database, and maybe use a form processor like HTML::FormFu and maybe other modules, it also uses a persistent environment that keeps the code compiled in memory and also maintain a permanent connection to the database, so it shouldn't spend time connecting to the database on each request.

Usually those who need to create a complex site also need to use a few tools for creating that site, and don't reinvent the wheel and do it from the scratch. And those tools are usually a web framework, a templating system, an ORM, a form processor and different other modules depending on their needs. And all these tools are high-level code which make the site work slower than when using just low-level Perl code, but the productivity increases very much, and the speed of the site can be improved in other ways, (like using a web proxy in front of more servers that run that site, or using cache at different levels...).

The most used and most powerful Perl web framework is Catalyst, but there are more other frameworks you can find on CPAN if you'll need. The most used ORM is DBIx::Class but you can also find other ORMS if you need. The most elegant and used templating system is Template-Toolkit but again, there are many other templating systems you can use, depending on your needs and requirements. And you can find a lot more information in the documentation of each module on CPAN.

And there is a pretty new interface named PSGI which was ported from Python's WSGI, and I could tell you some things about it, because you said that you want to know what should be done and not only what shouldn't, but probably it won't be very helpful for your current project so you can search for it on CPAN if you really want to find more information.



But, it appears that I'm getting contradictory messages here, none of which make me feel particularly comfortable with continuing to use perl for my project.


I don't see which were the contradictory messages.


I'm also not used to being talked down to like a child. Why would I think the Template Toolkit includes CGI? Well, if my OP had been read, you would have seen that /in the example/ in the documentation, no mention of including CGI in the code was made.

You said:
"I got it working now. I was under the assumption that the TEMPLATE
module included CGI and that adding CGI and going through the process of
generating the headers and specifying the beginning and ending of the
html was a given."

And I just told you that no, nothing is a given in Perl, unless you request it. Do you want to use CGI? Then use the CGI.pm module. Do you want to use a templating system to generate the content of the web page? Then use the templating system you choose. Do you want to be better informed about a certain module like Templating-Toolkit or Catalyst, or DBIx::Class, or HTML::FormFu, or Moose, then subscribe on the mailing list of that module, because the popular Perl modules have their own mailing list. Or you can ask on this list, no problem, but remember that Perl is not a language that can be used only for web programming, and that the web related things are not a given in Perl like in PHP, but you need to use the Perl modules you want to use.



But then, I also made it clear that the documentation is worse than useless. This was less an assumption on my part than simple ignorance. I'm not certain how much clearer I can be when I say I'm /new/ to the perl/web game. Is it so hard 1) to understand and 2) to consider the audience when crafting a reply that might be useful and not derogatory?

The problem is that you are asking how to do in Perl some things, in the way you are used to do them in PHP, and fortunately this is not possible, so you should be asking how to do some things, not how to use some Perl tools to do the things like in PHP.

Personally, the more replies I get on this list the less I'm inclined to use perl for this, since I'm not getting as much help as I expected in a /beginners/ list, but also I'm being given the impression that perl isn't used the way I'm intending to use it.

Yes, I also have that impression. :-)
If you want to find a quick answer about how to do in Perl a program that works like one in PHP, then you won't find that answer here. You may find it elsewhere because in Perl there are possible very many things, but the people on this list tend to give only good advices, and using a PHP-style programming it might not be one of them.


In other words, I'm just about at the 'I give up' stage and moving everything to a platform that has a good helpful community. My apologies to those that have helped. It's been greatly appreciated, but I'm about as frustrated as I can possibly be.


But why? I understood that you managed to make your program work because I have seen "solved" in the subject of this thread....

If you are frustrated because I told you that CGI.pm is seldom used, then don't bother because each one has its own preferences. Some people still use CGI.pm because they don't require a very high speed. Some use a web framework and an ORM because they need to do very many things very fast and this increases the productivity, but others don't like them because they require a steap learning curve. Some of them use mod_perl because they need a very fast program and don't use a web framework that decreases the speed because the speed is the most important for them and not the productivity. Some use Plack/PSGI and one of the newer web servers that use this interface, like Starman or Starlet, which are very fast, elegant and easy to use or other web servers like nginx or lighttpd, some use Template-Toolkit because it is very elegant and powerful and separates the Perl code and the code used for formatting the output, but others like Mason. Some like HTML::FormFu form processor and others like HTML::FormHandler and each one have their preferences, so you will ever get "contradictory" messages because in Perl there is no just a single good way, but many good ways, depending on your needs.

In other programming languages a certain web framework usually works with a single templating system and a single ORM, and another web framework works with another templating system and another ORM, so you will be forced to follow the single possible way, but in Perl, with Catalyst and Dancer and Mojolicious and probably with other frameworks you can choose to use the templating system you like, the ORM you like, the form processor you like if you need one and so on.

But these are very advanced things compared with using CGI.pm for creating a simple program and I told you about them to see how can be used Perl in the right way. Using CGI.pm for creating a kind of PHP parser is not a good way that should be recommended to newbies.
IMHO I think you should keep using PHP.

There is no the best programming language, and depending on each one's knowledge and needs, other languages like Python or Ruby or even PHP could be better.

Octavian


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to