About
Random notes: A pile of assorted scribblings, snippets and ramblings (mostly about programming and the software that makes my life easier).

Rhesa Rozendaal
Subscribe
Subscribe to a syndicated feed of my weblog.
Categories
Archive
November
Sun Mon Tue Wed Thu Fri Sat
 
29        
Links

2010. 11. 29

Devel::Declare, Method::Signatures, oh my


Actually, I got another inspiration from Method::Signatures, for use with CGI::Application.

But while implementing that, I decided to extract the hard Devel::Declare bits, and make a macro installer.

Here’s a re-implementation of Method::Signatures using my Devel::Declare::Macro:

 package Method::Signatures;
 use Devel::Declare::Macro;

 sub import {
  install_macro(
    into           => scalar(caller),
    name           => 'method',
    proto_parser   => \&make_proto_unwrap,
    proto_injector => \&inject_from_signature
  );
 }

 sub make_proto_unwrap {} # as before, except return \%signature
 sub inject_from_signature {} # as before
 1;

I’m somewhat confident it could simplify the implementation of Sub::Curried and MooseX::Method::Signatures as well.

Basically it packages the synopsis of Devel::Declare. As such, I don’t think I should maintain it. Comments welcome.

  package Devel::Macro;

  sub import {
      my $class = shift;
      my $pkg = caller;
      no strict 'refs';
      *{$pkg.'::install_macro'} = \&install_macro;
  }

  # Stolen from Devel::Declare's t/method-no-semi.t
  use Devel::Declare ();
  use Scope::Guard;
  use Sub::Name;
  sub install_macro {
      my %args   = @_;
      # I don't really understand why we need to declare method
      # in the caller's namespace.
      {
          no strict 'refs';
          *{$args{into}.'::'.$args{name}}   = sub (&) {};
      }
      Devel::Declare->setup_for(
          $args{into},
          { $args{name} => {
              const => mk_parser(
                  $args{proto_parser}||sub{''},
                  $args{proto_injector}||sub{join' ', @_},
                  $args{pre_install}||sub{},
                  )
              },
          },
      );

  }
  our ($Declarator, $Offset);

  sub skip_declarator {
      $Offset += Devel::Declare::toke_move_past_token($Offset);
  }

  sub skipspace {
      $Offset += Devel::Declare::toke_skipspace($Offset);
  }

  sub strip_name {
      skipspace;
      if (my $len = Devel::Declare::toke_scan_word($Offset, 1)) {
          my $linestr = Devel::Declare::get_linestr();
          my $name = substr($linestr, $Offset, $len);
          substr($linestr, $Offset, $len) = '';
          Devel::Declare::set_linestr($linestr);
          return $name;
      }
      return;
  }

  sub strip_proto {
      skipspace;

      my $linestr = Devel::Declare::get_linestr();
      if (substr($linestr, $Offset, 1) eq '(') {
          my $length = Devel::Declare::toke_scan_str($Offset);
          my $proto = Devel::Declare::get_lex_stuff();
          Devel::Declare::clear_lex_stuff();
          $linestr = Devel::Declare::get_linestr();
          substr($linestr, $Offset, $length) = '';
          Devel::Declare::set_linestr($linestr);
          return $proto;
      }
      return;
  }

  sub shadow {
      my $pack = Devel::Declare::get_curstash_name;
      Devel::Declare::shadow_sub("${pack}::${Declarator}", $_[0]);
  }

  sub inject_if_block {
      my $inject = shift;
      skipspace;
      my $linestr = Devel::Declare::get_linestr;
      if (substr($linestr, $Offset, 1) eq '{') {
          substr($linestr, $Offset+1, 0) = $inject;
          Devel::Declare::set_linestr($linestr);
      }
  }

  sub scope_injector_call {
      return ' BEGIN { ' . __PACKAGE__ . '::inject_scope }; ';
  }

  sub mk_parser {
      my $proto_parser   = shift;
      my $proto_injector = shift;
      my $install_cb     = shift;

      return sub {
      local ($Declarator, $Offset) = @_;
      skip_declarator;
      my $name = strip_name;
      my $proto = strip_proto;
      my @decl = $proto_parser->($proto);
      my $inject = $proto_injector->(@decl);
      if (defined $name) {
          $inject = scope_injector_call().$inject;
      }
      inject_if_block($inject);
      if (defined $name) {
          $name = join('::', Devel::Declare::get_curstash_name(), $name)
            unless ($name =~ /::/);
      }
      shadow(sub (&) {
          no strict 'refs';
          my $code = shift;
          $install_cb->($name, $code, \@decl);
          # So caller() gets the subroutine name
          *{$name} = subname $name => $code if defined $name;
      });
      };
  }

  sub inject_scope {
      $^H |= 0x120000;
      $^H{DD_METHODHANDLERS} = Scope::Guard->new(sub {
          my $linestr = Devel::Declare::get_linestr;
          my $offset = Devel::Declare::get_linestr_offset;
          substr($linestr, $offset, 0) = ';';
          Devel::Declare::set_linestr($linestr);
      });
  }

  1;


Some of my favorite applications


In no particular order, the following applications make my life so much easier:

  • debian/ubuntu
  • gaim
  • galeon
  • gnome
  • liferea
  • pan
  • perl
  • privoxy
  • rhythmbox
  • subversion
  • thunderbird
  • vim

Email::MIME::* modules aren’t very close friends


The Email::* modules are all pretty neat, with a pleasant API. But combining them isn’t always as effortless as you’d expect.

For my email2blog script, I use Email::Filter as the bridge between the MTA and blosxom:

 # grab email from STDIN
 my $mail = Email::Filter->new;

The $mail object has nifty methods that make it very simple to accept or reject a message.

Once I accept a message, I take out the attachments with Email::MIME::Attachment::Stripper. The first step is to construct a stripper based on the incoming email:

 # make stripper
 my $strip = Email::MIME::Attachment::Stripper->new(
             Email::MIME->new( $mail->simple->as_string ),
             force_filename => 1
 );

I’m not impressed with the hoops I have to jump through there. It should be easy for EMA::Stripper to build its own Email::MIME object when handed an Email::Simple object. But in fact, even Email::MIME itself doesn’t offer that option.

Next up is taking apart the MIME email:

 # strip attachments
 my $msg = $strip->message;
 my @attachments = $strip->attachments;

That’s pretty straightforward. $msg is an Email::MIME object, without the attachments. To get at the plaintext body is still not very comfortable:

 # extract plaintext body
 my $text = first { $_->content_type =~ m{text/plain} } $msg->parts;

Maybe that’s because there may not be a text/plain part with meaningful content, but I wouldn’t expect $msg->body to be empty after the strip operation. It is though, so that fancy grep is necessary.

The @attachments array on the other hand is very easy to work with. It has just the things you need, and nothing else.

All in all, the script ended up being about 60 well-spaced lines long, with most of that being taken up by sanitizing the input. The fact that I didn’t actually have to think how MIME encoding works under the hood was a big plus, and for that I’m very happy with the PEP Project.


First impressions


I installed Blosxom today. Getting it up and running was a piece of cake. Finding useful plugins took a little longer, but I’m happy with the current set:

I stole the layout from my Perl pages.


Test Driven Development and refactoring


On Safari Bookshelf:

  • “Test-Driven Development By Example”, Kent Beck
  • “Perl Testing: A Developer’s Notebook”, chromatic , Ian Langworth
  • “Refactoring: Improving the Design of Existing Code”, Martin Fowler, Kent Beck et al

On PerlMonks:

Other books I’ve heard good things about:

  • “Code Complete”, Second Edition, Steve McConnell
  • “The Pragmatic Programmer: From Journeyman to Master”, Andrew Hunt, David Thomas

Tidyhtml 0.01


Initial version

I’ve just written a tiny blosxom plugin that cleans up your html. It uses the 1.07_01 dev version of HTML::Tidy, which in turn uses libtidy.

Since this is the first ever version, I’m just going to paste it here. I’ll build a proper release later on.

# Blosxom Plugin: tidyhtml
# Author(s): Rhesa Rozendaal  
# Version: 0.01
# URL: http://oss.rhesa.com/blog/text/blosxom/plugins/tidyhtml

package tidyhtml;
use strict;

# --- Configurable variables -----

my $tidy_config = {
    tidy_mark     => 'no',
    wrap          => '120',
    indent        => 'auto',
    output_xhtml  => 'yes',
    char_encoding => 'utf8',
    doctype       => 'strict',
    add_xml_decl  => 'yes',
    alt_text      => 'photo',
};

# --------------------------------

use HTML::Tidy;

sub last {
    # only operate on html content types.
    return unless $blosxom::header->{-type} =~ /html/;

    $blosxom::output = HTML::Tidy->new( $tidy_config )
                                 ->clean( $blosxom::output );
    return;
}

sub start { 1 }

1;


JavaScript-driven syntax highlighting


Based on a PerlMonks idea, I’ve added syntax highlighting on code blocks. The javascript is here, and the css here.

See this post for an example (you need javascript enabled for this).


Markdown


Markdown is neat.


This time, with attachments


crocs for teh win

Let’s hear a “YAY!” :-)


A couple of Vim links



Emacs is far too small to be a proper kitchen sink — david feuer


Testing my email2blog script (take 2)


Ok, so mailing works, and the file gets stored in the proper location. Now, lets see if the permissions work out.


Magic SysRq


Marc from our hosting provider pointed me to /proc/sysrq-trigger. Useful if something’s wedged, and you can’t get a box to reboot in the normal way.

This page has more details, as it doesn’t seem to be documented in a normal man page.

The capabilities should be documented in “man proc”, but (at least on my system) are not. The “sysrq” is mentioned, but not documented. You can find documentation in /usr/src/linux-2.4.29/Documentation/sysrq.txt if you have source installed. This is from that file:

'r'     - Turns off keyboard raw mode and sets it to XLATE.

'k'     - Secure Access Key (SAK) Kills all programs on the
          current virtual console. NOTE: See important comments
          below in SAK section.

'b'     - Will immediately reboot the system without syncing
          or unmounting your disks.

'o'     - Will shut your system off (if configured and supported).

's'     - Will attempt to sync all mounted filesystems.

'u'     - Will attempt to remount all mounted filesystems read-only.

'p'     - Will dump the current registers and flags to your console.

't'     - Will dump a list of current tasks and their information
          to your console.

'm'     - Will dump current memory info to your console.

'0'-'9' - Sets the console log level, controlling which kernel
          messages will be printed to your console. ('0', for
          example would make it so that only emergency messages
          like PANICs or OOPSes would make it to your console.)

'e'     - Send a SIGTERM to all processes, except for init.

'i'     - Send a SIGKILL to all processes, except for init.

'l'     - Send a SIGKILL to all processes, INCLUDING init.
          (Your system will be non-functional after this.)

'h'     - Will display help ( actually any other key than those
          listed above will display help. but 'h' is easy
          to remember :-)

The key is also self-documenting: doing Alt-Sysrq-h (or any other unrecognized key) prints out a HELP message that briefly reminds you of the above functions. If you find that holding three keys is difficult, you do have some other choices. You can trigger it manually:

echo t > /proc/sysrq-trigger


Markdown sample text


This all the example text from Markdown basics.

Paragraphs, Headers, Blockquotes

A First Level Header
====================

A Second Level Header
---------------------

Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.

The quick brown fox jumped over the lazy
dog's back.

### Header 3

> This is a blockquote.
>
> This is the second paragraph in the blockquote.
>
> ## This is an H2 in a blockquote

Phrase Emphasis

Some of these words *are emphasized*.
Some of these words _are emphasized also_.

Use two asterisks for **strong emphasis**.
Or, if you prefer, __use two underscores instead__.

Lists

bulleted:

* Candy.
* Gum.
* Booze.

numbered:

1. Red
2. Green
3. Blue

multiline:

* A list item.

  With multiple paragraphs.

* Another item in the list.

Links

This is an [example link](http://example.com/).

This is an [example link](http://example.com/ "With a Title").

I get 10 times more traffic from [Google][1] than from
[Yahoo][2] or [MSN][3].

[1]: http://google.com/        "Google"
[2]: http://search.yahoo.com/  "Yahoo Search"
[3]: http://search.msn.com/    "MSN Search"

I start my morning with a cup of coffee and
[The New York Times][NY Times].

[ny times]: http://www.nytimes.com/

Images

![alt text](/path/to/img.jpg "Title")

![alt text][id]

[id]: /path/to/img.jpg "Title"

Code

I strongly recommend against using any `<blink>` tags.

I wish SmartyPants used named entities like `&mdash;`
instead of decimal-encoded entites like `&#8212;`.

If you want your page to validate under XHTML 1.0 Strict,
you've got to put paragraph tags in your blockquotes:

     <blockquote>
         <p>For example.</p>
     </blockquote>

A First Level Header

A Second Level Header

Now is the time for all good men to come to the aid of their country. This is just a regular paragraph.

The quick brown fox jumped over the lazy dog’s back.

Header 3

This is a blockquote.

This is the second paragraph in the blockquote.

This is an H2 in a blockquote

Some of these words are emphasized. Some of these words are emphasized also.

Use two asterisks for strong emphasis. Or, if you prefer, use two underscores instead.

bulleted:

  • Candy.
  • Gum.
  • Booze.

numbered:

  1. Red
  2. Green
  3. Blue

multiline:

  • A list item.

    With multiple paragraphs.

  • Another item in the list.

This is an example link.

This is an example link.

I get 10 times more traffic from Google than from Yahoo or MSN.

I start my morning with a cup of coffee and The New York Times.

alt text

alt text

I strongly recommend against using any <blink> tags.

I wish SmartyPants used named entities like &mdash; instead of decimal-encoded entites like &#8212;.

If you want your page to validate under XHTML 1.0 Strict, you’ve got to put paragraph tags in your blockquotes:

 <blockquote>
     <p>For example.</p>
 </blockquote>


Vim config, plugins


My preferred editor is Vim. I use a couple of plugins that make it even nicer:

My local vimrc has a couple of niceties too.


Climate variability and climate change: some semantics


This page shows how variability and change differ using a couple of simple graphs.


New version of “tidyhtml”


Slightly cleaned up, and made sure we’re outputting proper utf-8.

Grab tidyhtml 0.03 here and enjoy :-)


The “feedback” plugin


Took some tweaking to get it to work. It only works on single-story pages, which aren’t accessible with the vanilla blosxom flavours.

I had to tweak my story.html a bit to give me links to a single story:

<p>
    <a name="$fn"
       href="$blosxom::url$blosxom::path/$fn.$blosxom::flavour"
    >
        <h2 class="pagetitle">$title</h2>
    </a>
    <br />
    $body
</p>

By putting the href attribute on the anchor, you get to the actual story.

I also added a bit of fluff to make a nice footer:

<p class="storylinks">
<span id="commentlink">
    <a href="$blosxom::url$blosxom::path/$fn.$blosxom::flavour">
        comments: $feedback::comments_count
    </a>
</span>
<span id="permalink">
    time: $hr:$min <i>[$directorybrowse::browseable_path ]
    <a href="$url/$yr/$mo_num/$da#$fn">permalink</a></i>
</span>
</p>

Adding this css makes it look decent:

p.storylinks #commentlink {
    text-align: left;
    width:100px;
    float: left;
}
p.storylinks #permalink {
    text-align: right;
    position: relative;
    display: block;
    margin-left:100px;
}

Once that was in place, the feedback plugin works like a charm. I like the moderation, and it looks reasonably secure.


Some references on OOP, Design patterns and Best Practices


General reading

On Safari Bookshelf:

  • “Advanced Perl Programming, 2nd Edition”, Simon Cozens
  • “Perl Best Practices”, Damian Conway

Elsewhere:

Advanced stuff


Testing my email2blog script


I’ve just written a small Email::Filter script that will allow me to post using email.

We’ll see; if this shows up, it works :-)


Handy Blosxom links



Galeon + Privoxy = nice!


After getting increasingly annoyed with Firefox and its crazy memory leaks, I temporarily switched to Opera. After dealing with the ugly QT layout (by using the beautiful “tango” skin), it was mostly enjoyable. But some issues cropped up (horrible javascript performance mostly), so I went to look for another browser.

I looked through Ubuntu’s applications, and tried Galeon, Epiphany and w3m.

  • w3m is awesome, but for my work I really need a graphical browser with JavaScript support
  • Epiphany seemed to lack features I felt I needed
  • Galeon looked OK, and soon proved to be my favorite

The biggest thing missing from all of them was AdBlock support. But after some googling, I found a lead to Privoxy. What a find! It’s absolutely painless to install, and Gnome has a global http proxy setting, which makes Privoxy immediately useful for every browser I care to use. Brilliant!

Life is good again :-)