From: "Parag Kalra" <paragka...@gmail.com>
Hello All,
I have only interacted with PHP/MySQL to design a website.
I wanted to know if it is possible to use Perl instead of PHP (and I am
pretty sure it should be possible :) ). But my main concern is what are
the
main Pros & Cons of using Perl instead of PHP.
Perl is usually used and prefered by those that need to do more complex apps
that don't need just the core language (php or perl) and MySQL.
If you said that you used PHP and MySQL, I think you have created low-level
apps that doesn't use a templating system like Smarty, nor an ORM, or a form
processor, or a framework, although there are a few for PHP also.
Well, in most cases, Perl is not recommended for creating web apps if you
want to create low-level applications that use just perl and possibly MySQL.
It is recommended to use Perl (in my opinion) in the following combinations
(for web apps, because perl can be also used for creating desktop apps):
Use a web framework like Catalyst, and Template-Toolkit as a templating
language, and DBIx::Class as an ORM, and HTML::FormFu as a form processor
(but unlike other frameworks, Catalyst is not strict and offers you the
possibility of not using a templating system at all, or using another one
like HTML::Template or Mason, and using no ORM or another orm than
DBIx::Class or another form processor than HTML::FormFu if you need such a
thing).
The disadvantage of using higher level tools like these is that it would
take you a long time for learning to use them, and after you've created your
programs you will see that they run much slower than a web app that use only
low-level code.
The advantage is that it would take you much less time to create the
applications, and they will be very easy to maintain. They would look much
nicer than a low-level code.
For example, here is a Catalyst subroutine:
package MyApp::Controller::Book;
use base 'Catalyst::Controller::HTML::FormFu';
sub edit : Local : FormConfig {
my ($self, $c, $book_id) = @_;
my $book = $c->model('DB::Book')->find($book_id);
my $form = $c->stash->{form};
if ($form->submitted_and_valid) {
$form->model->update($book);
$c->flash(message => 'The book was inserted successfully in the
database.');
$c->response->redirect($c->uri_for('/'));
}
else {
$form->model->default_values($book);
}
}
That's all.
package MyApp::Controller::Book;
This is the definition of the Catalyst controller (which is a Perl module).
It is important for establishing the URL that will be used to access this
code. If your domain name is www.foo.com, you could access it with
http://www.foo.com/book/edit/123
where 123 is the $book_id for the book you want to edit the data for.
use base 'Catalyst::Controller::HTML::FormFu';
This module is one that inherits from Catalyst::Controller::HTML::FormFu (if
you want to use HTML::FormFu form processor in your app).
sub edit : Local : FormConfig {
"Local" is a type of URL dispatching that can be used in Catalyst apps, but
there are more ways, some of them more complicated and useful.
FormConfig is another subroutine attribute that helps you because you don't
need to manually write a few more lines of code that loads and process the
configuration file with the form definition.
my ($self, $c, $book_id) = @_;
Well, $self and $c are the first 2 parameters which are sent to every
Catalyst controller. You will use $c for accessing the request data, the
response, cookies, different Catalyst plugins, models, views, etc. After
these 2 parameters, you'll get the rest of the parameters sent in the URL.
my $book = $c->model('DB::Book')->find($book_id);
With this line you get a DBIx::Class object that contains information about
the book you found in the database with the id $book_id.
You will be able to get information about this book using methods with the
same name as your fields from the database, for example $book->title,
$book->author, or you would be able to delete this book from the database
using $book->delete...
Depending on your database definition, you might be able to get information
about other tables in the database, using code like:
$author_age = $book->author->age;
my $form = $c->stash->{form};
This line gets the $form object that was placed automaticly in the Catalyst
stash when FormConfig was previously used a few lines above.
The form definition is by default placed in a configuration file
root/forms/book/edit.conf in the directory of this web app.
That configuration file can be in any format like Apache style configuration
(httpd.conf), simple perl data structure (hashe of hashes and arrays), .ini
configuration files, .yaml, .xml, .json... whatever you like.
and it contains information about the fields of the form, constraints that
they should respect, inflators, deflators, validators, filters... It could
also define complex types of fields that create more html elements when the
form is generated.
if ($form->submitted_and_valid) {
This code is executed only if the form was submitted and if it is valid
(respects the constraints and validators - for example you can define a
validator that verifies that there are no dupplicates in the database).
$form->model->update($book);
The form definition has a configuration that let it know which is its
DBIx::Class resultset (which is the table it should add to or update to), so
after form validation it gets the data from the form and update the $book
object with it.
$c->flash(message => 'The book was inserted successfully in the
database.');
This message is stored in the flash, so it could be printed after the next
page is displayed.
$c->response->redirect($c->uri_for('/'));
After the book data was updated, the app redirects and prints the main page
(or another one). The previously set message is also printed in that page in
the place where the Template-Toolkit directive [% message %] is.
}
else {
This code is executed if the form was not submitted. If the form was
submitted and if it has errors, it is re-printed with the user-typed text
and with the error warnings specified for each field that has errors. Those
error messages can be localized in the languages you want and as any
localization in Catalyst, they can be kept in po/mo files, in perl modules,
or in the database.
$form->model->default_values($book);
The form is displayed and filled with the data from $book object.
}
}
Another recommended way for using perl instead of PHP is when you want to
create very low-level apps that run very fast or if you want to access
Apache's internals. In that case you just need to use mod_perl. Regardless
the language you use, whenever a higher level code is used, it will run
slower but the code will be much nicer and easy to maintain and when you use
a low-level code, it will run faster but it wouldn't be so nice and it would
be also harder to maintain.
Octavian
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/