Florian Maier: sorry for the silence
... we are moving to bigger office building at the moment ;-)
$ xvfb-run -a make testinstead of a plain
make test
. That'll automatically create a fake
xserver, set up DISPLAY
and run make test
in that environment.
That works well for manually installing modules. When installing using
CPAN.pm
you can make things easier by writing a distropref.
First, tell cpan where your distroprefs are. I use ~/.cpan/prefs
:
$ cpan cpan[1]> o conf init prefs_dir [...] <prefs_dir> Directory where to store default options/environment/dialogs for building modules that need some customization? [] /home/rafl/.cpan/prefs cpan[3]> o conf commit commit: wrote '/home/rafl/.cpan/CPAN/MyConfig.pm'Now write a distropref for the modules that need an X server and put it into your prefs dir as
X11.yml
--- match: distribution: /(?x:Wx Gtk2 Gnome2 ... other modules requiring an X server )-\d test: commandline: "xvfb-run -a make test"Now the tests for Wx, Gtk2, Gnome2 and all other distributions you list in that regex will be executed with a fake X server. I have yet to figure out how to write a distropref that just prepends to the test commandline instead of replacing it so I won't need to have another pref for all modules using
Module::Build
.
my
declarations that
includes a type name, like this:
my Str $x = 'foo';However, that didn't do anything useful, until Vincent Pit came along and wrote the excellent Lexical::Types module, which allows you to extend the semantics of typed lexicals and actually make them do something useful. For that, it simply invokes a callback for every
my
declaration with a type in the scopes it is
loaded. Within that callback you get the variable that is being
declared as well as the name of the type used in the declaration.
We also have Moose type constraints and the great
MooseX::Types module,
that allows us to define our own type libraries and import the type
constraints into other modules.
Let's glue those modules together. Consider this code:
use MooseX::Types::Moose qw/Int/; use Lexical::Types; my Int $x = 42;The first problem is that the perl compiler expects a package with the name of the type used in
my
to exist. If there's no such package
compilation will fail.
Creating top-level namespaces for all the types we want to use would
obviously suck. Luckily the compiler will also try to look for a
function with the name of the type in the current scope. If that
exists and is inlineable, it will call that function and use the
return value as a package name.
In the above code snippet an Int
function already exists. We
imported that from MooseX::Types::Moose
. Unfortunately it isn't
inlineable. Even if it were, compilation would still fail, because it
would return a Moose::Meta::TypeConstraint
instead of a valid
package name.
To fix that, let's rewrite the code to this:
use MooseX::Types::Moose qw/Int/; use MooseX::Lexical::Types qw/Int/; my Int $x = 42;Let's also write a MooseX::Lexical::Types module that replaces existing imported type exports with something that can be inlined and returns an existing package name based on the type constraint's name.
package MooseX::Lexical::Types; use Class::MOP; use MooseX::Types::Util qw/has_available_type_export/; use namespace::autoclean; sub import my ($class, @args) = @_; my $caller = caller(); my $meta = Class::MOP::class_of($caller) Class::MOP::Class->initialize($caller); for my $type_name (@args) # get the type constraint by introspecting the caller my $type_constraint = has_available_type_export($caller, $type_name); my $package = 'MooseX::Lexical::Types::TYPE::' . $type_constraint->name; Class::MOP::Class->create($package); $meta->add_package_symbol('&'.$type_name => sub () $package ); Lexical::Types->import; # enable Lexical::Types for the caller 1;With that the example code now compiles. Unfortunately it breaks every other usecase of MooseX::Types. The export will still need to return a
Moose::Meta::TypeConstraint
at run time so this will continue to
work:
has some_attribute => (is => 'ro', isa => Int);So instead of returning a plain package name from our exported function we will return an object that delegates all method calls to the actual type constraint, but evaluates to our special package name when used as a string:
my $decorator = MooseX::Lexical::Types::TypeDecorator->new($type_constraint); $meta->add_package_symbol('&'.$type_name => sub () $decorator );and:
package MooseX::Lexical::Types::TypeDecorator; use Moose; use namespace::autoclean; # MooseX::Types happens to already have a class that doesn't do much # more than delegating to a real type constraint! extends 'MooseX::Types::TypeDecorator'; use overload '""' => sub 'MooseX::Lexical::Types::TYPE::' . $_[0]->__type_constraint->name ; 1;Now we're able to use
Int
as usual and have Lexical::Types invoke
its callback on MooseX::Lexical::Types::TYPE::Int
. Within that
callback we will need the real type constraint again, but as it is
invoked as a class method with no good way to pass in additional
arguments, we will need to store the type constraint somewhere. I
choose to simply add a method to the type class we create when
constructing our export. After that, all we need is to implement our
Lexical::Types callback. We will put that in a class all our type
classes will inherit from:
Class::MOP::Class->create( $package => ( superclasses => ['MooseX::Lexical::Types::TypedScalar'], methods => get_type_constraint => sub $type_constraint , , ), );The Lexical::Types callback will now need to tie things together by modifying the declared variable so it will automatically validate values against the type constraint when being assigned to. There are several ways of doing this. Using
tie
on the declared variable
would probable be the easiest thing to do. However, I decided to use
Variable::Magic
(also written by Vincent Pit - did I mention he's awesome?), because
it's mostly invisible at the perl level and also performs rather well
(not that it'd matter, given that validation itself is relatively
slow):
package MooseX::Lexical::Types::TypedScalar; use Carp qw/confess/; use Variable::Magic qw/wizard cast/; use namespace::autoclean; my $wiz = wizard # store the type constraint in the data attached to the magic data => sub $_[1]->get_type_constraint , # when assigning to the variable, fail if we can't validate the # new value ($_[0]) against the type constraint ($_[1]) set => sub if (defined (my $msg = $_[1]->validate($ $_[0] ))) confess $msg; (); ; sub TYPEDSCALAR # cast $wiz on the variable in $_[1]. pass the type package name # in $_[0] to the wizard's data construction callback. cast $_[1], $wiz, $_[0]; (); 1;With this, our example code now works. If someone wants to assign, say,
'foo'
to the variable declared as my Int $x
our magic
callback will be invoked, try to validate the value against the type
constraint and fail loudly. WIN!
The code for all this is available
github and should also
be on CPAN shortly.
You might notice warnings about mismatching prototypes. Those are
caused by Class::MOP and fixed in the git version of it, so they'll go
away with the next release.
There's still a couple of caveats, but please see the documentation
for that.
sub base : Chained('/') PathPart('') CaptureArgs(0) ... sub index : Chained('base') PathPart('') Args(0) ... sub default : Chained('base') PathPart('') Args ...It's a nice and clean syntax that keeps all important information right next to the method it belongs to. However, attributes in perl have a couple of limitations. For one, the interface the perl core provides to use them is horrible and doesn't provide nearly enough information to do a lot of things, but most importantly attributes are just plain strings. That means you will need to parse something like
"Chained('base')"
into
(Chained => 'base')
yourself to make proper use of them.
While that's easy for the above example, it can be very hard in the
general case because only perl can parse Perl. It's one of the reasons
you can't use Catalyst::Controller::ActionRole to apply
parameterized roles to your action instances, because parsing
parameters out of things like
Does(SomeRole => names => [qw/affe tiger/], answer_re => qr/42/ )
would be awful and wrong.
With Catalyst 5.8 most of the attribute related code has been removed
from the internals. It's now using MooseX::MethodAttributes to do
all the heavy lifting. Also the internals of how actions are
registered have been refactored to make it easier to implement
alternate ways without changing the Catalyst core.
As a proof of concept for this I implemented a new way of declaring
actions that's very similar to how Moose provides it's sugar
functions. You can get it from
github.
With that, the above example looks like this:
action base => (Chained => '/', PathPart => '', CaptureArgs => 0) => sub ... ; action index => (Chained => 'base', PathPart => '', Args => 0 ) => sub ... ; action default => (Chained => 'base', PathPart => '', Args => undef) => sub ... ;It also moves method declaration from compiletime to runtime, making this possible:
for my $action (qw/foo bar baz/) action $action => (Chained => 'somewhere', Args => 0) => sub my ($self, $ctx) = @_; $ctx->stash-> $action = $ctx->model('Foo')->get_stuff($action); ;Admittedly, that's all very ugly, but illustrates well what kind of things we're able to do now. But it doesn't need to be ugly. With Devel::Declare we have a great tool to add our own awesome syntax to perl, similar to how things like MooseX::Method::Signatures, MooseX::MultiMethods and MooseX::Declare do. So how would a declarative syntax for Catalyst controllers look like? I don't know. Ideas include something like this:
under /some/where, action foo ('foo', $id) ...to mean:
sub foo : Chained('/some/where') PathPart('foo') CaptureArgs(1) ...Adding Moose type constraints to this would be interesting, too, and make validation of captures and arguments a lot easier. Multi dispatch similar to MooseX::MultiMethods could be handy as well:
under /some/where action ('foo', Int $id) # find and stash an item by id action ('foo', Str $name) # search items using $name action ('foo', Any $thing) # display error pageSo you see there are a lot of possibilities that should be explored. Unfortunately I have no idea what kind of syntax and features people would like to have, so your feedback on this would be much appreciated. :-)
$ perl -MYAML::Syck -e print Can't locate YAML/Syck.pm in @INC (@INC contains: /usr/local/lib/perl5/5.10.0/i686-linux /usr/local/lib/perl5/5.10.0 /usr/local/lib/perl5/site_perl/5.10.0/i686-linux /usr/local/lib/perl5/site_perl/5.10.0 .). BEGIN failed--compilation aborted.Update: Well done to Raphael who spotted the local that I d overlooked. D oh! Now to see if I can discover where that came from. Update 2: Also thanks to Florian who emailed in at about the same time (I don t read my email as much as my website dashboard, usually).
perl -V
:
Compiled at Aug 4 2008 09:48:59 @INC: /usr/local/lib/perl5/5.10.0/i686-linux /usr/local/lib/perl5/5.10.0 /usr/local/lib/perl5/site_perl/5.10.0/i686-linux /usr/local/lib/perl5/site_perl/5.10.0Our good systems show a December compiled at date and a much longer @INC list. I ve tried reinstalling pretty much every package I think might be connected, including libc6, libperl5.10, perl, perl-base and perl-modules (those last four are all version 5.10.0-19). I really don t want to reinstall the whole system because it s behind an irritating firewall that will make it a 10-hour round-trip site visit. I m currently hunting and removing obsolete packages to try to make sure they re not causing problems, because I m running out of straws to clutch at. How would you recover a debian system from this?
http://xkcd.com/344/ I wouldn’t have gotten the joke if it wasn’t for you. Thanks.The email not only made me smile (for it is always nice to know your students are leaning something) but it gave me the idea that in the future I might include a comic based exams composed of 5-10 comic strips (many from xkcd) and ask for an exegesis of them. Why not? Seems like fun to me. And it would be great to include the following as one of my course objectives: “By the end of this class you will be able to read xkcd and actually understand (most) of it.” If they could do that, well, they must have learned at least something update: One of my readers, Florian, sent this nice bit, which I have not read before: “I read your blog via Planet Debian and immediately felt reminded of Jane
Recently I was talking with one of the best researcher know, TimLater in the semester, and for my hacker course, we will be reading Understanding Comics, which will give us a bit of a meta perspective on why comics are so good at conveying a certain type of message. I can’t wait to read it as I have heard it is fantastic.
O’Halloran. He has been able to inspire generations of middle and high
school students to care for the natural world. Tim told me that Gary
Larson has had a major impact on his teaching. Tim uses Far Side
cartoons to introduce topics, to illustrate points, and to “reinforce
the notion that the more we investigate the universe, the richer is our
experience.” When designing exam papers Tim finds the cartoons “ease the
tension and spark the memory.” It all began when, in the fall of 1985,
he was given the task of teaching science to 162 Tulsa ninth-graders who
were convinced that it was absolutely irrelevant to their futures. Tim
put one hundred Far Side cartoons on a large bulleting board, and told
the students to study them. The consensus was that they didn’t
understand the humor - The Far Side was “too weird”. However, Tim wrote
me, “Each time we completed a unit and the students approached the
bulletin board with newly acquired wisdom, I smiled quietly and thanked
the cosmos for Gary’s perspectives as the kids roared with the confident
laughter of the enlightened.” Having taught myself for a while, I can fully and happily relate.
http://xkcd.com/344/ I wouldn’t have gotten the joke if it wasn’t for you. Thanks.The email not only made me smile (for it is always nice to know your students are leaning something) but it gave me the idea that in the future I might include a comic based exams composed of 5-10 comic strips (many from xkcd) and ask for an exegesis of them. Why not? Seems like fun to me. And it would be great to include the following as one of my course objectives: “By the end of this class you will be able to read xkcd and actually understand (most) of it.” If they could do that, well, they must have learned at least something update: One of my readers, Florian, sent this nice bit, which I have not read before: “I read your blog via Planet Debian and immediately felt reminded of Jane
Recently I was talking with one of the best researcher know, TimLater in the semester, and for my hacker course, we will be reading Understanding Comics, which will give us a bit of a meta perspective on why comics are so good at conveying a certain type of message. I can’t wait to read it as I have heard it is fantastic.
O’Halloran. He has been able to inspire generations of middle and high
school students to care for the natural world. Tim told me that Gary
Larson has had a major impact on his teaching. Tim uses Far Side
cartoons to introduce topics, to illustrate points, and to “reinforce
the notion that the more we investigate the universe, the richer is our
experience.” When designing exam papers Tim finds the cartoons “ease the
tension and spark the memory.” It all began when, in the fall of 1985,
he was given the task of teaching science to 162 Tulsa ninth-graders who
were convinced that it was absolutely irrelevant to their futures. Tim
put one hundred Far Side cartoons on a large bulleting board, and told
the students to study them. The consensus was that they didn’t
understand the humor - The Far Side was “too weird”. However, Tim wrote
me, “Each time we completed a unit and the students approached the
bulletin board with newly acquired wisdom, I smiled quietly and thanked
the cosmos for Gary’s perspectives as the kids roared with the confident
laughter of the enlightened.” Having taught myself for a while, I can fully and happily relate.
CF3401A9 Elmar Hoffmann AF260AB1 Florian Zumbiehl 454C864C Moritz Lapp E6AB2957 Tilman Koschnick A0ED982D Christian Brueffer 5A35FD42 Christoph Ulrich Scholler 514B3E7C Florian Ernst AB0CB8C0 Frank Mohr 797EBFAB Enrico Zini A521F8B5 Manuel Zeise 57E19B02 Thomas Glanzmann 3096372C Michael Fladerer E63CD6D6 Daniel Hess A244C858 Torsten Marek 82FB4EAD Timo Weing rtner 1EEF26F4 Christoph Ulrich Scholler AAE6022E Karlheinz Geyer EA2D2C41 Mattia Dongili FCC5040F Stephan Beyer 6B79D401 Giunchedi Filippo 74B11360 Frank Mohr 94C09C7F Peter Palfrader 2274C4DA Andreas Priesz 3B443922 Mathias Rachor C54BD798 Helmut Grohne 9DE1EEB1 Marc Brockschmidt 41CF0322 Christoph Reeg 218D18D7 Robert Schiele 0DCB0431 Daniel Hess B84EF12A Mathias Rachor FD6A8D9D Andreas Madsack 67007C30 Bernd Paysan 9978AF86 Christoph Probst BD8B050D Roland Rosenfeld E3DB4EA7 Christian Barth E263FCD4 Kurt Gramlich 0E6D09CE Mathias Rachor 2A623F72 Christoph Probst E05C21AF Sebastian Inacker 5D64F870 Martin Zobel-Helas 248AEB73 Rene Engelhard 9C67CD96 Torsten VellerIt’s likely that this happened thanks to a very successful key signing party somewhere in germany (looking at the email addresses). [Update: It was the LinuxTag 2005 KSP.] It might be a nice challenge to beat that clique during next Debconf ;) And the biggest clique I’m in contains 23 keys. Not too bad.
Next.