A Foolish Manifesto

fREWdiculous!

Archive for the ‘Uncategorized’ Category

Contributing to Open Source

I’ve used Open Source for a little over ten years now. I’ve been sufficiently indoctrinated that Open Source (Free Software) is both morally and technically the right choice. That’s not what this post is about. If you disagree with those premises, that’s fine. The idea here is that I use all kinds of Free Software all the time. I use Vim for a text editor. I use zsh as a shell. Firefox is my browser. This blog runs on WordPress. The webserver we use at work is Apache. And the of course all of our code depends on Perl and numerous libraries.

We don’t pay for any of that software! Not a dime! And that’s fine, but nothing comes for free.

So far I’ve worked on three open source projects. The first was TOME, a book sharing system we used at school. Next I wrote some of the spec tests for Perl 6. And then most recently I’ve been doing some things for DBIx::Class.

One of the excellent things about the DBIx::Class developer community is that they really do their best to help you to work on the source.yourself. Recently they (or more specifically ribasushi) helped me add the full sorting capabilities to the SQL Server parts of DBIx::Class. More lately I’ve been adding things for the paging capabilities, which is great because paging with SQL Server is horrendous.

Anyway, the most important part of all of this is that I am part of something that will help me and other people. Furthermore, it really wasn’t that hard to add the code. They showed me where to add it, did a little code review to help me clean it up, and that was it! If only more communities were like that the Open Source world might be even more vibrant.

  • 0 Comments
  • Filed under: Uncategorized
  • Avatar!!!

    I recently purchased an Avatar to be created by Scott Meyer of Basic Instructions. Today he sent me the completed avatar. Here it is:

    This is me!

    This is me!

    Pretty sweet, huh? Anyway, I figured this would be cool, because I get to look cool and support an excellent webcomic/artist.

    Get your own here!

    Oh yeah, and maybe you want to see the original. That was done by my roommate at the time.

  • 0 Comments
  • Filed under: Uncategorized
  • Vim Feature of the Day: gv

    Have you ever highlighted something in vim, yanked it, and then realized you wanted to yank it to a different buffer, often + or *? Well, try the command gv. It will highlight whatever you had previously selected. I probably use it at least once a day.

    Enjoy!

  • 0 Comments
  • Filed under: Uncategorized
  • WorldOfGoo

    I just completed World of Goo (or buy direct, here). Very fun game!

    I like video games, but I tend to not play them very much because I do all kinds of other things (lots of programming if you can’t tell :-) ) but recently I’ve found that they help me clear my mind when I am trying to figure stuff out. Like, I’ll be coding and I will usually get stuck on a design issue. It’s rarely a question of how to get it done, but more, what’s the best way to get it done. I certainly don’t always choose the right answer, but I try to go back and correct wrong answers as soon and as often as possible.

    Furthermore, I really appreciate these indie games. They have something in common with what I call art (a post for another day.) Here’s an interesting factoid: World of Goo was made by four people. Only two of which were coders! That blows me away.

    Anyway, the game was great; it installed and ran without a hitch on Ubuntu; so try out the demo, and if you dig it, purchase it.

  • 0 Comments
  • Filed under: Uncategorized
  • So I’d like to do a post on CGIApp and Catalyst. People on IRC keep telling me that using CGIApp is wrong (mostly because they’ve never used it) and that I should switch to Catalyst.

    Catalyst may be great, but I haven’t seen any solid posts about how Catalyst is great. So help me out. Ignoring the fact that Catalyst is what everyone uses (so there are lots of plugins for it) what makes it so good?

  • 3 Comments
  • Filed under: Uncategorized
  • Windows Agony: Con

    At $work I manage the subversion repositories for all of the software that we develop. It’s certainly not something that I’m great at, but I’ve used it longer than most so I am the most qualified to deal with it.

    Furthermore, at work we use this tool (Freescale?) which, when it creates a project, creates a Boot directory and a Con directory. Ok, so I had helped our head honcho EE create a repository to store his project data and versions. He’s puttering along and he thinks, “Hey, I want to ‘save’ this version so I can go back to it later!” So I explain to him tags and how to set it up and all this jazz. Well, it turns out that when we made the repo initially we did not make tags, trunk, and branches, like we should have. We just put everything in the root of the repo. Foolish! So anyway, I tell him that we can reorganize it fairly easy and we do that. So we make the changes, delete the old directory, and recheckout from trunk…

    It failed. It could not check out the directory! Some of you may be able to guess why: in Windows you cannot (easily) create a directory named “con” (or com, or a few others.) So we are having the hardest time getting it to check out. Meanwhile he has to make a release for the customer and I am under the gun. So he pulls up a copy he made (how?) and gets back to work and I try to figure out how to deal with this in my office. At this point he has asked me to just revert the changes.

    So I go back to my office, try checking it out a few different ways and have no luck. So finally I get an idea, I figure I’ll check it out in a virtual machine with Linux installed! So I do that, I run the reverse merge to undo all of our changes, and I check everything back in. It worked!

    So the moral of the story? Don’t name folders “con.”

  • 2 Comments
  • Filed under: Uncategorized
  • DBIC’d

    This is a blogish version of a message I posted to the DBIC Mailing list recently.

    First off, this is my table structure:

    User has many Roles (Role belongs to User)
    Role has many Permissions (Permission belongs to Role)
    Permissions has many Screens (Screens has many Permissions)
    Screens belongs to Section (Section has many Screens)

    So I thought I could do this:

    1
    2
    3
    4
    5
       my @sections = $user->roles
          ->related_resultset('permissions')
          ->related_resultset('screens')
          ->related_resultset('section')
          ->all;

    But related_resultset doesn’t work with many_to_many because it’s not a “real” relation (I’d like to hear about why that is at some point.)

    The following is close to what I wanted

    1
    2
    3
    4
    5
    6
    7
       my @sections = $user->roles
          ->related_resultset('role_permissions')
          ->related_resultset('permission')
          ->related_resultset('permission_screens')
          ->related_resultset('screen')
          ->related_resultset('section')
          ->all;

    But it turns out it returns a section per role, which often means duplicates.

    So I figured I could do a distinct, so I finally tried this:

    1
    2
    3
    4
    5
    6
    7
       my @sections = $user->roles
          ->related_resultset('role_permissions')
          ->related_resultset('permission')
          ->related_resultset('permission_screens')
          ->related_resultset('screen')
          ->related_resultset('section')
          ->search(undef, { distinct => 1 });

    And it worked! How cool is that?

    I actually later on ended up only getting the screens and then getting the sections based on that, otherwise we got false positives on the sections. Anyway, now we have a nice roles/permission based tree getting built on our app for the navigation.

    And this next little trick could be an entire post in itself, but my Draft queue is getting pretty huge, so I’ll just include it here:

    1
    2
    3
    4
    5
    6
    7
      $cd_rs->search({
        artist_id => {
          in => $artists_rs->search({
            name => { like => '%beat%'},
          })->get_column( 'id' )->as_query
        },
      });

    So basically what this does is a subselect. DBIC is very much strives to be consistent throughout, which brings us the amazing new as_query method. This turns the given resultset into a data structure, which can then be passed to other resultsets searchs to create subselects. The above search will find all of the CD’s by artists with the string ‘beat’ somewhere in their names.

    Anyway, hope you enjoyed this post. My brother is getting married in a week and my sister is graduating highschool on Tuesday. I say this because I doubt I will be able to post much next week. So worst case scenario I will post again on the first of June.

  • 1 Comment
  • Filed under: Uncategorized
  • Perl::Tidy: annoying facts

    So I was trying to use perltidy programmatically, that means using Perl::Tidy. Basically I wanted to use an existing .perltidyrc along with the backup option. That is, instead of making a new file with .tdy at the end, replace the original and back it up to .bak. So after reading the docs I figured that this should work:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
       use Perl::Tidy ();
       use File::Spec;

       my $file = File::Spec->catfile( $dir,
          $filename );

       Perl::Tidy::perltidy(
          source     => $file,
          argv        => '-b',
          perltidyrc => $perltidyrc,
       );

    Unfortunately that just doesn’t work. Here’s how I got it to work:

    1
    2
    3
    4
       Perl::Tidy::perltidy(
          argv        => "-b $file",
          perltidyrc => $perltidyrc,
       );

    I also had to modify the .perltidyrc file some as apparently Perl::Tidy doesn’t have a way to choose who wins when there are conflicts in the switches and the config file. One way or another, it was annoying.

    Maybe I was doing it wrong?

  • 2 Comments
  • Filed under: Uncategorized
  • Vim Tip of the Day

    Every now and then I want to run a given vim command on a bunch of lines. In the past I would have either executed the command and then pressed j. (Hi J-Dot!) to go down and repeat the command. Or if the command were more complex I would have used a macro and done it over and over with @@.

    Well, for simple stuff on a range there is an easier way! Lets say you want to delete the first two words of a bunch of lines you have highlighted. This is all you have to do:

    1
    '<,'>:normal d2w

    SWANK.

  • 2 Comments
  • Filed under: Uncategorized
  • Why CPAN is Awesome

    Have you ever written a server? It’s kinda fun! Yes, I’m a nerd. Anyway, I learned the easy way and the hard way to make a server in Perl yesterday. Here’s the easy way:

    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
    53
    54
    55
    #!/usr/bin/perl
     
    use strict;
    use warnings;
    use feature ':5.10';
    use Socket;
    use Carp;
    use constant PORT => 7890;
    use lib '../lib';
    use WebCritic::Critic;
     
    my $dir = shift;
    my $port = shift || PORT;
    my $proto = getprotobyname 'tcp';
     
    # create a socket, make it reusable
    socket SERVER, PF_INET, SOCK_STREAM, $proto or
       croak "socket: $!";

    setsockopt SERVER, SOL_SOCKET, SO_REUSEADDR, 1 or
       croak "setsock: $!";
     
    # grab a port on this machine
    my $paddr = sockaddr_in( $port, INADDR_ANY );
     
    # bind to a port, then listen
    bind SERVER, $paddr or croak "bind: $!";
    listen SERVER, SOMAXCONN or croak "listen: $!";
    say "SERVER started on port $port ";
     
    my $client_addr;
    my $critic = WebCritic::Critic->new({
       directory => $dir
    });
    while ( $client_addr = accept CLIENT, SERVER ) {
     
        # find out who connected
        my ( $client_port, $client_ip ) =
           sockaddr_in($client_addr);

        my $client_ipnum =
           inet_ntoa($client_ip);

        my $client_host =
           gethostbyaddr $client_ip, AF_INET;
     
        # print who has connected
        say "got a connection from: $client_host",
            "[$client_ipnum] ";
     
        # send them a message, close connection
        say CLIENT $critic->criticisms;
        close CLIENT or
           croak "couldn't close connection! $@";
    }

    So that’s the Perl code to make a simple server! Unfortunately it is a little incomprehensible, at least to me. A lot of that has to do with the fact that Socket is just a translation of socket.h. Why are all those functions weirdly named? What do they do? I don’t know. I don’t even care to know. Why? I’m not a C programmer.

    So I found IO::All. Check out the rewrite.

    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
    #!/usr/bin/perl
     
    use strict;
    use warnings;
    use feature ':5.10';
    use IO::All;
    use Carp;
    use constant PORT => 7890;
    use lib '../lib';
    use WebCritic::Critic;
     
    my $dir = shift;
    my $port = shift || PORT;
     
    my $socket = io(":$port") or
       croak "server couldn't load on port $port";

    say "server loaded on port $port";
     
    my $critic = WebCritic::Critic->new({
       directory => $dir
    });
    while ( my $s = $socket->accept ) {
       say "Servicing client";
       $s->print($critic->criticisms);
    }

    It’s like, half the length and so much simpler! Anyway… next up: Web Based, AJAX-y, “threaded” version of PerlCritic coming up soon! (I am using it at work :-) )

  • 4 Comments
  • Filed under: Uncategorized