Friday, March 02, 2012
Mason 2
Saturday, January 14, 2012
Schlep
Narrowing this down to my own Perl web development work - the schlep for me was always getting the basic web app running with user registration, login pages, password reset mechanisms, etc. - in every new project that was the most repeatable, boring work. I think everyone has the feeling that this does not need to be like that. I've started thinking about what could be a solution to this and here are my first experiments about fixing it: Plack-Middleware-Auth-Form, WebPrototypes::ResetPass, WebPrototypes::Registration (I might rename the first one to WebPrototypes as well). The point is to solve it across the multiple web frameworks, templating languages and storage layers - so that it can survive moving from project to project.
What is your schlep?
Saturday, December 10, 2011
A kind of call by name
$self->create_user( username => $username, email => $email, pass_token => $pass_token );I wish I could get rid of the naming redundancy in this call:
$self->create_user( $username, $email, $pass_token );(without changing 'create_user' of course).
Probably some new syntax would be needed.
Friday, November 18, 2011
'use strict' and cargo cult programming
I used to program without use strict; use warnings but after exposure to the usual propaganda I switched and I found that the cost of mindlessly adding it is negligible, the cases where I need no strict are very rare, and there are many benefits of doing it, especially when working with old code. This cult is rather effective in luring the cargo planes to land in my atoll. On the other hand I am all for deep analysis and checking your assumptions from time to time. There are many valid points in Marc Lehman common::sense and I would like to see them discussed. While we are on the road to have use strict by default we might also try to make it better.
Saturday, November 12, 2011
$ primes for money
Money has been said to change people's motivation (mainly for the better) and their behavior toward others (mainly for the worse). The results of nine experiments suggest that money brings about a self-sufficient orientation in which people prefer to be free of dependency and dependents. Reminders of money, relative to nonmoney reminders, led to reduced requests for help and reduced helpfulness toward others. Relative to participants primed with neutral concepts, participants primed with money preferred to play alone, work alone, and put more physical distance between themselves and a new acquaintance.from one of the first links in the query above. Pretty sad - can that apply to the Perl community? Another link from that list, an entertaining BBC video report suggests also some other effects: hunger and pain insensitivity.
Monday, November 07, 2011
Thesis: simple - antythesis: easy - synthesis: ...
The development sprints he attacks are not about doing the bulk of the work - they are about building a prototype on which we can test our assumptions. Without the understanding that we get from these prototypes we could simplify as much as we want but it would not change the fact that our solution solves the wrong problem. Agile is not an enemy of simple, it puts a lot of weight to doing the easy - but not because this is the goal - rather it uses easy as a mean to get to the correct. Agile is the answer to the paradox that we don't know what we should make until we already have a prototype of that thing. I wish more developers cared about simple - but only after they know what is needed.
Tuesday, November 01, 2011
Notes on the Synthesis of Form
Alexander's Notes on the Synthesis of Form was required reading for researchers in computer science throughout the 1960s. It had an influence[8] in the 1960s and 1970s on programming language design, modular programming, object-oriented programming, software engineering and other design methodologies. Alexander's mathematical concepts and orientation were similar to Edsger Dijkstra's influential A Discipline of Programming.The solution to the design problem that he proposes there does not look too attractive now, but his models, his metaphors, his insight into the design process - it's all still relevant and spot on. I am surprised that the Agile movement does not quote "Notes" as one of their foundation texts.
Saturday, October 15, 2011
Concentration and Flow or Yet Another Dependency Injection Note
Collaborators in an algorithm are like those tools, having them readily available lets you concentrate on the problem.
Friday, October 14, 2011
Object oriented versus functional interface
my $w3c = DateTime::Format::W3CDTF->new;
my $dt = $w3c->parse_datetime( $date_string );
I wish it was:
my $dt = DateTime::Format::W3CDTF->parse_datetime( $date_string );
and that the library created the parser on the fly as needed. It's not only less typing - but also much simpler mental model. This simpler model is sometimes too simple - for example if you parse a lot of dates then sparing the parser creation each time can make a difference.
I think the optimal thing to do is provide two APIs - like JSON - a functional one:
$perl_hash_or_arrayref = decode_json $utf8_encoded_json_text;
and an object oriented one:
$json = JSON->new->allow_nonref;
$perl_scalar = $json->decode( $json_text );
for those that need that extra control.
Monday, October 03, 2011
open expects filename as binary data encoded in the system characterset
use strict;
use warnings;
use autodie;
use HTML::Entities;
use Encode;
my $a = HTML::Entities::decode( 'ñ' );
open(my $fh, '>', $a );
print $fh "Without encoding\n";
close $fh;
open(my $fh1, '>', encode( 'UTF-8', $a ) );
print $fh1 "With encoding\n";
close $fh1
And here is the result when run on an system with UTF8 locales:
zby@zby:~/myopera/tmp$ ls
? a.pl ñ
zby@zby:~/myopera/tmp$ cat ñ
With encoding
'a.pl' is the name of the script itself, the mark '?' hides the F1 hexadecimal code and that file contains 'Without encoding'.
Friday, September 30, 2011
Courriel::MMS
It is a bit heuristic - for example many operators send a subject like 'You have received an mms' - which is useless for us, so the library tries to find something else that would act as the subject we need. For dealing with the various mobile operators, that each send a slightly different format of these emails, I used the Factory design pattern together with Module::Pluggable - this is a novel design for me so I wait for comments.
Tuesday, September 20, 2011
URI->path expects binary data
Consider this code:
use 5.010;
use Encode 'encode';
use URI;
my $uri = URI->new( 'http://example.com/' );
say $uri->path( encode("UTF-8", "can\x{00B4}t-make-it-work" ) );
say $uri->path( "can\x{00B4}t-make-it-work" );
The output (in perl 5.14.0) is:
http://example.com/can%C2%B4t-make-it-work
http://example.com/can%B4t-make-it-work
If your page is encoded in UTF8 - then the first one is correct: %C2%B4 is the URI encoded UTF8 encoding of Unicode Character 'ACUTE ACCENT' (U+00B4). If your page encoding is Latin1 - then the second one would be correct - but this is only by accident - in that case you should still use encode("iso-8859-1", ...).
There are probably many other string manipulating libs that should document if their input should be binary encoded data or decoded character strings.
Thursday, September 01, 2011
Names are special
Sunday, August 28, 2011
is_utf8 is useless - can we have is_character?
$data_structure = utf8::is_utf8($json)
? from_json($json)
: decode_json($json);
taken, together with the
is_character suggestion, from otherwise very informative post: Quick note on using module JSON. I have seen similar code in many places. The idea is to check if the string you have is character data or a string of bytes and treat it appropriately. Unfortunately is_utf8 does not do that check:
use strict;
use warnings;
use utf8;
use HTML::Entities;
use JSON;
my $a = HTML::Entities::decode( ' ' );
my $json = qq{{ "a": "$a" }};
print 'is_utf8: ' . ( utf8::is_utf8( $json ) ? 'yes' : 'no' ) . "\n";
my $data_structure = utf8::is_utf8( $json )
? from_json( $json )
: decode_json( $json );
This fails (on my machine) with following output:
is_utf8: no
malformed UTF-8 character in JSON string, at character offset 8 (before "\x{8a0}" }") at a.pl line 12.
If that still is a mystery try this:
use strict;
use warnings;
use HTML::Entities;
use Devel::Peek;
Dump( HTML::Entities::decode( ' ' ) );
the output (on my machine) is:
SV = PV(0x24f2090) at 0x24f3de8
REFCNT = 1
FLAGS = (TEMP,POK,pPOK)
PV = 0x2501620 "\240"\0
CUR = 1
LEN = 16
this string is internally encoded as "\240" i.e. "\x{0a}" which is Latin1 encoding of non-breaking space. It does not have the utf8 flag set - so the code above tries to treat it as UTF8 encoded stream of bytes and fails.
I don't know if we can have
is_character easily - but the lack of introspection here is surely painful.
Wednesday, August 24, 2011
CPAN, decoupling and Dependency Injection
sub fetch
{
my ($self, $uri) = @_;
my $ua = LWP::UserAgent->new;
my $resp = $ua->get( $uri );
...
}
Yes - this is taken from a post by chromatic.
Now imagine that this is code from a CPAN module you installed and that some security concerns require you to replace LWP::UserAgent with LWPx::ParanoidAgent there. Bad luck - you'll probably need to subclass it, override that whole
fetch method and pray that it will not change too much with every new release of the original module.
This is really why I am drumming this Dependency Injection drum over and over again - code that uses it is more reusable, more universal:
use Moose;
has 'ua', is => 'ro', default => sub { LWP::UserAgent->new };
sub fetch
{
my ($self, $uri) = @_;
my $ua = $self->ua;
my $resp = $ua->get( $uri );
...
}
Now you would not have any problem with providing a LWPx::ParanoidAgent object for the fetch method to use.
By the way, with classical DI you'd move that
LWP::UserAgent->new completely out from the class, here it stays as a 'default' that can be overridden from outside if you need. The problem with classical DI is that you need to have a place where to move that initialization code - here it is sidestepped for the 'normal' usage and you need to worry about it only in the cases where you really need to. Java probably does not have this 'default' mechanism.
Thursday, August 18, 2011
Dependency Injeciton - the cooking metaphor
Ingredients:
- 2-1/4 cups sifted cake flour
- 2 teaspoons baking powder
- 1/2 teaspoon salt
- 1/2 pound (2 sticks) sweet butter, room temperature
...
Preparation:
Preheat the oven to 350 degrees Fahrenheit. Butter and line two 8 x 3-inch baking pans or one 12 x 3-inch pan with parchment.
It is not:
Preheat the oven to 350 degrees Fahrenheit. Find a cow and milk her, wait until ...
neither it is:
Preheat the oven to 350 degrees Fahrenheit. Take your credit card and go to the grocery around the corner ...
Dependency injection is about writing your programs in a very similar manner - you first declare the collaborators and then go on with using them.
Thursday, August 11, 2011
So what is Dependency Injection again?
But this is not all - DI is also about keeping all object collaborators in it's attributes instead of reaching out for global objects (or signletons which are globals in disguise or class attributes). It thus improves object's encapsulation, makes them more self-reliant and testable. Or maybe this part is not DI - but simply writing Object Oriented code?
On the other hand, the separated out object factories are hard to test because they depend on all the objects classes they create and you want to keep them as small and simple as possible. How many such factories you need? If we have something that has a http request object as attribute - then we cannot build it until the http request arrives from the user. If we keep all collaborators in objects attributes - then we cannot build them until we have all information needed to build all these collaborators first. We thus need one factory per scope.
Tuesday, August 02, 2011
Subclassing applications
I imagine that it would be perfect for extended configuration of applications - including web applications. Wouldn't it be great if you could run a slightly changed version of you main web app by making it's code available from PERL5LIB and then subclassing it to change the colors used, add some minor new features and remove some pages for and affiliated site? Or if you could install a blog engine from CPAN, and then subclass it to add new and override old features? This could even make distribution of CPANized applications more popular.
This is one of the things I am experimenting with at Nblog (see also the screencast: Experiments with inheritance in WebNano based applications).
Monday, July 25, 2011
Dependency Injection - Introduction - my talk for YAPC::Europe 2011
Monday, June 27, 2011
Callbacks versus the Template Method pattern
What do you think about them? It seems that some programmers groups prefer one solution over the other (it's hard not to notice how callbacks are popular in the Plack related stuff). Callbacks are simpler, you don't need additional classes there, they don't complicate the program structure, but they are also more constrained. Template Methods are more structural. You can make them call other template methods and make that whole thing fine grained - that is harder with callbacks. In FormHandler I wanted to be able to stack the many generic checks together into lists - but such lists are not very reusable, you cannot override parts of it like you could if they were named methods. The point is that this additional class is another indirection layer and as we know that all programming problems are solvable with in this way :)