Top

About code generators

October 20, 2008  

Perlsphere: About code generators

Today someone on IRC brought up a good point, the code currently
generated by Mojolicious helpers demonstrates just features and not best
practices.

# This is a templateless action
sub test {
my ($self, $c) = @_;

# Response object
my $res = $c->res;

# Code
$res->code(200);

# Headers
$res->headers->content_type(’text/html’);

# Content
my $url = $c->url_for;
$url->path->parse(’/index.html’);
$res->body(qq/Forward to a static document.<\/a>/);
}

This snippet generated by Mojolicious::Script::Generate::App shows the
possibilities of manipulating the response object directly, in a way
nobody would ever do in a real application.
I never thought about the possibility of people misinterpreting the
example as “the right way” to do something.

Maybe it’s a good idea to keep misleading examples like this one buried
in a manual, even though people won’t be able to see the full potential
of the API at first glance.
For the next release i might try a new example app using only best
practices and see how it works out.

Read more from Perlsphere.

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!





Bottom