A Foolish Manifesto

fREWdiculous!

Using search.cpan.org AND metacpan

I appreciate the effort and openness of metacpan, but their search is still pretty bad. To be clear, compare the results of the search for DBIx:Class::Source on SCO and metacpan. That’s why I made the following greasemonkey/dotjs script:

1
2
3
4
5
6
$('a').each(function(i,x){
   var obj = $(this);
   var href = obj.attr('href');
   var re = new RegExp('^/~([^/]+)/(.*)$');
   this.href = href.replace(re, 'https://metacpan.org/module/$1/$2');
})

Put this in ~/.js/search.cpan.org.js to install it with dotjs. Feel free to extend it to work for more than just modules.

  • 2 Comments
  • Filed under: Uncategorized
  • In the spirit of one of my other posts I’ve decided to chronicle my path with at least a couple event loops.

    More than eighteen months ago I documented my decision to start using an event loop as it would handle things I may not have considered, the example mentioned specifically in that post being exceptions. Things went well! I used the code I documented in that post for a long time with no issues until recently. It turns out that the event loop I was using didn’t actually handle exceptions at all, thus completely nullifying my reason to use it.

    So I looked elsewhere. I looked at the grandfather of event loops, POE. I like a lot of the components that have been written on top of POE, but POE itself is frustratingly low level. That’s a topic for another post though (yes I looked at Reflex.)

    After my last post and speaking with Rocco Caputo, auther of our venerable POE, I came up with the following runner role:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    package Lynx::SMS::DoesRun;

    use Moose::Role;
    use POE;

    # this merely uses our logger etc
    with 'Lynx::SMS::HandlesDieForPOE';

    requires 'single_run';

    has period => (
       is => 'ro',
       required => 1,
    );

    has schema => (
       is => 'ro',
    );

    sub run {
       my $self = shift;

       POE::Session->create(
          inline_states => {
             _start => sub {
                $_[KERNEL]->sig( DIE => 'sig_DIE' );
                $_[KERNEL]->yield('loop');
             },
             sig_DIE => \&die_handler,
             loop => sub {
                $_[KERNEL]->delay( loop => $self->period );
                $self->single_run;
             },
          },
       );

       POE::Kernel->run;
    }

    no Moose::Role;

    1;

    This works fine. It’s (to me) a little ugly, but I imagine that I’d get used to it if I were to write much more POE. But then Rocco pointed out that maybe I’m just wasting my time with event loops for this use case. Ultimately using POE as a glorified Try::Tiny is stupid and really not even the goal. So finally I’ve ended up just a few steps beyond where I started:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    package Lynx::SMS::DoesRun;

    use Moose::Role;
    use Try::Tiny;
    use Log::Contextual qw(:log :dlog);

    requires 'single_run';

    has period => (
       is => 'ro',
       required => 1,
    );

    has schema => (
       is => 'ro',
    );

    sub run {
       my $self = shift;

       while (1) {
          try {
             $self->single_run;
          } catch {
             my $error = $_;
             log_error { $error }
          };
          sleep($self->period)
       }
    }

    no Moose::Role;

    1;

    The observant reader will notice that despite me mentioning the above use case, which is really the only important one for me given that our actual server will run all of our services in separate processes, there is still the benefit of Event Loops mentioned in the first post for development purposes (starting all services in a single program.) I have indeed converted that to POE, but that probably doesn’t matter. I run my unified service script maybe once or twice a year at this point. Here it is if anyone is interested:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    package Lynx::SMS::Runner;

    use Moose;
    use POE;

    with 'Lynx::SMS::HandlesDieForPOE';

    has tasks => (
       is => 'ro',
       default => sub { [] },
    );

    sub run {
       my $self = shift;

       POE::Session->create(
          inline_states => {
             _start => sub {
                $_[KERNEL]->sig( DIE => 'sig_DIE' );
                $self->create_children_sessions,
             },
             sig_DIE => \&die_handler,
          },
       );

       POE::Kernel->run;
    }

    sub create_children_sessions {
       my $self = shift;
       my $x = 0;
       my @tasks = @{$self->tasks};
       for my $task (@tasks) {
          POE::Session->create(
             inline_states => {
                _start => sub {
                   $_[KERNEL]->delay(loop => ($x++ / @tasks ));
                },
                loop => sub {
                   $_[KERNEL]->delay( loop => $task->period );
                   $task->single_run;
                },
             },
          );
       }
    }

    no Moose;

    __PACKAGE__->meta->make_immutable;

    1;

    I look forward to using POE for actual heavy-lifting in another one of our projects, and will post about the experience when I get there.

  • 0 Comments
  • Filed under: perl
  • Perl Event Loop

    I have some extremely basic code using AnyEvent but I recently found out that I was doing it wrong. That is, the entire reason I am using an event loop is to catch errors, log them, and keep going. That’s one of the great benefits that Catalyst gives me; I override one thing and I get universal error logging. The problem is that AnyEvent specifically does not handle this use case.

    I have a working solution, but as I am planning on rewriting our services in evented code this prohibition makes me really worried. The problem is that you can’t just know your code won’t die. Exceptions happen and as a developer of a language that’s not Java or C# I don’t know where they come from. My current solution is ok, but I don’t think it’s really viable long term. Here’s my current code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #!/usr/bin/env perl

    use strict;
    use warnings;

    use AnyEvent;
    use Try::Tiny;

    sub event {
       print "looped\n";
       die "lol" if rand() < .5;
    }

    sub NEVER_DIE {
       my $code = shift;
       return sub {
          try \&$code, catch { warn $_ } # <-- this should be logging, you get the idea
       }
    }

    my $cv = AE::cv;
    my $w = AE::timer 0, 1, NEVER_DIE(\&event);
    $cv->recv;

    This works for simple cases, but if I chose to go down this route in the long term I’d have to wrap every single code ref in NEVER_DIE, which is pretty lame.

    I looked at POE as it may support my use case better but as far as I can tell it’s support is WORSE. Here’s what I came up with:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    #!/usr/bin/perl

    use strict;
    use warnings;

    use POE;
    use Try::Tiny;

    sub handler_start {
      my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
      $kernel->yield('event');
    }

    sub NEVER_DIE {
       my $code = shift;
       return sub {
          try \&$code, catch { warn $_ } # <-- this should be logging, you get the idea
       }
    }

    sub event {
       print "looped\n";
       die "lol" if rand() < .5;
       $_[KERNEL]->delay_add('event', 1);
    }

    POE::Session->create(
     inline_states => {
       _start    => \&handler_start,
       event     => NEVER_DIE(\&event),
       _stop     => sub{},
     }
    );

    POE::Kernel->run();
    exit;

    So I still have to use NEVER_DIE, so that’s a lose, and worse, if event dies before the call to delay_add we end anyway. Sure, I could put delay_add at the beginning of event, but that brings me to another thing that really bothers me about the “POE Way” (my own terminology, I may just not be getting it), for my AnyEvent code I can add a bunch of things and they don’t have to know about each other. The loop handles calling the events. With POE it seems like I have to manually tell it “call this, now call this.” That seems to defeat the entire purpose! What am I missing here?

    If anyone knows an event loop I should consider (MUST RUN WELL ON WINDOWS) or maybe some setting in POE and some kind of POE timer thing, or some way of safely overriding how AE calls it’s events, please, comment and let me know.

  • 3 Comments
  • Filed under: perl
  • Recently I took it upon myself to make Catalyst::Plugin::Authentication know users had logged in after users had logged in in a completely non-Catalyst part of our app. After LOTS of frustration, code spelunking, and bugging a couple people in #catalyst (hobbs and t0m) I got it working.

    Basically what I did was have the session plugin look at a different cookie and load information from our own strange brew of session table. It’s not perfect, but I’m much happier with it than I was before. Here’s the code:

    First, you need to create your own Session Store, our app is called Lynx, so the namespace reflects that:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package Lynx::Session::Store;

    use strict;
    use warnings;

    use base qw/Catalyst::Plugin::Session::Store/;

    use DateTime::Format::MSSQL;
    use Catalyst::Authentication::Store::DBIx::Class::User;
    sub get_session_data {
       my ($c, $key) = @_;

       my ($k, $v) = split /:/, $key;

       if ($k eq 'session') {
          if (my $login = $c->model('DB::Login')->single({ access_num => $v })) {
             return {
                __user_realm => 'default',
                __user       => {
                   user => $login->userid,
                },
             }
          }
       } elsif ($k eq 'expires') {
          if (my $cookie = $c->request->cookie('Access_Num')) {
             if (my $login = $c->model('DB::Login')->single({ access_num => $v })) {
                my $ex = DateTime::Format::MSSQL->parse_datetime($login->last_accessed)->epoch + 720 * 60 - DateTime->now(time_zone => 'local')->offset;
                return $ex;
             }
          }
       }
    }

    sub store_session_data { }
    sub delete_session_data { }
    sub delete_expired_sessions { }

    1;

    We have stub methods for the session stuff that we don’t support. Eventually I may fill those out, but what’s more likely is that we remove this code entirely and just use what’s provided by CPA.

    Next is get_session_data, which gets arguments like session:1234 and expires:1234. They are meant to return the session data and the expiry time (seconds since epoch) respectively. Clearly I had to do a lot of really weird stuff with datetime to get that expiration date from our database, but it works, so that’s cool. You may store your expiration directly. Who knows.

    So far, so weird. Then I had to figure out how to “inflate” the session. The keys __user_realm and __user are hardcoded in CPA, and I kinda think they should change to just current_user_realm and current_user, or maybe catalyst-plugin-authentication-user. Whatever. But the fact is they are what they are. The value for __user_realm is which realm is currently selected. I imagine the vast majority of people should have that set to default, as they typically only have a single realm (we actually have two, but I didn’t realize till this code broke in a special way.) The value for __user is not a user object, but instead what get’s passed to the auth store’s from_session method. I am mostly sure about that, but it’s a pretty deep stack trace at that point.

    Next up I made a Session subclass:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package Lynx::Session;

    use strict;
    use warnings;

    use base qw/Catalyst::Plugin::Session/;

    sub sessionid {
       my $c = shift;

       my $access_num;
       if (my $cookie = $c->request->cookie('Access_Num')) {
          $access_num = $cookie->value;
       }

       return $access_num;
    }

    1

    This is clearly pretty basic. I just overrode sessionid to look at our cookie to get the sessionid.

    After that I just loaded the plugins I needed and configured CPA:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    ...
    use Catalyst qw(
       Authentication
       +Lynx::Session
       Session::State::Cookie
       +Lynx::Session::Store
    );
    ...
       'Plugin::Authentication' => {
          default => {
             credential => {
                class => 'Password',
                password_field => 'password',
                password_type => 'clear'
             },
             store => {
                class => 'DBIx::Class',
                user_model => 'DB::User',
             },
          },
       },
      ...

    Note that the credential is unused in my use case as catalyst doesn’t do the actual authentication at all.

    Hope this helps someone!

  • 0 Comments
  • Filed under: Uncategorized
  • Cloning Objects in Perl

    Recently I needed to do some deep cloning of some objects at work. I think I ended up looking at all of the major ways to do it, and I figure I might as well discuss them here.

    What is deep cloning?

    Nearly everyone should be able to answer this, but it doesn’t hurt to define it anyway. Deep cloning means you clone other things the current object is related to, recursively. So while a shallow clone of a hashref (in Perl) would be merely:

    1
    my $clone = { %{ $other_hash_ref } };

    That doesn’t do if the things in the hash get mutated and are also references, because in that case you’ll be modifying parts of the other hash, possibly surprisingly.

    Isn’t this solved?

    Well yes. If it’s something as basic as a simple data structure you can just use Storable. The code for above would become:

    1
    2
    use Storable 'dclone';
    my $clone = dclone($other_hash_ref);

    Storable has been core enough for long enough that if it’s not core you need to upgrade ;-)

    What’s your problem?

    Sadly just default Storable isn’t good enough. I needed to deeply clone the objects, but not clone any related schemata. That is, the objects had a DBIx::Class::Schema object attached to them and for various reasons I do not want to clone that at all. The correct way to deal with such an issue is to define the two Storable hooks as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    my @stack;
    sub STORABLE_freeze {
       my ($self, $cloning) = @_;

       die q(you can't freeze this thing silly!) unless $cloning;

       my %ret = %$self;

       my %frame;
       $frame{schema} = delete $ret{schema};
       push @stack, \%frame;

       return \%ret
    }

    sub STORABLE_thaw {
       my ($self, $cloning, $ice) = @_;

       die q(you can'
    t thaw this thing silly!) unless $cloning;
       my %frame = %{pop @stack};
       my $new = $self->new({
          %$self,
          map {
             $_ => $frame{$_}
          } keys %frame,
       });

       %$self = %$new;
    }

    This is a little more generic than you probably need, and came from my prototype module, Clone::Hooker, but I gave up on that as well as Storable.

    Why did you give up on Storable?

    Two reasons; first, defining the hooks above might be a bad thing. Storable is something that someone other than me may use, and by defining the hooks above I am changing the relatively generic interface of Storable for my module. Second, there’s a better alternative that I ended up using.

    WHAT DID YOU DO?!

    I ended up settling on the handy MooseX::Clone. Obviously it is for Moose modules only, but all of my modules are Moose objects in this case. It’s very simple to use, here’s how it works for me:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
       package Dashboard;

       use Moose;

       with 'MooseX::Clone';

       has gadgets => (
          is => 'rw',
          isa => 'ArrayRef',
          traits => [qw(Clone)],
       );

       1;

       package Gadget;

       use Moose;

       with 'MooseX::Clone';

       has schema => (
          is => 'ro',
       );

       1;

       my $d = Dashboard->new(
          gadgets => [
             Gadget->new(
                schema => $schema,
             )
          ]
       );

       my $cloned_d = $d->clone;

    This avoids the “global” nature of changing the interface of Storable, is fairly unobtrusive in my code, and works well.

  • 0 Comments
  • Filed under: Uncategorized
  • Shortcut Constructor Method & Conversion

    I left my book and notes at work yesterday, hence the late post.

    Shortcut Constructor Method

    What is the external interface for creating a new object when a Constructor Method is too wordy?

    Sometimes creating an object is exorbitantly wordy. The example that the author gives (in javascript) is the following:

    1
    var p = new Point({ x: 1, y: 2 })

    Add methods to a lower level object that can construct your objects. Take care to only do this rarely.

    This can’t be done with the example given in javascript, but the idea is to do something like the following:

    1
    var p = ( 1 x 2 )

    Personally, I’m very wary of this idea. I see the value, but even operator overloading, which is a step HIGHER level than this, is usually viewed skeptically. I do think it’s a good idea to make shortcut methods to instantiate related objects, but that’s a far sight better than creating a method on all integers. If you do monkey-patch something like integer, it would be best if it were done dynamically, so only the code in your own project sees it.

    Conversion

    How do you convert an object’s format to another object’s format?

    This is (at least to me) quite obvious. Some would think that they should add methods to every object to convert to other formats. So one might monkey-patch the DOM stuff to return a jquery DOM thing with the asJQDom method or something like that. Of course doing that means you’re going to end up with a ton of random conversion methods.

    Convert objects by merely instantiating the second object type

    This just seems so obvious I almost feel bad even writing it…

  • 0 Comments
  • Filed under: Uncategorized
  • I’m surprised I haven’t actually blogged this before. I had to do it recently for the first time in a long time and I figured I’d share the secret sauce.

    At work we just added a complete permission system on top of our existing user system, but we didn’t want to make the UI as flexible as the underlying code. We ended up making a single role (which has all permissions) called “Full Control”. Without that role all you get is the stuff configured directly for your user; that is, your user gets a dashboard. So instead of making a grid of roles etc etc we just made a single checkbox on the user edit form. Of course I could have put in controller code to handle this special case, but I’m trying to get better at factoring code correctly. (As an aside: two years ago I would have also put all of this in the model; the frustrating thing is that Fat Model Skinny Controller only really works for relatively small apps. I’ll try to do a blog post on why I think that at another point later :-) )

    Anyway, first off, here’s the full_control accessor I made:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    sub full_control {
       my $self = shift;

       if (exists $_[0]) {
          my $full_control = $_[0];
          if ($full_control) {
             $self->set_roles({ name => FULL_CONTROL });
          } else {
             $self->user_roles->delete;
          }
          return $full_control
       } else {
          $self->roles->search({ name => FULL_CONTROL })->count
       }
    }

    Not a whole lot going on. If an argument is passed we set the user’s roles based on the truthiness of the argument. Because the system is currently just the one role we delete all roles for clearing it. Later on if we make the system more full featured we’ll have to change this up a bit of course. If no argument is passed we just return the count of full control roles, as that approximates truthiness just fine.

    Next up are the “insert” and update wrappers. I quote insert because I actually override new:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    sub update {
       my ($self, $args, @rest) = @_;

       my $full_control = delete $args->{full_control};

       my $ret = $self->next::method($args, @rest);

       $ret->full_control($full_control);

       return $ret
    }

    sub new {
       my ($self, $args, @rest) = @_;

       my $full_control = delete $args->{full_control};

       $args->{user_roles} = [ { role => { name => 'Full Control' } } ] if $full_control;

       my $ret = $self->next::method($args, @rest);

       return $ret
    }

    The code for update should be abundantly clear. We just update the object, calling our accessor afterwards. The new code is a little bit more messy. Basically, instead of trying to use the accessor on new (which is wrong as new doesn’t actually imply an insert) we just leverage the excellent MultiCreate which DBIx::Class provides for us.

    And that’s it! I hope this helps you get your job done that much faster/better :-)

  • 0 Comments
  • Filed under: Uncategorized
  • How do you set instance variables from a constructor method?

    The fundamental issue here is that often validation is bypassed at construction time, for whatever reason. So one’s accessor may look something like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    sub x {
       my $self = shift;

       if ($self->constructing) {
         if (exists $_[0]) {
           $self->{x} = $_[0];
         } else {
           return $self->{x}
         }
       } else {
         if (exists $_[0]) {
           die 'too high!' if $_[0] > 100;
           die 'too low!'  if $_[0] < 0;
           $self->{x} = $_[0];
         } else {
           return $self->{x}
         }    
       }
    }

    Clearly this method is just doing to much. To solve this we make special set methods that are entirely to be used during construction. So in Perl this might look like the following:

    1
    2
    3
    4
    sub _set_x {
      my ($self, $x) = @_;
      $self->{x} = $x;
    }

    Interestingly, with Moose we happily side-step this issue, as the default constructor doesn’t go through the accessors and already sets the raw values.


    Ok, so I think I may start trying to apply this stuff to JavaScript instead of Perl. I almost feel like the fact that I have Moose in Perl is cheating. I know that there is Joose in JavaScript, but I’ve yet to use that in production, and I find that I have a harder time making well factored code in JavaScript than Perl. Part of that is that the underlying libraries I use in JS (ExtJS 3) are not really well factored either, but I still struggle with overall structure.

  • 1 Comment
  • Filed under: Uncategorized
  • Sadly reading is going slower than expected due to being so busy with various things in life. Oh well, just a single pattern today.

    Constructor Method

    How do you represent instantiation?

    In addition to a vanilla constructor, add methods for common cases to instantiate typical objects. For strange cases allow the use of accessors.

    Using Perl (with Moose) an example might be:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package Point;

    use Moose;

    has x => (is => 'ro');
    has y => (is => 'ro');

    sub r_theta {
      my ($class, $r, $theta) = @_;

      $class->new(
        x => $r * cos($theta),
        y => $r * sin($theta),
      );
    }

    1;

    So now both of the following work:

    1
    2
    my $p = Point->new(5, 6);
    my $v = Point->r_theta(10, 1.4);
  • 1 Comment
  • Filed under: Uncategorized
  • Today I had to spend time taking care of passport stuff for my upcoming honeymoon, so I only got to read a handful of pages. I’ll post my notes nonetheless.

    Methods are more important that state because, correctly factored, methods paper over any changes in state over time. Most of us who took OO classes in college had this hammered into our brains :-)

    Methods should be written to get something done, but should also be written to communicate with the reader. Method names like “task_1″, “task_2″, etc are completely useless for a regular person, and should be named as to what they actually do.

    Small methods are expensive in that they cost more CPU cycles and typically cause the novice trouble in following the structure of a program. On the other hand, more methods means more human readable names, easier maintenance (pinpointing changes,) and method overrideability is much more feasible with small methods.

    Composed Method

    How do you split your program into methods?

    As already mentioned, large methods are faster and easier for the reader to follow, but small methods with good names work well in the long run. A seasoned programmer is able to see a method and assume what it does without needing to read the code for it. On top of that, small methods with good names allow you to communicate the structure of your code to the reader. Also, small methods are a must for inheritance.

    Split your program into methods that do a single identifiable task.

    A Perl example might be something like:

    1
    2
    3
    4
    5
    6
    7
    sub run_app {
      my $self = shift;

      $self->intialize_app;
      $self->app_loop;
      $self->shutdown_app;
    }

    The Composed Method patter can be used in a top down fashion, that is, write your higher level methods in an almost pseudo-code fashion, and then fill in the details of the lower level methods as you work. You may also opt to use the bottom up approach of writing a larger method and splitting it into smaller methods as you notice repetition or other reusable structures. Or lastly (and I think the most new idea to me) you can use this to find holes in your API. So if an object is calling more than one method on another object, the second object probably needs to implement a method that will encapsulate the multiple calls.

  • 1 Comment
  • Filed under: Uncategorized