Nice Trick to do Movable Type One-Liners in Perl

| 1 Comment | No TrackBacks |
Thanks to Google translate (and the community feed) I found out about this nice little trick (original Japanese version) by Akira Sawada today.  When doing any kind of Perl development, you can run a one-line command just by calling
perl -e 'some command'
and it will be executed.  But did you know you can also (in your MT folder) do this
perl -MT -e 'some command'
and have it executed with all of Movable Type's settings and modules etc. loaded?
Normally, to do something like that, you would need to use a line like:

perl -Ilib -Iextlib -MMT -e 'MT->new;some command'

The two 'I' options would add the 'lib' and 'extlib' folders to the list of places where Perl goes and looks for modules to include, the -M option would include the 'MT.pm' module and the call to MT->new would initialize MT by loading settings, connecting to the database etc. before the command (or commands) would actually be executed.

Pretty long and tedious to type every time you wanted to execute a quick, one-line test command, for example to display the title of entry ID 43

perl -Ilib -Iextlib -MMT -e 'MT->new;print MT::Entry->load(43)->title'

Akira's brilliant trick is to have a little file called T.pm in the MT folder, containing following lines:

package T;
use strict;
use warnings;
use lib qw( lib extlib );
use MT;
BEGIN { MT->new };
1;

This is basically a small Perl program that adds the 'lib' and 'extlib' folders to the list of places Perl looks for including modules, then includes MT.pm and initializes it. In other words, the same things accomplished witht -I and -M and MT->new in the first one-liner.

Having this T.pm file present now allows you to replace the example one-liner with just this:

perl -MT -e 'print MT::Entry->load(43)->title'

Which is quite a bit shorter and way easier to remember.  And damn funny too, making it look like perl has its own special '-MT' option.

No TrackBacks

TrackBack URL: http://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/1794

1 Comment

And they hate fundraisinghtmlhtmlhtml Marshals on every flighthtmlS,女新款 女新款秋装睡衣 国旗情侣家居服套装 秋装睡衣 国旗情侣家居服套装Republicans and Democrats alike seemed to find good things in the address Follow the money in CongressԴ]

Why Political Conventions Still Matter The Romney and Obama Fantasy Tax Plans Mitt

Leave a comment


Movable Type Help?

"Can my Movable Type issue or project be taken care of by a professional?" Yes It Can Be! Contact YesItCan.be
Powered by Movable Type 5.2.7