A Foolish Manifesto

fREWdiculous!

Switch to Catalyst!

So this week, as previously alluded to, I convinced my boss to let me switch my current app from CGI::Application to Catalyst. I had gotten the book in the mail and I showed it to him to make the point that it’s a serious framework. Fortunately the switch has been mostly painless. The first reason being that our controller is pretty bare right now aside from validation, which took about a day to get entirely ironed out.

The interesting thing for me, most of all, is that I have gotten pretty good at writing regular expressions with vim to search and replace for CGIApp-isms to replace with Cat-isms.

Here are a list of some of the big ones:

Simple replacements:

1
2
:%s/return/$c->stash->{json} =
:%s/$self->query->Vars/$c->request->params

More complex stuff

1
2
3
:%s/$self->query->param(\(.\{-}\) = $c->request->params->{\1}
:%s/method (.\{-}) : Runmode/method \1($c) : Local
:%s/$self->schema->resultset(\s*'\(.\{-}\)'\s*) = $c->model('DB::\1')

If you know anything about regular expressions you know that the \1 means the first back reference. Now, vim’s regex flavor is a little strange because it is optimized for searching for plain text, so *most* characters default to literals. That’s why I have to escape the parentheses to make a matching group. Also note the following unusual construct: .\{-} . That’s the same as .*? in Perl. That’s actually surprisingly important.

Anyway, this switch has been fairly fun and exciting. The best part being the inimitable structure of a Catalyst application. For example, the fact that we have a dev server with lots of affordances for (duh) developers other than little setup is great, and built in config file reading is something that I have always wanted. We always ended up rolling our own solution in other projects, but this is really supreme since it’s in one place and not just Perl code.

An there are lots of pleasant things like how it’s really easy for our app to have both JSON and TT support. This will be really good later on when we start to do pdf printouts and whatnot. Instead of adding methods for those things into the controller, like in CGIApp, we will just add another View module.

The main thing that has weirded my out so far is that in CGIApp the App is the controller. In Catalyst you have an App, which also seems to be an instance variable, with accessors for CGI parameters and whatnot, and you also have Controllers. Anyway, I need to wrap my head around all that. Hopefully reading through the book will help with some of these issues.

How about you? Are you still happy with CGIApp? Are you adventurous enough to use Reaction?

  • 0 Comments
  • Filed under: Uncategorized
  • Chapter 7: Open Source

    Some of you probably know that I have some opinions, thoughts, and ideas. I actually started this blog because I wanted to write my own (can you guess what?) Manifesto. I chose to write it as a blog because I tend to change my mind. Ask some of my friends and family. They have all observed that I was going to be a math teacher, a psychologist, a biological engineer, a doctor, and a writer. (Take note: I am none of those things.)

    I started programming in earnest about 10 years ago, when I purchased Programming Perl. I had done some basic, but I knew that real programmers used perl. I knew I would never use any other language. 6 years later I turned my back on perl when a professor introduced me to ruby. While in ruby-land I learned functional programming, what MVC was, what an ORM is, and the beauty of syntax (I still dig 5.times {…}). I knew that Rails was the One True way to program websites and that Prototype and Scriptaculous were the only way to program javascript.

    Then 4 years later someone offered to pay me to write perl. I came back somewhat grudgingly, and I came extremely close to trying to write a certain project with rails. Fortunately (for my current dogmatism) my boss convinced me to stick with perl. Somewhere along the line I learned that perl 6 is truly being developed. I helped some and had some fun. I read the book currently titled The Passionate Programmer. After reading it I decided to start seriously look into switching from IIS to Apache.

    After setting up Apache on my personal computer so I could have a useful error log I started researching ORM’s. I found that The One True ORM of any given language is DBIx::Class. I will never use another ORM as long as I live. I have posted about it a few times now. I’ll leave that at that.

    Larry Wall says that the three programming virtues are laziness, impatience, and hubris. I agree with his conclusions. The first two often lead to code reuse. Code reuse is an excellent goal. Code reuse is what keeps my current codebase nimble and exciting to work on.

    Part of code reuse means using libraries to help you get your job done. Did I mention DBIx::Class? Yeah, it helps me get my job done. Now, when I first started getting paid to code I was told that in a professional context, we don’t waste our time reinventing the wheel. Agreed! Let us not reinvent the wheel.

    So instead of reinventing the wheel, we’ll purchase a library that does the job for us find some Open Source library that does (or almost does) the job for us. Before I got further I’d like to make a few points about Open Source software. (Also, let me remind you that I am Holden Caulfield right now so I may be lying, on purpose or accident.)

    I do not use Open Source software because I desire or need freedom. My political friends tell me that I am spoiled for saying that freedom is not the highest virtue and that I would not be willing to die for freedom. There are other virtues that I (hope) would be willing to die for, but that’s another chapter.

    I do not use Open Source software because I am poor. I purchase indie video games because they are awesome works of art and they are not cheap. I donate to Open Source and otherwise free software that I regularly use because I am glad to pay for the excellent work that someone will do to make my job/life easier. I think that it is fine to ransom features as an Open Source programmer.

    I do no use Open Source because I am a communist. There is no reason that a programmer should give you his time and effort for free. Let me redact that statement: there is no reason that a person should give you his time and effort for free. If you view me as a carbon offset to the earth and that everything I do should be given to the poor, that’s fine. We are all wrong sometimes. Let me be clear: I love Ayn Rand as a phiosophess and I agree with her unconditionally.

    I use Open Source software because I am a programmer. Jeff Atwood says that “If it’s a core business function, write that code yourself, no matter what.“. I agree Jeff! The problem comes when you purchase an over-the-counter library, it suits your fancy perfectly, and then six months later, as always happens, the customer wants more. The library no longer works for you, so you either pay the Closed Source vendor to implement the features you need, or find another library and port all of your code to that.

    This is what happens to me: I use an Open Source library that does what I need. I eventually outgrow it or it doesn’t meet a specific need, I either whine enough to get someone to add the feature I need, or I figure out how to add it myself. I’m not even a very good programmer; I just really like to program.

    Let me put it another way: do you have any friends who really like to work on their car? Do they buy the brand new drive-by-wire automatic Toyota that is more black box than car?

    This post is pushing up against the thousand word mark and we certainly wouldn’t want to go there, so I’ll repeat myself one more time: I am a programmer. I will continue to use Open Source software because I love to program and because I don’t want any Golden Handcuffs.

    So programmers, ask and it will be given to you; seek and you will find; untar and the code will be opened to you. Suits: feel free to purchase black boxes as Golden Handcuffs. Thank you and have a nice weekend.

  • 3 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
  • 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.

  • 1 Comment
  • Filed under: Uncategorized
  • Vim Tip of the Day

    I post on forums and mailing lists fairly often, and when I copy and paste code I tend to shift all the code to the left 6 or more characters so that the forum won’t see unnecessary indentions. Well, today I found the solution: blockwise selection. To do this in vim you will just type Ctrl-V and you are in blockwise selection mode.

    You also probably want to do a :set virtualedit=block so that you can highlight characters that don’t exist.

    Anyway, it’s nice because instead of highlighting lines it highlights a square, so you just start at the beginning line and all indents are removed like magic!

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

    A coworker called me and asked me what it meant that when vim opened a file there were a bunch of ^@’s in it. I didn’t know off hand, but I did know that vim had some feature to print out the ascii value of the character that the cursor is over. So without further ado:
    :as or ga

    Enjoy!

  • 0 Comments
  • Filed under: Uncategorized
  • Splits, panes, and tiles

    How do you manage numerous windows when you have a gigantic viewing space? Or what if you have a really tiny viewing space? At work I have two 22″ monitors and maximization is just too ridiculous to consider and it is typically a huge waste of space.

    I decided that if I am going to have a lot of windows open I should look into something that can help me tile things correctly. A lot of the tiling solutions out there are all or nothing. Once you use them you can no longer have overlapping windows, you must use their strange layouts, and if you don’t know their hotkeys you can’t use your computer. I decided that for now, none of those are an option.

    Instead I found a program called GridMove. It allows you to have numerous grids to snap windows into. You can achieve “snapping” with mouse interaction or with various hotkeys. Overall I have been extremely happy with it.

    Work Splits

    I even wrote a tiny AHK script to open programs for one of my projects and place them in the right place.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #o::

       Run "C:Documents and SettingsfrewMy DocumentsCodeaircraft_ducting"
       WinWait, "C:Documents and SettingsfrewMy DocumentsCodeaircraft_ducting",,3
       sendinput,#6
       Run "C:Program FilesApache Software FoundationApache2.2logserror.log"
       WinWait, BareTail,,3
       sendinput,#5
       Run "C:Program FilesVimvim72gvim.exe" -c "call ACDRI()" -S C:Documents and SettingsfrewMy DocumentsCodeaircraft_ductingsession
       WinWait, GVIM,,3
       sendinput,#4
       Run "C:Program FilesMozilla Firefoxfirefox.exe" -new-window http://localhost:8080/devcgi/init.plx
       WinWait, "Mozilla Firefox",,3
       sendinput,#1

    return

    But that’s not all! I tend to use splits in my text editor a lot, and with that in mind you may notice the about call to ACDRI() and the S switch. S reloads all of my previous editor settings, including splits, and ACDRI() opens my project file.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    function! ACDRIEnd()
       wincmd h
       wincmd h
       wincmd h
       q!
       mksession! "C:Documents and SettingsfrewMy DocumentsCodeaircraft_ducting"session
       qa
    endfunction

    function! ACDRI()
       Project "C:Documents and SettingsfrewMy DocumentsCodeaircraft_ductingproject"
       set foldlevel=1
    endfunction

    And lastly, at home where monitor space is much more of an issue, I tend to have gvim and a console side by side, with numerous splits each. The console splits are achieved with ^a S and ^a |. You can move to the next one with ^a .

    Linux Laptop Screenshot

    Anyway, does anyone have any other tips having to do with automated window management?

  • 3 Comments
  • Filed under: Uncategorized
  • Vim Settings

    Today I am just going to talk about my favorite vim “stuff.” A lot of this I have gathered over the past 3-5 years of serious vim usage. I used vim before that, but not with this heavy of customization. I’ll start with the simple stuff and move up from there.

    Basic settings:

    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
    " Enable Line Numbers
    set number

    " Ignore case for searches
    set ignorecase

    " Unless you type an uppercase letter
    set smartcase

    " Incremental searching is sexy
    set incsearch

    " Highlight things that we find with the search
    set hlsearch

    " This is totally awesome - remap jj to escape
    " in insert mode.  You'll never type jj anyway,
    " so it's great!
    inoremap jj <Esc>

    " If you have caps lock on disable too many J's
    nnoremap JJJJ <Nop>

    " Set off the other paren
    highlight MatchParen ctermbg=4

    " no longer press shift to enter commands
    nnoremap ; :
    nnoremap : ;
    vnoremap ; :
    vnoremap : ;

    I also highly recommend the InkPot color scheme for gui mode and metacosm for console mode. Here’s some code to pull that off:

    1
    2
    3
    4
    5
    6
    " Favorite Color Scheme
    if has("gui_running")
       colorscheme inkpot
    else
       colorscheme metacosm
    endif

    I also use some plugins to make my life easier.

    • matchit: allows you to match things like html tags and other complicated matches with %.
    • NERD_commenter: simple keystrokes to comment/uncomment any code.
    • project: this allows you to have a user defined file listing on the left (or right) which I find quite nice for navigation. You can also use it to have special mappings defined when you open a file in a specific project.
    • surround: surround anything with quotes, parens, braces, xml tags… Great for html editing, but I use this all the time for normal code. (And I just used it to put all of these items in a list easily!)

    Do you have any suggestions for killer additions to vim?

  • 2 Comments
  • Filed under: Uncategorized