Working withShared Libraries In Perl
What is a Library ?●   An interface that allow you to access existed    functionality without writing it over and over    ...
There are two types of libraries●   Static library – Compiled* inside your code like it    was written for that applicatio...
Why ?             (Perl vs Shared Libraries)●   Accessing user environment tools.    For example:    –     Qt/GTK    –    ...
(In Linux)    There are two ways to write one●   The wrong, long way that (almost) everyone use●   The short, readable, ea...
Lets create Google (again)           By Erez Wolf
The wrong way:/* ac.h */…extern unsigned char google(void);…/* ac.c */…unsigned char google(void) {  return 42;}…/* main.c...
The wrong way ...# make it support PIC$ gcc -c -Wall -Werror -fpic ac.c# Create the shared library$ gcc -shared -o libac.s...
The right and easy waylibrary apas;function google : Byte; cdecl;begin  google := 42;End;exports google;end.$ fpc apas.pas...
How to load Shared libraries in Perl●   DynaLoader – The old way●   XSLoader – The new way●   FFI::Raw – External way (ver...
FFI::Raw●   It is very simple●   No need for .XS file (shard library for Perl ABI)●   No requirement for Makefile.PL●   Bu...
FFI::Raw – The code#!/usr/bin/env perl ­wuse strict;use v5.16; # Oh yea baby !use FFI::Raw;my $google = FFI::Raw­>new(  li...
Questions ?●   https://en.wikipedia.org/wiki/Library_%28computing%2
Upcoming SlideShare
Loading in …5
×

Working with Shared Libraries in Perl

3,446 views

Published on

Published in: Design
  • Be the first to comment

  • Be the first to like this

Working with Shared Libraries in Perl

  1. 1. Working withShared Libraries In Perl
  2. 2. What is a Library ?● An interface that allow you to access existed functionality without writing it over and over again● Considered non volatile tool● Usually Written by compiled languages such as C, C++ and Pascal● Not executable on its own
  3. 3. There are two types of libraries● Static library – Compiled* inside your code like it was written for that application● Shared Library – Compiled once, resident in memory, (usually**) providing a map table for all its shared functions* Your cpan modules considered static library.** Depends on the file format and OS
  4. 4. Why ? (Perl vs Shared Libraries)● Accessing user environment tools. For example: – Qt/GTK – Xlib/XCB – Libc, libstdc++ – Databases (Firebird, PostgreSQL) – etc...● Using lower level tasks with a system (inotify ...)● ...
  5. 5. (In Linux) There are two ways to write one● The wrong, long way that (almost) everyone use● The short, readable, easier to maintain, less buggy way, that (almost) no one use.
  6. 6. Lets create Google (again) By Erez Wolf
  7. 7. The wrong way:/* ac.h */…extern unsigned char google(void);…/* ac.c */…unsigned char google(void) {  return 42;}…/* main.c */#include <stdio.h>#include <ac.h>int main(void) {  printf(“Google: %dn”,          google());   …}
  8. 8. The wrong way ...# make it support PIC$ gcc -c -Wall -Werror -fpic ac.c# Create the shared library$ gcc -shared -o libac.so ac.c# Register the library with ld.so.conf and refresh path and cache$ sudo ldconfig# Linking$ gcc -Wall -o test main.c -lac
  9. 9. The right and easy waylibrary apas;function google : Byte; cdecl;begin  google := 42;End;exports google;end.$ fpc apas.pas$ ls *apas*# libapas.so# Register the library with ld.so.conf and refresh path and cache$ sudo ldconfig # use the lib 
  10. 10. How to load Shared libraries in Perl● DynaLoader – The old way● XSLoader – The new way● FFI::Raw – External way (very simplified)● Ctypes – Still in development, unstable support● Inline – Writing C/Java etc inside Perl
  11. 11. FFI::Raw● It is very simple● No need for .XS file (shard library for Perl ABI)● No requirement for Makefile.PL● But, lack support for few features such as struct :(
  12. 12. FFI::Raw – The code#!/usr/bin/env perl ­wuse strict;use v5.16; # Oh yea baby !use FFI::Raw;my $google = FFI::Raw­>new(  libapas.so, google,  FFI::Raw::uint, # Return type (always first));say $google­>call();
  13. 13. Questions ?● https://en.wikipedia.org/wiki/Library_%28computing%2

×