Nice Trick to do Movable Type One-Liners in Perl

| No Comments | 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

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

Recent Comments

  • Laura: Do you see MT really picking itself up any time read more
  • hiranyaloka.myopenid.com: Here's another way to split a string with the RegexList read more
  • Michel Oudendijk: Found it! I inadvertently placed the commentchallenge tag within the read more
  • Michel Oudendijk: Hi Maarten, I too installed this on one of my read more
  • hiranyaloka.myopenid.com: Thanks for the tip, Maarten. For more on how to read more
  • Nelly: Where can I visit something that could teach me somehow read more
  • Mihai Bocsaru: Michael, you should know that Maarten's plugin could work for read more
  • Mainer: Have you heard anything else about this? There does not read more
  • Andrew Bowden: How on earth did you know that this was something read more
  • Jonh: An other web based news aggregator: http://www.latestnewsheadline.com read more
Powered by Movable Type 5.13-en