| A Perl Introduction This chapter is a brief introduction to Perl for users who are not familiar with Perl, or aren't experienced programmers. We can't teach you how to program in Perl, but we will try to demonstrate some of Perl's utility and power.

## 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.

### Hints

It'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 `/etc/ld.so.conf` file. Solaris (and most other Unix dialects) uses the `LD_LIBRARY_PATH` environment variable to control the library search path.

Second, you must have your Gimp and Gtk binary directory in your path (controlled by the `PATH` environment variable). The configure programs that are used to prepare both Perl-Fu's and Gtk's Perl Module are dependent on finding `gimp-tool` and `gtk-config`.

If you have multiple installations of Gimp and/or Gtk, it is very important to set both `PATH` and `LD_LIBRARY_PATH` to search in the right directories first.

Suppose that you have Gimp 1.0.x and Gtk 1.0.x installed under `/opt/gimp` and Gimp 1.1.x and GA 1.2.x installed under `/opt/gimp1.1` and you want to enable Perl-Fu within Gimp 1.1.x.

Then you have to set the `PATH` to:

`PATH=/opt/gimp1.1/bin:$PATH`

and your `LD_LIBRARY_PATH` to:

`LD_LIBRARY_PATH=/opt/gimp1.1/lib:$LD_LIBRARY_PATH`.

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 Script

Normally, 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 `/usr/bin/perl`.)

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 `#!/bin/bash` most of the time on a Linux system).

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 `-w` switch that comes after `#!/usr/local/bin/perl` orders Perl to warn you if there are potential errors in your script. We strongly recommend always using the `-w` flag in your Perl scripts.

### Perl Functionality Modules

A 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 `Net:remotecopy` (this command is purely fictitious) functionality in Perl. This functionality is not present by default in Perl, so you must tell Perl to load the functionality. This is done by the `use` command in Perl:

use Net:remotecopy;

This line will tell Perl to load the "remotecopy" functionality. After you have loaded `Net:remotecopy`, you can use all the remote copy commands in a Perl program. It is similar to a Perl-Fu script (which is just a name of a Perl script using Gimp functionality)

use Gimp;

use Gimp::Fu;

This tells Perl to load the `Gimp` functionality and the `Gimp::Fu` functionality. So, why do you have to load two modules? Well, the Gimp module provides you with basic Gimp functionality in Perl. The Gimp::Fu module (that is dependent on the Gimp module) loads a set of more Gimp functions that make it easy to write Perl-Fu scripts.

### Perl Variables

Variables are things that can store things. For example, the variable `A` can store the value `10`. In Perl you define a variable like this:

$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 Functions

A 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` function that will flush Gimp's display. In order to flush Gimp's display, enter:

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` followed by the name of the function that you want to implement:

sub karins_flush_display {

gimp_displays_flush();

}

This is naturally a quite meaningless function. Nevertheless, you can now use `karins_flush_display();` instead of the `gimp_displays_flush();` function call.

A Perl-Fu script has one or several self-defined subroutines that you use to manipulate or create Gimp images.

#### Variables And Functions

In 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 `my` makes the variables (`$variable1` and `$variable2`) local variables within the subroutine. This means that you can't access them or the values they carry outside the subroutine.

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 \
of $variable1 and $variable2)

}

some_function(1,2);

Then, `some_function` is called with `1` and `2` as arguments. Perl reads the whole file and stores the subroutine. Then, Perl will execute the first command, which in this case is `some_function(1,2);`.

The line `my($variable1, $variable2) = @_;` lets us assign 1 to `$variable1`, and 2 to `$variable2`, because they are stored in the special `@_` variable in that order. The next line will simply print 3, because that is the sum of 1 and 2.

#### Perl-Fu Specials

What's special about Perl-Fu scripts is the `\&function_name` that is used in the registry part of Perl-Fu (don't worry, Dov will cover the registry part of Perl-Fu). Anyway, this is a very special way of including the subroutines to call in Perl scripts. You must use that kind of reference when you call your first and only subroutine in the registry part of Perl-Fu.

### Loops

Do 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 `$i=0;` `$i<$number_of_loops; $i++` tells us that the loop start value is 0 (`$i=0`) and that each time the loop is done we will add 1 to `$i` (`$i++`). Before starting a loop we will check if `$i` is less than 10 (`$i<$number_of_loops`). If it is, we will continue to loop, and if it isn't, we will exit the loop:

$number_of_loops = 10;

for ($i=0; $i<$number_of_loops; ++$i) {

print $i;

}

This is a simple example loop that will print `0123456789` when you run the code. This line shows that we have made 10 loops, and that we started on 0 and ended on 9.

### Some Final Notes

We 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