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'.