Emacs cperl-mode Perl6+MX::Declare news

I recently invested some tuits to rebase my Perl6 enhancements in Emacs cperl-mode to the latest version 6.2 that Ilya Zakharevich provides on his homepage.

After wading through tears and blood (well, it was more about different levels of fatigue) I felt strong enough to also finally merge "the other big branch"(tm) that Jonathan Rockway maintains for MooseX::Declare syntax enhancements.

The result is yet another branch mxd-perl6-merge on github. The leading version is ilyaz' (including useless whitespace left as is), then Perl6 adaptions, then MX::Declare which in turn also made some things more clever for the Perl6 changes.

If you ever tried one of the cperl-mode hacks for Perl6 or MooseX::Declare, please switch to this merge. It should work the same or even better for you. If not, report it to me.

Starting Over from Scratch

From Lee Harris's circuitous polemic Civilization and Its Enemies: The Next Stage of History :
The error of abstract reason is, in short, forgetfulness. It forgets that its abstractions are designed to try to capture the infinitely elusive real. It begins to use these same abstractions as yardsticks by which to judge the real.
Harris was summarizing a lengthy criticism of Enlightenment-era political thought, and the failure of the various radical philosophies that attempted to wipe away and reconstruct society from the ground up. I find the same lesson applies to good software: it's often imperfect, but those imperfections are there because reality demands them, and scrapping a working, if imperfect solution in favor of building a new one inspired by a new abstraction is a project often doomed to failure.

Objects are experts

Note: this is part of an occasional series of posts aimed at newer programmers, or newer Perl programmers. I'm going to talk about what objects are, but even programmers who think they know the answer might benefit from some of this. That includes myself. You might take exception to some of what I write, so feel free to chime in with counter-arguments.

For many older programmers such as myself (I'm 42 as of this writing), their first computer program is often a variant of the following BASIC program:

10 INPUT "What's your name? ", N$
20 PRINT "Hello, "; N$
30 GOTO 10

That's already pretty interesting. In just three lines of code we have some idea about:

  • I/O (input/output)
  • Variables (N$)
  • Data types (the '$' indicates a string variable)
  • Flow control (the GOTO)

L. Peter Deutsch hates Perl

... and Larry Wall.

Deutsch: [...] that is Lisp is lexically pretty monotonous.

Seibel: I think Larry Wall described it as a bowl of oatmeal with fingernail clippings in it.

Deutsch: Well, my description of Perl is something that looks like it came out of the wrong end of a dog. I think Larry Wall has a lot of nerve talking about language design - Perl is an abomination as a language.

I wonder what the right end of a dog is?

L. Peter Deutsch (of Ghostscript fame) in Coders at Work

Class custom dumping for Data::Dump

I'm using DateTime objects a lot these days: anytime I get some date/time data from outside of Perl, the first thing I do is convert them to DateTime object, to avoid calculation/formatting hassle ahead.

However, the dumps are not pretty.

% perl -MDateTime -MData::Dump -e'dd [DateTime->now]'

Mapping substitutions

I am always wondering what is the best procedure to perform a substitution on a set of elements. While map could be a good idea,
@foo = map { s/bar/foo/g; $_ } @bar
is not legible. Probably better to use
@foo = @bar; s/bar/foo/g for @foo;
While I understand that the substitute operator return value is useful and relevant, I wonder if there is a cleaner syntax for this behavior with map.

Handling Hierarchy in Databases

There have been a lot of solutions to solve the hierarchy in a relational database. I'm pretty sure that I've tried them all, but ultimately they all fell short for one reason or another. Nested Sets are a beautiful answer, but fall short if you have too much data. Materialized paths are great except that they don't maintain ordered siblings. Many others require complex joins, special triggers, or stored procedures that you can't use in SQLite, or would require you to do something different in each database you support.

Through much trial an error, I solved the problem with a mechanism that is easy to use, fast, and will work with any SQL database. It doesn't require stored procedures, triggers, joins or anything else, so it will work even on the simplest of databases. I call that solution Lineage.

Why?

I see code…

my @showart;
my @articles;
map {push(@showart, $_) if($_->{'article_description_cat'} eq $category);} @{$alteration};
@articles = sort {$a->{'article_description_rank'} <=> $b->{'article_description_rank'}} @showart;

So what do we have there?

  • A superfluous temporary variable: @showart.
  • A use of map that explicitly throws away map’s return value and goes for the side-effects only.
  • Lines that are way too long.
  • Don’t get me started about the semicolon in the map expression.
  • Don’t make me think about the whitespace that is missing in all sorts of places.
  • An instance of intentional obfuscation?

Bleg: Why the alternating result?

I know I made a mistake by doing a double glob, but why this pattern of output? Note, there is only one file matching /etc/magic.* and two files matching /etc/magic*, and none matching /etc/magic.dragon*

% perl -E'say (glob </etc/magic.*>) for 1..10'
/etc/magic.mime
2
/etc/magic.mime
4
/etc/magic.mime
6
/etc/magic.mime
8
/etc/magic.mime
10

% perl -E'say (glob </etc/magic*>) for 1..10'
/etc/magic
/etc/magic.mime
3
/etc/magic
/etc/magic.mime
6
/etc/magic
/etc/magic.mime
9
/etc/magic

% perl -E'say (glob </etc/magic.dragon*>) for 1..10'

I'm really hating Perl's context today.

Dan Meyer on math, Alan Kay on physics, and the Pharo tutorial

Dan Meyer has a nice TED talk about math pedagogy:

I find it very similar to Alan Kay's TED talk where he shows off various Smalltalk things to illustrate physics concepts:

The best introductory programming book I have ever seen (and I've seen a lot in many languages) is Squeak: Learn Programming with Robots, which many people might recognize as a very LOGO like introduction.

Along with that, I was quite intrigued with Pharo's introduction to Smalltalk. This world has a tutorial class. When you first open the world (which is just double-clicking the icon of the single file you downloaded), the big window tells you exactly what to do first then leads you through a 24 step tutorial of the entire language and some of the tools:

If you haven't run into Smalltalk before, its world is what many other languages strive to be but ignore. Smalltalk is where the idea of a refactoring browser started, and what a lot of people want for Perl. I think a lot of the work going into Padre is really an attempt to force a Smalltalk-like world on Perl. I even tried to make my own Perl system browser, which I've now put it on Github although I don't use it anymore.

Introduction and API spec

Hello, good evening and welcome.

For the next few months I will be using this blog to help document and publicise my "Ctypes for Perl" project. The project is being carried out for TPF under the auspices of the Google Summer of Code programme, and mentored by Reini Urban.

What's a ctypes?

'ctypes' is the Foreign Function Interface (FFI) library distributed with the Python core. It basically allows native C libraries to be called easily from Python; for module authors, it allows the wrapping of C libraries in pure Python.

This is obviously a powerful concept. Imagine a world where Perl module authors didn't need to use XS, and module consumers don't need to have a correctly configured compiler set up on their system. This is the purpose of the project: to create an easy, cross-platform, pure-Perl interface to native C libraries.

Implementations

2010 Perl Survey

Yeah, you've probably already seen this, but in case you haven't, read the Perl survey announcement and take the survey!

Perl::Critic --praise

Wouldn't it be cool if Perl::Critic had a --praise option?

It would tell programmers they have done something worth mentioning and throw in some inspirational kudos and uplifting recognition:

$ perlcritic --praise script.pl
Good! Filehandle is not a Bareword at line 4, column 1.  See page 202,204 of PBP.  (Cleverness: 1)
Nice use of comments in a regex. Maintainers will be happy at line 9, column 1.  See page 289 of PBP.  (Cleverness: 3)
Outstanding use of Moose roles at line 11, column 1.  (Cleverness: 2)
Named parameters are definitely easier to read at line 42, column 1 (Cleverness: 1)
Quite sure this 1 line of Perl translates to at least 12 in Java at line 58, column 1 
Oh yeah! You rock! at line 74, column 1.  (Cleverness: 4)
This package may be useful to the community. Make sure you upload it to the CPAN at line 74, column 1.  (Cleverness: 5)

I think psychologists call it positive reinforcement. :)

Extracting and accessing the release history in perlhist

I needed to make some charts showing the sequence of perl's releases, so I wrote some code to extract all that from perlhist. I thought I might turn it into a module or add it to Module::CoreList, but I have a lot of other things I need to do. If someone else wants to do that, however, here's my script:

The Perl Survey 2010 is ready for you to complete!

Finally after many delays, the Perl Survey 2010 is ready for participants to complete. It should take you about 10 to 15 minutes. The purpose of this survey is to find out information about programmers who use Perl, the tools that they use, and their opinions about Perl. The survey is funded by The Perl Foundation, with hosting support from Strategic Data and Shadowcat Systems. We plan to re-run the survey every two to three years to see how the community changes over time.

So please fill in a questionnaire yourself by visiting the Perl Survey website. Once you've done that, please tell your friends, tell your friends friends, and your friends friends friends. We don't just want heavily involved members of the Perl community to complete the survey but more casual Perl programmers as well. The 2007 survey managed about 4,000 responses, but I'd be happy with around the 1,000 mark.

I'll be presenting the initial cut of the data analysis in three weeks time (Week beginning June the 5th) at the German Perl Workshop.

No semicolon after return values

Perl lets you omit semicolons at the end of a block. That is, you can treat ; as a statement separator, not a terminator, as in Pascal.

OK, but why would you want to do that? The advantage of terminating each statement is that you don't need to do anything special for the first and last lines: You can insert or remove lines at any point without worrying about the rest of the code. Why throw that away?

Because sometimes it doesn't make sense to add code at the end. Consider this code:

sub foo {
  my ($s) = @_;
  $s =~ s/%/%%/g;
  $s
}

Programmers need a muse

The Muses in Greek mythology are the goddesses or spirits who inspire the creation of arts; they were considered the source of the knowledge. And I believe as a programmer you need your muse.

Most of the time whenever I talk with people about what I do, they automatically start picturing me as some kind of a robot which rambles on for hours in a language they don't understand, but computers do.

I don't blame them, it's hard to put "computer" and "free-will" in the same sentence, because computers are nothing but machines that follow orders, no matter how complex these might be; and in that mindset, they don't put "creativity" near "computer".

But programmers are not machines, we are the ones that tell them what to do, and in that, there is an amazing amount of creativity: as we have the power to create.

Just want to know WNOHANG...

package MyApp;
# I just want to know WNOHANG from sys_wait_h ... [-;

our $WNOHANG_VALUE ||= `$^X -MPOSIX=:sys_wait_h -e "print WNOHANG;"`;
sub WNOHANG () { $WNOHANG_VALUE }

!!'^^';

sigh maybe useful for a long-time running program if you are sensitive to memory usage..

So mst lost iron man

It was fortuitous that I joined Iron Man at this late stage as now I get to vote for mst's forfeit!

Test-driving MongoDB on a Moose

I spent a few hours this last weekend trying out MongoDB, and its Perl driver.

I was curious to see how I could put NoSQL databases to good programming use. MongoDB stroke me as a good contender for some experiments: it's easy to install, well documented and has an intuitive query language that reminds me of SQL::Abstract syntax. Besides, MongoDB allegedly is very fast and scalable, which left me wondering how my configuration management apps could benefit from a backend switch from relational to a non-ACID document based DB like MongoDB, just like these people did for their server monitoring software.

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.