Skip to content

Instantly share code, notes, and snippets.

@plu
Created November 23, 2010 16:42
Show Gist options
  • Save plu/712069 to your computer and use it in GitHub Desktop.
Save plu/712069 to your computer and use it in GitHub Desktop.
#!/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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment