Syntactic Confectionery Delight | |
PerlMonks |
How Scheme and Perl Became Friendsby beppu (Hermit) |
on Feb 20, 2002 at 10:00 UTC ( [id://146539]=CUFP: print w/replies, xml ) | 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. I believe this was done for security purposes. The GIMP comes with a Script-Fu Server which lets you send arbitrary Scheme code over a socket for a running GIMP process to execute. This Scheme code could be coming from anywhere on the Internet, so you'd be foolish to let it do whatever it wanted. To make an analogy with something familiar to all of us here, imagine what life would be like JavaScript programmers could access filesystems on the client side. 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. gimp-request
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).
Back to
Cool Uses for Perl
|
|