Kill Perl

| No Comments | No TrackBacks
I was exploring the other day the intranet of the corporation that I work for and I've found an article named "Kill Perl" on the blog of a guy that managed IT security. His idea was that Perl is an insecure and unmaintainable language and must be banned from any customer facing website or application. And he succeeded in favor of Java.
I did not agree and tried to explain him that he is talking about PERL, things have changed now and Perl programming practices have improved. "system/exec/backtics" calls are rarely used in practice, DBIx::Class makes it easy to avoid SQL injection and Moose provides a great maintainable object system, even better then Java's.

But I didn't have much luck convincing him so I'll also send him this article, PERL is dead. Long live Perl!, to reinforce my words.

Extending Catalyst::Response

| 1 Comment | No TrackBacks

This is how to extend the functionality of Catalyst::Response (and other components in a similar way). For example I would like better default response codes for redirect so I will extend Catalyst::Response into MyApp::Response and override ("around" to be more precise) the redirect method. In order to tell Catalyst to use this response class I will write this in MyApp :

# Override Catalyst::Response class
use MyApp::Response;
__PACKAGE__->response_class('MyApp::Response');

Here is the content of MyApp::Response file :

 package MyApp::Response;
use Moose;
extends 'Catalyst::Response';

around 'redirect' => sub {
my $orig = shift;
my $self = shift;
my ($location, $status) = @_;

# Behave as an accessor
return $self->$orig() unless @_;

my $method = $self->_context->request->method;

# Set better defaults for response codes
# http://sebastians-pamphlets.com/the-anatomy-of-http-redirects-301-302-307
unless ($status) {
if ($method eq 'GET' or $method eq 'HEAD') {
$status = 301; # Moved Permanently
}
elsif ($method eq 'POST') {
$status = 303; # See Other (POST-Redirect-GET pattern)
}
}

return $self->$orig($location, $status);
};

1
I've just read about the Enlightened Perl Organisation's initiative to make Perl more visible through blogging. I've also read Matt S. Trout's rant about this topic and it convinced me to revive me 2 year old Perl blog (containing only one post). So here I am, decided to start my blogger career for the sake of showing what Perl actually stands for.

The first step I had to take was moving the blog from blogger.com to a more flexible Perl based opensource solution hosted on my own server. I've looked and found only two options: Blosxom and MovableType. The first one is very lightweight and has lots of plugins, the second one has lots of functionality out of the box. If I where to go with Blosxom i knew for sure that while searching for the right plugins I would get lost and abandon the blogging project (again). So I chose MovableType.

What other Perl based blog software do you know ? Anything opensource based on Catalyst for example ?

Send Catalyst errors by email

| No Comments | No TrackBacks
So you've launched your brand new Catalyst site in production but your test suite is not complete (or present) and you expect a few glitches here and there. You want to be the first to know when an error happens so checking the error_log file once in a while is out of the question.

Here is what you can do about it :
Override the finalize_error method and make it email the nice Catalyst error page to you. I assume debugging is disabled on a production server so we have to temporarily enable it to fool Catalyst into generating the big nice debug page. This way you don't have to use your own email template and fill it with session,stash,flash and all kind of hashes. On the other hand you might want to change the short error message (non-debug one) to something more personalised, but that in a future post.
Also add the Email module if it's not on the used modules list already and configure the address to send the debug info to.

use Catalyst qw/...other_modules_here_and_then... Email/;

# Set here where the emails should go or add it in your
# appname.yml config file if you are using ConfigLoader
__PACKAGE__->config(
admin_email => 'your@email.com',
);

sub finalize_error {
my $c = shift;
my @args = @_;

# This behaviour might change !
my $class = ref $c;

my $error_output;
unless ( $c->debug ) {
no strict 'refs';
no warnings 'redefine';

*{ "$class\::debug"} = sub { 1 };
$c->NEXT::finalize_error($c, @args);
$error_output = $c->response->output;

*{ "$class\::debug"} = sub { 0 };
};

$c->NEXT::finalize_error($c, @args);
$error_output ||= $c->response->output;

$c->email(
header => [
From => $c->config->{name} . '@error.com',
To => $c->config->{admin_email},
Subject => $c->config->{name} . " error",
],
attributes => {
content_type => "text/html",
},
body => $error_output,
);
};

Later edit: this is now outdated and I'm sure it can be done in a more elegant way (e.g. using Catalyst::Action::RenderView::ErrorHandler) so I'm open to suggestions.

Find recent content on the main index or look in the archives to find all content.

Recent Comments

  • bobtfish: Note that this sort of technique is used on the read more

Tag Cloud

Categories

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.25