GObject Introspection for Perl

The GObject Introspection framework provides the means to dynamically create language bindings for a library from an interface description. This page describes the Perl bindings for the GObject introspection library.

Setup

In order to get and up and running you need to build three modules from Git. For this to succeed, gobject and libffi need to be installed.

gobject-introspection

gobject-introspection provides the API to read the interface descriptions, as well as various tools to create them.

Get it from Git:

  git clone git://git.gnome.org/gobject-introspection

And build it:

cd gobject-introspection
./autogen.sh --prefix=<prefix>
make install

gir-repository

gir-repository provides a set of interface descriptions for various libraries. Many libraries (like pango or libgda) started installing typelibs on their own, so this module will become less and less relevant. But if your library doesn't have native support for gobject-introspection yet, take a look inside gir-repository.

Glib::Object::Introspection

Finally, Glib::Object::Introspection is the binding that makes all the magic available to Perl land. It requires the Perl modules ExtUtils::Depends, ExtUtils::PkgConfig, and Glib.

Get it from Git:

git clone git://git.gnome.org/perl-Glib-Object-Introspection

And build it:

cd perl-Glib-Object-Introspection
perl Makefile.PL INSTALL_BASE=<prefix>
make test
make install

If your ExtUtils::MakeMaker is too old to understand INSTALL_BASE, use PREFIX.

Usage

Now that you have all those things installed, how do you use them to wrap libraries? For a quick example, take a look at the perl-Glib-IO module. First, you need to create the interface description for the library.

Create or find .gir and .typelib

GObjectIntrospection uses XML files ending in .gir to describe library interfaces. GObjectIntrospection offers a tool called g-ir-scanner which reads C headers and gtk-doc annotations to create an initial stab at a .gir file. The program g-ir-compiler then creates the binary .typelib representation that can be loaded by gobject-introspection. Quite a few library interface descriptions are included in gir-repository already, or they are provided by the libraries themselves. If your library is not among the ones in gir-repository or do not build introspection data, you'll need to figure out how to use g-ir-scanner and g-ir-compiler.

Write wrapper module

Once you have a .typelib description of your library, it's easy to create Perl bindings:

package Foo;

use Glib::Object::Introspection;

sub import {
  Glib::Object::Introspection->setup(basename => $BASENAME,
                                     version => $VERSION,
                                     package => $PACKAGE);
}

This will setup the interface described by $BASENAME-$VERSION.typelib using $PACKAGE as the top-level Perl package. For a well-behaving library, this should be all that is needed. Example values: $BASENAME = 'Gio'; $VERSION = '2.0'; $PACKAGE = 'Glib::IO'; .

Examples

Various Perl modules for GNOME libraries have been moved to the Introspection framework:

  • Glib::IO - the GLib interfaces, objects, and I/O library

  • Gtk3 - the version 3.x of the GTK+ toolkit

Projects/GTK-Perl/Introspection (last edited 2016-11-26 14:31:36 by EmmanueleBassi)