Object-Oriented Versus Procedural Scripting

If you are familiar with the structure of Gimp plug-ins written in C, you will immediately notice a few details of the previous example that differ between Perl and C. Because Gimp-Perl is a scripting extension that acts as a high-level interface to libgimp (the Gimp API), you do not necessarily have to use the clumsy PDB-style calling syntax that you must use in C.

All libgimp functions and additional plug-ins are registered in the Procedural Database. The entries in the PDB serve to document the Gimp API; you can use the PDB Browser (found under the Xtns menu) to search the API. As shown in Figure 5-2, the browser displays the function interfaces using C syntax. For example, if you want to copy a layer using the function gimp_layer_copy( ), you would look up the function in the browser and write the function call (in C) as:

ret_vals = gimp_run_procedure("gimp_layer_copy",
                              &nret_vals,
                              PARAM_LAYER, layerID,
                              PARAM_INT32, TRUE,
                              PARAM_END);
The Procedural Database browser
Figure 5-2. The Procedural Database browser

In Perl you can call the function more simply:

$new_layer = gimp_layer_copy($layer, 1);

Note that the style shown above can be used only if you have imported the Gimp API function names with the :auto tag. Otherwise, you would have to specify the package name:

$new_layer = Gimp::gimp_layer_copy($layer, 1);

You can get away with sticking to the simple procedural-style calling ...

Get Perl Graphics Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.