A Foolish Manifesto

fREWdiculous!

Archive for January, 2009

Perl6 vs Ruby: reduce

Ruby:

1
sum = (1..10).reduce {|x,y| x+y}

or maybe

1
sum = (1..10).reduce {:+}

Perl6:

1
my $sum = [+] 1..10;

That has got to be some of the sexiest perl syntax ever!

  • 1 Comment
  • Filed under: Uncategorized
  • Music

    And now on a totally different subject:

    Bands I recommend:

    • Sparkadia
    • Snow Patrol
    • CSS
    • M83
    • Steely Dan
    • The Do
    • Pacifc!
    • The Faint
    • Astronautalis
    • Gonzales
    • Gotye
    • Deastro
    • Exit Clov
    • Belle & Sebastian
    • Mother Mother
    • Jens Lekman
    • LCD Soundsystem
    • The Mountain Goats
    • Goldfrapp
    • Santogold
    • The Republic Tigers
  • 0 Comments
  • Filed under: Uncategorized
  • Winter Wonderland

    Ok, so this post is about my Christmas time of the year. The first major thing that happened was Catherine and I visited Los Angeles to see my friend J-Hay. It’s the second time we’ve gone to visit him in LA and it was a really good time. Last time we tried to fit a lot in so it was a little stressful. One major highlight was going backstage on The Office. J-Hay had found the set on accident trying to find some pizza place. So he took us there, knowing that we are huge fans. When we were looking at the place and taking pictures one of the set crew looked at us, asked if we were fans, considered for a moment, and then told us we had 2 minutes to see the backstage. He showed us some of the warehouse and some of Angela’s house (yet to be aired!)

    The rest of the trip was pretty mellow, but in a good way. We went to pinkberry (AWESOME) of course, and went to some sushi place where it was 2.50 a plate. That was great. And then last, but certainly not least, Catherine and I decided that our new nicknames for each other would be Sausage (her) and Ratjar (me.) Who doesn’t like autogenerated halo names? (pix)

    After that I had to get back to work. I only skipped 1.5 days of work for LA, so I could use flex time (work late on other days) to make up for it. There was one problem though: I hadn’t accrued as much FTO (Flexible Time Off) as I needed for all of my little vacations. So what did I do? 12 hour days of course! It’s strange getting to work at 9 and then leaving at 9. One day I am gonna sleep at work, just to say I did it…

    So next up (less than a week after LA) I went to Crested Butte, CO with some friends for skiing/snow boarding. It was Joy and C-4, Joy’s parents, and Kevin. We left Friday and got back Thursday at 1:30 AM. We skied Sun-Wed. Everyone except for Kevin wanted me to ski and Kevin wanted me to snowboard, so I flipped a coin to decide. Fate chose snowboarding so that’s what I did. It was pretty rough my first day, but I got better over the next few days and it was really fun by the time we left. Fact: The first day I snowboarded I turned 23, and the last day I snowboarded I worked over the internet.
    (pix)

    I got back to work and worked less than a weeks worth of days but more than a weeks worth of hours and then it was time to go home for Christmas! I left after work on the 23rd and arrive early in the wee hours of the morning on the 24th. It was an interesting Christmas with lots of bonus family members. I must recognize the gift that my mom got me though: since I have been more of an adult I haven’t really asked for really expensive gifts because if they are a little expensive I just buy them and if they are a lot expensive they are, obviously, too expensive. So I decided to put unusual things in my wishlist that would at least be interesting for the reader. Here is what I put this year:

    1. A Free ticket to the Universe
    2. 4.5 Extra Hours per day
    3. A way to only have to sleep 4 hours a day
    4. Any given power item (star from mario, sonic shoes, the one ring, etc)
    5. Real Magic minus the evil
    6. Musical skill
    7. A better, more convincing argument that can be used in any situation
    8. The ability to have theme music to life with the caveat that it’s
      always appropriate to the situation and it’s good music

    So mom decided to get a bunch of these, and what happened was I would open one, guess which it was supposed to fulfill on the list, and then open an envelope that went with the gift and see if I got it right. Here is how it went:

    1. Black shirt with skull singing on it: Musical Skull (mom misread it :-) )
    2. Some double shots from starbucks: a way to only have to sleep 4 hours a day
    3. A toy want with a star on it: Any given power item
    4. A piece of paper with, “…you have a point…”: a better argument
    5. A cassette tape of The Alexander Brothers’ “The Way Old Friends Do”: theme music

    That whole situation was just too funny. Catherine also came down for a few days and we had lots of fun. I also got a newer car (I got it for the sunroof…)
    (pix)

    And now I am back and everything is back to normal. Well, more than before anyway. At work I am (kindav?) heading up a new project, so that’s exciting. A coworker and I went to the customer’s site and discussed what we would do on Thursday. Over the next week or two I will be drawing up a plan and trying to estimate just how long it will really take to execute. It’s pretty exciting as we will be, hopefully, avoiding a lot of the mistakes made in our older systems.

    Ok, back to the weekend!

  • 0 Comments
  • Filed under: Uncategorized
  • Ok, so irb is totally great for testing out some syntax and general sanity checking, but we don’t really have that with perl…or do we?

    I am sure that all of the real perl hackers out there know this, but the best perl shell is your real shell. If I wanna do some cool stuff with perl I can do a lot of it directly from my shell with -e (I recommend -E as you can use ‘say’, which is helpful so that you can avoid quote issues.)

    Anyway, since my brain has been so affected by ruby functional programming I have been doing a lot of:

    1
    perl -Mautobox::Core -E '@f = (<*.foo>); @f->foreach(sub { ... })'

    So I am making an alias like this:

    1
    alias ipl='perl -Mautobox::Core -E'

    Every little keystroke matters in such a case :-)

    Anyway, hope this helps at least remind you of the power you already have.

  • 3 Comments
  • Filed under: Uncategorized
  • So I haven’t totally figured everything out about CGI::Application::Dispatch, but I am learning a lot. First off, here are two things that I learned today.

    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
    package ACD::Dispatch;
    use base 'CGI::Application::Dispatch';
    use warnings;

    sub dispatch_args {
        return {
            prefix  => 'ACD',
            debug => 0,
            table   => [
                '/'  => {
                    app => 'Welcome',
                    rm => 'index'
                },
                # The rm must be optional if you want
                # /controller to go to the startrunmode.
                ':app/:rm?/:foo?' => { },
            ],
            args_to_new => {
                PARAMS => {
                    cfg_file => '/path/to/config.pl',
                }
            },
        };
    }

    1;

    Now, notice the :foo param. If you want to get access to that in your controller you use

    1
    $self->param('foo')

    but if you had a regular parameter as well and wanted to get access to that, you’d use

    1
    $self->query->param('bar')
  • 1 Comment
  • Filed under: Uncategorized
  • This is how I think a lot of code probably looks. Although it should be in methods and stuff, here is at the very least how to do just the basics:

    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
    56
    57
    58
    59
    #!/usr/bin/perl
    use strict;
    use feature ":5.10";
    use MyApp;
    use MyApp::DB;
    use JSON;
    use Scalar::Andand;

    my $schema = MyApp::DB->connect(@MyApp::DBConnectData);

    #find a given shop
    my $shop = $schema->resultset('Shop')->find(51311);
    say $shop->ShopNo;
    say $shop->OrderNo;
    say $shop->AgentRequestedFirst;
    say $shop->AgentRequestedLast;
    say '';

    #find all shops where the AgentRequestedLast starts with Dy
    my $rs = $schema->resultset('Shop')->search({
            AgentRequestedLast => {'LIKE', 'Dy%'},
        },{
            rows => 10,
            order_by => ['AgentRequestedLast',
                        'AgentRequestedFirst', 'ShopNo DESC']
        });

    # output pages 1, 3, and 6
    foreach (1,3,6) {
        say '';
        say "Page: $_";
        my $paged = $rs->page($_);
        while (my $shop = $paged->next() ) {
            #Note: I added this method to my Shop class
            say $shop->as_string;
            # Note: Andand is awesome.  
            #Thanks groovy, raganwald, and Leon
            say $shop->ShopperDueDate->andand->day_name();
        }
    }

    # and to get some sweet, delicious JSON
    $rs = $schema->resultset('Shop');
    use DBIx::Class::ResultClass::HashRefInflator;
    $rs = $rs->search({
            AgentRequestedLast => {'LIKE', 'Dy%'},
        },{
            rows => 10,
            order_by => ['AgentRequestedLast',
                      'AgentRequestedFirst', 'ShopNo DESC'],
            columns => [qw/AgentRequestedLast
                                 AgentRequestedFirst ShopNo/]
        });
    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
    my $data = {data=>[]};
    while (my $shop = $rs->next() ) {
        push @{$data->{data}}, $shop;
    }
    say to_json($data);
  • 0 Comments
  • Filed under: Uncategorized
  • Using “Rails” wisdom in Perl

    Ok so that may be a sensational title, but really the point is this: Rails people talk a lot, perl people just get stuff done. I am ok with getting stuff done, but I don’t know how perl people do it because they don’t talk about it as much.

    Anyway, with that in mind my company (MTSI) is starting a new project next week. I get to be a big part of the planning and I am pretty excited. Normally our code is just perl scripts that use SQL and string interpolation or template toolkit. The use of TT is a big, fairly recent step forward. I recently turned a utilities file into a full on module, so that’s good too.

    But really we aren’t where we should be. The state of the art with web applications has moved forward significantly in the past few years (and I think a lot of that is because of some smart people that use rails,) and there is no reason that we cannot use this knowledge in perl.

    I originally looked at Catalyst, but decided against it because my boss thought it was a pretty big commitment for something none of us have experience with. So I decided to look at CGI::Application (which we used for TOME.)

    Before I get into that I just want to say that we have decided on DBIx::Class as an ORM. I looked at Rose::DB::Object but DBIx::Class just seems to have more polish and support. Plus they support SQL Server which we use (no comment.) DBIx::Class is fairly easy to use and next time I’m at work I’ll post a snippet of how to do various things we want to do.

    The main reasons I went with CGI::Application are these:

    1. It’s a fairly small framework, so easy to understand what’s happening.
    2. It will give us a better, more solid organization of our code.
    3. It will let us switch to mod_perl (from IIS…no comment.)

    The biggest issue with CGI::Application that I initially had was this: how can I have multiple controllers? In TOME we only had one controller but I think we should have had at least two, maybe three. Anyway, after some research I found this: Re: Re: Re: Why CGI::Application?. Basically he does what I thought that you are supposed to do, except with some excellent OO goodness.

    I was thinking that you would just have like, 5 CGI::Applications and those would be the controllers. Well, instead of that you have 5 CGI::Applications that subclass a main one which has basic functions (logging in etc) that all the other ones need. If a controller gets too big you either split it into a couple or you subclass it for a couple related controllers.

    Hopefully it goes as well in my mind as it should :-)

  • 2 Comments
  • Filed under: Uncategorized
  • Ruby style functional programming in Perl!

    So recently I was asking if andand exists in perl (here and here) and someone implemented it! How awesome is that? See it here.

    Anyway, so I looked at the code and figured, “Well heck, if it’s that easy, I should do this for map and join on arrays!”

    It was already done! The autobox::Core module does it already! You have to use more javascript-y syntax instead of regular perl-ish, but I think it makes things more clear anyway.

    Example:

    1
    2
    3
    4
    5
    6
    7
    #!/usr/bin/perl
    use feature ":5.10";
    use autobox::Core;
    my @foo = (1,2,3);

    say join( ',', map { $_ * 2 } @foo );
    say @foo->map(sub { $_ * 2 })->join(',');

    To be perfectly clear, you would probably think of the first one as: we are joining the results of the map that multiplies each item by two and the second one as: multiply each item by two and then join them with a comma.

    Anyway, I am *so* stoked to use this at work.

  • 1 Comment
  • Filed under: Uncategorized