|Keep It Simple, Stupid|
How Scheme and Perl Became Friendsby beppu (Hermit)
|on Feb 20, 2002 at 10:00 UTC||Need Help??|
(...or "Perl as a Scheme Preprocessor")
When people hear that the GIMP can be scripted using Scheme, they think that it means you can write scripts to perform large batch operations. A common question that comes up on the gimp-user mailing list is, "I have a bunch of images in a directory - How can I script the GIMP to make a bunch of thumbnails out of them?" That might sound easy enough, but it's actually not.
The problem is that the Scheme interpreter within the GIMP has been castrated. All the functions that allow one to interact with the underlying operating system have been removed. That means no forking, no opendir/readdir/closedir, no nothing.
Basically, it's a nightmare waiting to happen, so the GIMP developers wisely stripped the Scheme interpreter of a lot of its power. Unfortunately, this made Script-Fu a lot less useful than it could have been. Ironically, hardly anyone uses the Script-Fu server, so it may seem like all the work performed to secure that GIMP was wasted.
However, it is through the Script-Fu Server that we make the GIMP usable for large batch operations, again. The perl script that follows lets you send Scheme code to a Script-Fu Server, but there's an added twist. It also lets you embed Perl code inside your Scheme, effectively turning Perl into a Scheme preprocessor.
Even if the Scheme interpreter is oblivious to the operating system that surrounds it, Perl knows what's up. Perl knows all about @ARGV and the filesystem and databases and the Internet. There is no shortage of data sources when Perl is involved. Now, you actually can write a Script-Fu program that can batch-create a bunch of thumbnails... and you can run that program from the command-line, even. You can do a lot more than that, too.
That's my Cool Use For Perl. bringing Scheme and Perl together, && bringing the GIMP and the command-line together... They're an odd couple, but they seem to get along OK.
PS: gimp-request made its first appearance in the Feb. 2002 issue of Linux Magazine. I've been wating since December to post this on perlmonks.org.
PPS: I know I could just use Gimp::Perl which doesn't suffer from these limitations, but not everyone has a Perl-enabled GIMP (whereas all GIMPs have a Scheme interpreter).