Textmate + Perl + New Package

| | Comments (0) | TrackBacks (0)
Everytime you create a new file in your project you need to type the package name:
lib/My/Project/DB/Result/Person.pm => My::Project::DB::Result::Person
That's really annoying, boring and prone to error. For Textmate users there's a simple solution (notice: the script expects your modules in directory "lib"):
#!/usr/bin/env perl
use strict;
use warnings;

my $package = get_package( path => $ENV{TM_FILEPATH}, walk_up_to => 'lib' );
my $content = get_content();

if ($content) {
    $content =~ s/^package (.*?);/package $package;/;
    print $content;
}
else {
    print "package $package;\n\nuse strict;\nuse warnings;\n\n1;";
}

sub get_content {
    local $/ = undef;
    return <>;
}

sub get_package {
    my (%args) = @_;
    my @chunks = split qr~/~, $args{path};
    while ( @chunks > 0 ) {
        last if $chunks[0] eq $args{walk_up_to};
        shift @chunks;
    }
    shift @chunks;
    my $pkg = join '::', @chunks;
    $pkg =~ s/\.pm//g;
    return $pkg;
}

Save that script and setup a new command in Textmates bundle editor as shown in the screenshot:
bundle editor
Next time you create a new perl module just hit Cmd + Ctrl + p and select "Filename to Package":
context menu
If the file is empty the script will insert following code:
package My::Project::DB::Result::Person;

use strict;
use warnings;

1;
If the file contained code already the script will just update the "package ...;" line with the new value. This is very handy if you move a file around and just want to update the package name.

0 TrackBacks

Listed below are links to blogs that reference this entry: Textmate + Perl + New Package.

TrackBack URL for this entry: http://www.pqpq.de/mt-tb.cgi/31

Leave a comment

About this Entry

This page contains a single entry by Johannes Plunien published on May 28, 2009 7:48 PM.

Catalyst + DBIC + DAO was the previous entry in this blog.

Gearman::Driver is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.