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 '' 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 in the MT folder, containing following lines:

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

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 and initializes it. In other words, the same things accomplished witht -I and -M and MT->new in the first one-liner.

Having this 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:

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