Saturday, September 24, 2011

Add Try::Tiny to Mouse or Moose


Here is a little snippet that shows how to extend Mouse (lightweight version of Moose) with Try::Tiny for better exception handling.

Here would be your application class:

package MyApp::Mouse;

use Mouse ();
use Mouse::Exporter;
use Try::Tiny;

Mouse::Exporter->setup_import_methods( 
	as_is => [qw[try catch finally]],
	also  => 'Mouse'
);

1;

Then you simply use MyApp::Mouse in place of the Mouse module for your classes:

package Foo;

use MyApp:::Mouse;

sub t {
	my ($self,$d) = @_;
	
	my $v = try {
		die "testing" if $d;
		'no';
	} catch {
		$_;
	}; 

	return $v;
}

1;

A simple test?

package Foo;

~$ perl -Mfeature=:5.12 -MFoo -e say Foo->new->t;
no

Of course this recipe works with Moose, simply replace the Mouse namespace with the Moose namespace and your in business.

Friday, September 23, 2011

Experimenting with #mojolicious experimental groups block

In the course of discussion via IRC in #mojo the groups block was added as an experimental feature to Mojolicious::Lite (originally called routes but shortly after renamed), here is the result of me playing with it in the context of a authentication and authorization:


use Mojolicious::Lite;

under sub { 
  my $self = shift;
  my ($user) = split /:/, $self->req->url->to_abs->userinfo;
  
  unless ($user) {
    $self->res->headers->www_authenticate('Basic realm=test');
    $self->render_text('You must log in', status => 401);
    return;
  } else {
    $self->stash( user => $user );
    return 1;
  }
};

helper greet => sub {
  sprintf(
    "Hello %s would you like to play a game",
    shift->stash('user')
  );
};

get '/' => sub { shift->greet; };

group {
  under '/gtw' => sub { shift->stash('user') eq 'david' };

  get sub {
    shift->render_text('The only way to win is to not play at all');
  };
};

get '/chess' => sub { shift->render_text('checkmate'); };
get '/bye'   => sub { shift->render_text('Bye!', status => 401); };

app->start;


Friday, September 9, 2011

Implementation of Pascal's Triangle with Perl and Moose

Probably not the best way to do it, but I thought I would give it a hack:




#!/usr/bin/perl

package Row;

use Moose;

has triangle => (
is => 'ro',
isa => 'Triangle',
);

has prev => (
is => 'ro',
isa => 'Row',
);

has members => (
is => 'rw',
isa => 'ArrayRef[Int]',
lazy_build => 1,
);

sub _build_members {
my $self = shift;

return [1] unless $self->prev;

my @prev = @{ $self->prev->members };

my @values = (1);

for(my $i = 0; $i < ( scalar(@prev) - 1 ); $i++) {
my $c = $prev[ $i ];
my $n = $prev[ $i + 1 ] || 1;

push @values, $c + $n;
}

push @values, 1;

return \@values;
}

sub print {
my $self = shift;
print join ' ', @{ $self->members },"\n";
return $self;
}

__PACKAGE__->meta->make_immutable;

no Moose;

package Triangle;

use Moose;

has rows => (
is => 'rw',
isa => 'ArrayRef[Row]',
default => sub { [ Row->new({}) ] },
);

sub first {
my $self = shift;
return $self->rows->[0];
}

sub last {
my $self = shift;
return $self->rows->[ $#{ $self->rows } ];
}

sub next {
my $self = shift;
my $row = Row->new( prev => $self->last );
push @{ $self->rows }, $row;
return $self->last;
}

__PACKAGE__->meta->make_immutable;

package main;

die "Usage: $0 \n"
unless $ARGV[0] > 1;


my $t = Triangle->new;

$t->next->prev->print foreach(1..$ARGV[0]);