Skip to content

Perl block with lexically-scoped method delegation

May 15, 2010

About a year ago I came across this interesting Ruby blog post Keyword With.

This is something I always wanted in Perl and immediately marked a note in my list of blog/CPAN ideas with “Devel::Declare and with statement” (with side note that “using” might be more appropriate so not to clash with Moose roles sugar).

Unsurprisingly this just languished in my ever increasing list 😦

However last month I had a few hours spare one weekend so pulled out my laptop and rummaged through my list and decided to see how difficult it would be to implement “with”.

Unfortunately I kept hitting brick walls with local scoping. I came to conclusion that I may have to use eval or even go down a different track using an AUTOLOAD solution.

I left that weekend experiment with at least a very basic AUTOLOAD proof of concept:

use 5.012;
use warnings;

{
    package FileStuff;
    use Moose;
    
    has name => ( is => 'rw', isa => 'Str' );
    
    sub save { say "save ",   $_[0]->name }
    
    sub del  { say "delete ", $_[0]->name }
    
    sub mv {
        my ($self, $to) = @_;
        say "rename ", $self->name, " to $to";
        $self->name( $to );
    }
    
    sub say { say "Method say: ", @_ }
}

my $f = FileStuff->new( name => 'foobar' );

# using $f {}
{
    local *AUTOLOAD = sub {
        our $AUTOLOAD;
        my $name = (split '::', $AUTOLOAD)[-1];
        die "method $name NOT FOUND!" unless $f->can( $name );
        $f->$name( @_ );
    };
    
    # my "DSL" stuff!
    save();               # $f->save();
    mv( "barbaz" );       # $f->mv( "barbaz" );
    del();                # parenthesis are always required
    say( "builtin say" ); # builtin wins over $f->say
}

I wasn’t sure if this would ever evolve into a robust solution. However I found it reassuring to note that other people had similar problems trying to use local scoping in similar way when I came across this Perlmonks post A useful use of goto a few days later. So this gave the AUTOLOAD approach some merit.

Well chances are this blog post or any additional development may never made it any further. What changed that was an email from chocolateboy this morning pointing me at his new Scope::With CPAN module.

Blimey… Father Christmas had delivered my xmas pressie early and without even seeing my xmas list πŸ™‚

chocolateboy’s first example of Scope::With even shows how it works with my Builder module:

use Builder;
use Scope::With;

my $builder = Builder->new();
my $xml = $builder->block('Builder::XML');

with ($xml) {
    body(
        div(
            span({ id => 1 }, 'one'),
            span({ id => 2 }, 'two'),
        )
    );
}

say $builder->render();

Which is exactly why I wanted a “with” statement in Perl and it looked like chocolateboy also felt the same!

The documentation even gave an “using” example:

use Scope::With qw(using);

using ($xml) {
    div(
        span( ... ),
        span( ... ),
    );
}

Getting worried now someone was reading my mind πŸ™‚

This is truly awesome stuff. chocolateboy has the habit of producing game changing CPAN modules. For eg. autobox and Method::Lexical. To me Scope::With is potentially another one that falls into this lofty category.

I’m deeply honoured to find my Builder module being referenced and also glad to see that a “with” statement using Devel::Declare and AUTOLOAD isn’t such a delusional idea πŸ™‚

So many thanks to chocolateboy for writing Scope::With. Keep up the good work.

/I3az/

PS. chocolateboy’s Scope::With docs also references a prior art module with by Vincent Pit. This module achieves similar things but in different way/implementation.

PPS. CPAN is so big and wonderful that its easy to miss or even forget whats on there!

PDL advert get its 15 minutes of fame!

May 6, 2010

You may recall that I added the PDL (Perl Data Language) logo to Stackoverflow opensource ads recently.

Well the logo achieved its 15 minutes of glory when it was fortuitously screen captured for the following ReadWriteWeb article about Stackoverflow raising venture capital:

Well done to all those of you voted this up to make this happen πŸ™‚

NB. This is the Hacker News post which pointed me to the ReadWriteWeb article.

/I3az/

Perlcast website is back

May 4, 2010
tags: ,

Good news, Perlcast is back from the GoDaddy grave πŸ™‚

Bad news, The Perl Report which I list on my blog links as now disappeared 😦

Fingers crossed The Perl Report is only a temporary issue. And touch wood Perlcast can return to more regular podcasts (perhaps its something that TPF can look into has part of their promoting Perl?)

/I3az/

PS. Some blog house keeping:

  • Removed use Perl; from my link list. Not much going on their front page these days and it doesn’t look very appealing if you go there with no login/profile.
  • Added YAPC TV to my link list.
  • And also Presenting Perl
  • And yes there are no ironman badges at moment (since ironman site upgrade)

Podcasts & Perl

April 30, 2010
tags: ,

I’m an avid listening of tech podcasts. My podcast subscription list is so large I have a backlog that in some cases stretch back a year.. sometimes two!

There are some podcasts where I do keep upto date with (MacBreak Weekly, TwiT), while there are others I dip in and out of as my time and mood dictates.

Early this year I encountered a Perl conjunction among my podcast listening which culminated in Merlin Mann shouting “Perl, Perl, Perl” in MacBreak Weekly 182: There’s An App For That.

This was the main inspiration for me to write yesterday’s post. Today’s follow up is simply to list all the Perl “related” podcasts that I listened to:

FLOSS Weekly
Opensource podcast hosted by Leo Laporte with Randal Schwartz or Chis DiBona. Below are feature episodes in this podcast series:

Linux Outlaw
Linux and opensource podcast. The following were on episodes of the podcast:

This Week In Tech
Weekly tech roundtable with Leo Laporte at the helm. Below were guests on TwiT (see yesterdays post)

I’ll finish off by mentioning a wonderful dedicated Perl podcast from the Portland Perl Mongers who provide recordings of their regular talks.

/I3az/

PS. I would have also like to have included Perlcast but it appears to have fallen off the web. And Perlcast twitter hasn’t been updated since May 2009 😦

Hopefully it will return in not too distant future?

Famous Perl programmers

April 29, 2010

subtitle: That you probably didn’t know were (Perl programmers)

My definition of famous: They have a Wikipedia entry πŸ™‚

  • Ward Cunningham

    Early pioneer of Extreme Programming and inventor of the Wiki (both the name and the implementation, which was and still is in Perl)

  • Dan Bricklin

    Co-inventor of the first PC spreadsheet program (VisiCalc) and creator of wikiCalc, an online spreadsheet written in Perl (though I believe Bricklin has written a Javascript version as well).

  • John Graham-Cumming

    Inventor of POPFile, a pioneering bayesian spam filtering program written in Perl

  • Its amazing what one can find out by listening to tech podcasts! (more on this in a future blog post).

    Lets wrap up with two more famous programmers you probably have heard of but perhaps didn’t know that they did (and possible occasionally still do!) use Perl:

    Been meaning to namedrop Cal Henderson because it provides me with the opportunity to point you to his brilliant tongue in cheek DjangoCon 2008 Keynote “Why I Hate Django” πŸ™‚

    Embedded YouTube video follows (also here is the link)

    Hats off to the Pythonista’s for showing excellent sense of humour with the keynotes wonderful mickey taking!

    /I3az/

Caralyst 5.8: The Perl MVC Framework

April 19, 2010
tags: ,

No you haven’t misread that and it isn’t a typo! You can see this new Perl book on sell at Amazon right now!

caralyst

Above is my image capture for posterity. Here is the Amazon UK link of the book .

I suspect the morphed authors J; John, A Rockway maybe a bit miffed about this till Amazon fix it. At least the publishers didn’t make the same mistake when printing the book πŸ™‚

/I3az/

Perl ads on Stackoverflow

April 10, 2010

Stackoverflow kindly allow opensource projects to be mixed into their normal advertising stream (shown on their webpages).

To submit an opensource project then you will need a login to their meta stackoverflow site (OpenID, once account created you can then associate this login with your stackoverflow one). After that you need logo of 220x220px and a link to the opensource project.

The final prerequisite is that the submission receives a minimum of 6 votes before ad is served up.

There are currently the following Perl projects listed (all of these are currently above 6 votes and so do get served):

And I have just gone and added PDL & Template Toolkit to this list:

    PDL

NB. Anyone wanting to improve on my cropping & massaging I did to get logo’s to 220×220 then please do so (its a community wiki. Just login and amend).

As part of Perl marketing, advocacy, support or whatever tickles your fancy, please consider adding some other Perl projects which you find invaluable. And while there please help out the current projects with an up vote πŸ™‚

/I3az/

PS. Perl itself could be included (there are Python & Clojure submissions). I’m not sure what logo should be used or even allowed to be used. Perhaps someone in the know with logo to hand could submit the appropriate ad.

PPS. Stackoverflow gets lots of eyeballs. Depending on what you read, its either around 1m or nearly 3m unique visits per month!

So thats a lot of developer eyeballs these ads are being beamed to!

Two questions… similar answers… same module

March 31, 2010
tags: ,

I recently answered the following two Perl questions on Stackoverflow:

… with pretty much identical answers using MooseX::SingletonMethod.

Not bad to be able to kill two birds with one stone and also pimp my module at same time πŸ™‚

NB. In fact it was three birds with this one stone. You will have to look in Google cache to find the now closed Building a Moose class at runtime and tuning it (2415367) question.

/I3az/

The unlikely intersection of Boy George and Damian Conway

March 21, 2010

The first London.pm Perl tech talk of 2010 occurred last week at the good old Aunty Beeb. The theme was “what’s new in Perl” and as per usual it was an enlightening and fun evening.

Even more so when one of the presentations was punctuated by loud cheers from the the ending of the The One Show, which was being broadcast live beneath our very feet. Unbeknown to me and probably most of the Perl mongers present was that Boy George was the star guest!

Still not to be out done the Perl talks were also ended on a triumphant high note with the announcement that the mad scientist of perl himself would be speaking at the next London tech talk!

Looking forward to that πŸ™‚

/I3az/

PS. And if Damien Conway’s talk is even half as good as his classic OSCON 2008 talk then it will be an experience not to be missed.

Regex brain fart!

March 11, 2010
tags: ,

Yesterday I got stuck for far too long on a simple regex not doing what I expected it to do. Here is an example:

sub prefix {
    return $1 if $_[0] =~ m/^(\w+)_/;
    return 'unknown';
}

say prefix( 'radio_some_very_long_var_name' );
say prefix( 'comment_blahblah'              );

I was “hoping” to see radio and comment but got back an incorrect radio_some_very_long_var on first item (and unfortunately I hadn’t noticed the _name had been dropped. That may have saved a lot of the heartache!).

I looked long and hard at the regex thinking…

what is wrong with \w+ ?

Of course what was wrong was me 😦

\w regex includes matching the _ (underscore) πŸ™‚

From perlre pod

\w – Match a “word” character (alphanumeric plus “_”)

I don’t think I’ve ever needed the pleasure of knowing that… but I do now! Here’s the correct regex i needed:

m/^([A-Za-z]+)_/

Doh!

/I3az/