Introduction
|
|
Perl-Fu is not a part of the core Gimp 1.0.x distribution. However, the next version of Gimp (1.2) will include Perl-Fu as a standard part of Gimp. There's no need to grumble over the fact that you run Gimp 1.0.x. Perl-Fu is available as a plug-in to Gimp 1.0.x, and Perl-Fu is also included in the development version of Gimp. Perl-Fu is a plug-in that makes it possible to write Gimp scripts in Perl, just like the Script-Fu plug-in makes it possible to write Gimp scripts based on Scheme. Yes, both Perl-Fu and Script-Fu are plug-ins. In our opinion, Perl is considerably more straightforward and easy to use than Scheme. Furthermore, there are a lot more publicly available Perl scripts (and experienced Perl programmers), than there are scripts based on Scheme. Note that we are not referring to specific Gimp scripts, but to scripts in general. Perl is also the major scripting language used by CGI scripts. If you are somewhat familiar with UNIX shell scripts, then Perl will be very easy to learn. In fact, you will probably find Perl scripts more uncomplicated to write than shell scripts. Just about every awk, sed and other tweaks that you usually execute in a shell script can be done natively in Perl without the help of other UNIX shell programs. In "A Tutorial For Perl Gimp Users" starting on page 735, you will find Dov Grobgelb's Perl-Fu tutorial. It's a very good tutorial, and if you are familiar with Perl, you will probably be able to use Perl-Fu right away. However, the tutorial doesn't cover "learning Perl." That is a vast topic, and it's simply beyond the scope of this book; but we will give you a crash course in Perl. How Experienced Do You Have To Be?For total script newbies, the crash course will make it easier to read and understand Dov's tutorial. After reading the crash course, you can make the decision of whether Perl-Fu is something that you want to use or not. If you want to learn more about Perl-Fu, then the best advice we can give you is to go and buy a Perl book and learn the basics of Perl programming. If you are familiar with shell scripting, for example, then you can probably (with some help from Perl's on-line documentation) start to use Perl-Fu after reading the crash course and Dov's tutorial. The Perl-Fu man pages covers every command in the Perl-Fu plug-in, and you'll find them in the "Perl-Fu Man Pages" appendix starting on page 835. Those pages are an excellent source of information when you write Perl-Fu scripts. Just to make it clear: You will need to understand Perl if you want to use Perl-Fu, but Perl is a very extensive language, and you don't need to learn all of it. It's often sufficient to learn elementary Perl-Fu for basic Gimp scripting. When you are a more experienced Perl programmer, you can learn more about advanced Perl scripting. In this chapter, we will discuss how you install and configure the Perl-Fu plug-in. This is generally a simple procedure, but if you don't have Perl installed, then it can be a bit more troublesome. | |
Perl-Fu Installation And Configuration
|
|
If you have read the previous chapters about installing miscellaneous accessories to Gimp (see "Drawing Tablets And Gimp" starting on page 657 and "Scanning And Scanners" starting on page 219), then you'll probably expect to find 10 or more pages about how to install Perl-Fu. Well, we will not do it this time. If you have read this far, you should have gathered enough experience by now and only need some hints. There are also a lot hints about compiling in "Compiling Plug-ins" starting on page 769. HintsIt's always a good idea to get the latest source code of the Perl-Fu plug-in and the Gtk Perl extension that Perl-Fu needs. Check out Perl-Fu to find out where to download Perl-Fu and the Perl Gtk module. First of all, make sure that your Gimp and Gtk libraries are in your library search path. This is controlled in Linux by the Second, you must have your Gimp and Gtk binary directory in your path (controlled by the If you have multiple installations of Gimp and/or Gtk, it is very important to set both Suppose that you have Gimp 1.0.x and Gtk 1.0.x installed under Then you have to set the
and your
This procedure will ensure that you compile and install Perl-Fu and the Perl Gtk module for usage in Gimp 1.1.x. If you don't set your environment correctly, it is more or less certain that you will fail to compile and install Perl-Fu. Naturally, you will also need to have Perl installed on your system. Either Perl 5.004_04 or Perl 5.005 or higher. Our last tip is that you must read the README and INSTALL files that come with Perl-Fu and the Perl Gtk module. If you think you can manage without them, then you're either suffering from a severe case of hubris, or (which is more unlikely) you really are an expert on UNIX, Gimp and Perl with associated configuration, compilation, installation, etc. | |
Crash Course
|
|
We can't cover all basic Perl functions here, but we will try to explain the fundamental functions that you'll need to understand before taking on Dov's Perl tutorial. This section is for absolute beginners, so don't expect to understand other Perl scripts after you have read this crash course and Dov's tutorial. Defining A Perl ScriptNormally, when you execute a program the program file is a binary file built up of ones and zeros. However, a Perl program file is different, because Perl is an interpreting language. Perl uses ordinary text files that humans can read and not binary 0 and 1 files. It's therefore necessary to tell the operating system that it should use the Perl interpreter (i.e., execute the file with the help of Perl) when the operating system launches the file. This is done by "chmod:ing" the file: chmod 755 perl_fu_script.pl and with a special line in the top of the Perl file: #!/usr/local/bin/perl This special line tells the OS that it should use Perl to execute the file. (Naturally the location of Perl is site specific, but in this case it was You have probably heard of shell scripts. Shell scripts make use of readable files, just like Perl does. A shell script's first line is: #!/bin/sh (or As you see, there are several types of scripting languages, and the first line in the text files tells us which type of interpreter to use. The rest of the file contains the script code. The Perl Functionality ModulesA core Perl installation has a lot of functions available for the programmer, and it is also possible to install extra Perl modules to add more functions to Perl if needed. Loading all Perl functions all the time will make a Perl program slow (that definition is not totally correct, but it's good enough for now). A better way is to load functionality into Perl only when you need it. For example, say you are writing a network application that is supposed to copy a remote file (a file present on another computer) to your computer. Then you may need the use Net:remotecopy; This line will tell Perl to load the "remotecopy" functionality. After you have loaded use Gimp; use Gimp::Fu; This tells Perl to load the Perl VariablesVariables are things that can store things. For example, the variable $variable = 10; Notice the ending ";" which tells Perl that it has reached the end of the command (this is mandatory for all Perl commands). We could also write: $variable = 10 ; But that is not as elegant, is it? Perl FunctionsA function in Perl is a routine that does things for you. Most of the time, functions are used to help the programmer in different ways. Instead of writing the same thing in the code a number of times, the programmer can do this once in a single function. He will then call on that function when he needs it. This means that you don't need to type all those lines of code, but also that you get a code that is simpler to understand. In fact, when you import a functionality module (such as the Gimp module), then you import a lot of functions that you can use in your code, such as the gimp_displays_flush(); and the display will be flushed. This is obviously much easier than writing the "flush display" code every time you need it (you would also have to know how to implement the function; now, you just have to know how to use it). To create your own functions, also called subroutines, you have to declare the function by the special name: sub karins_flush_display { gimp_displays_flush(); } This is naturally a quite meaningless function. Nevertheless, you can now use |
|
A Perl-Fu script has one or several self-defined subroutines that you use to manipulate or create Gimp images. Variables And FunctionsIn Perl-Fu scripts you will frequently see: sub some_function { my($variable1, $variable2) = @_; #The subroutine code #Hash marks are comment marks in a Perl script } The keyword The @_ is a special variable carrying the arguments that the subroutine was called with (well, that's not the whole truth but it's good enough for now; and we don't want to get too complicated here). Let's say that you have this code: sub some_function { my($variable1, $variable2) = @_; print $variable1 + $variable2; #(print will simply print the sum \ } some_function(1,2); Then, The line Perl-Fu SpecialsWhat's special about Perl-Fu scripts is the LoopsDo you want to repeat the same thing over and over again? Then, you should use Perl's loop functionality. There are several ways of doing this in Perl, but we will only explain the for loop because it is used in Dov's tutorial. Dov uses a for loop as in this tutorial: $number_of_loops = 10; for ($i=0; $i<$number_of_loops; $i++) { #(The code) } The $number_of_loops = 10; for ($i=0; $i<$number_of_loops; ++$i) { print $i; } This is a simple example loop that will print Some Final NotesWe haven't really learned much about Perl -- we have barely showed some of the Perl functions that you'll need to understand Dov's tutorial. If you find Perl-Fu interesting, we suggest that you buy a "Learn Perl" book. Such a book along with the example Perl-Fu scripts that you'll find in the Perl-Fu distribution, and of course the Perl-Fu manpages (that you will find in the "Perl-Fu Man Pages" appendix starting on page 835) will be a very good start to explore Perl-Fu. Also, don't forget to read Dov's excellent Perl-Fu tutorial --consider it mandatory! |
Frozenriver Digital Design http://www.frozenriver.nu Voice: +46 (0)31 474356 Fax: +46 (0)31 493833 support@frozenriver.com |
Publisher Coriolis http://www.coriolis.com |