In the course of working with a project today, I came across some useful information. First, the use of command-line parameters in Perl. All you really have to do is add a space between the program name and the first, and then the first and the second, and so on.
For instance:
perl example.pl first second
And then in your program, make use of the @ARGV array, which contains all of the arguments. Like other arrays, the index starts at 0 – so to get the first argument, you would use $ARGV[0]. Easy!
Next, I had need to make use of the Movable Type mode, but since I was using a command-line parameter, I wasn’t sure how to populate it.
Like most programmers, I love to use existing examples, because they generally work – while my attempt may not. So I went looking. And found nothing. Luckily, David knew where to find what I needed, and we made it happen. All you need to do is populate the __mode parameter, like so:
if ($ARGV[0] eq 'example') { $app->{query}->param('__mode', 'example'); }
The final project was to do something about some really long URLs. Naturally there are services like TinyURL that exist for this very purpose. But it’s one thing when you want to go to a site, enter the value, then get your value back out.
I wanted it inside the program. WWW::Shorten to the rescue. A quick note to my host to have the module installed and it was ready-to-go. This sweet little module allows simple access directly to a number of URL-shortening services, right in the Perl!
Just a reminder – we do this sort of thing regularly. If you’re in need of a Movable Type Consultant, let us know. We can provide installation or upgrade assistance, custom plugins, even brand new applications built on top of (or beside of) Movable Type.