Φτάνοντας πιο μακριά: το Peta::FFI για γραφικά#
Τα κεφάλαια των εργαλειοθηκών κάλυψαν αυτό που το CPAN ήδη τυλίγει. Αυτό το κεφάλαιο, και ο υπόλοιπος οδηγός, πηγαίνει πιο μακριά: προσεγγίζει μια βιβλιοθήκη γραφικών σε C που δεν έχει καθόλου σύνδεση με το CPAN, χρησιμοποιώντας το εγγενές FFI της pperl. Αυτή είναι μια δυνατότητα αποκλειστική της pperl. Η σύνδεση μιας βιβλιοθήκης C με τον παραδοσιακό τρόπο σημαίνει τη συγγραφή ενός shim σε C, τη μεταγλώττισή του και την αποστολή μιας εργαλειοαλυσίδας κατασκευής μαζί με τον κώδικά σας· η pperl καλεί τη βιβλιοθήκη C απευθείας, τον ίδιο μηχανισμό που σας δίνει ήδη το Math::GMP χωρίς καμία μεταγλωττισμένη σύνδεση να κατασκευαστεί.
Peta::FFI is built into the runtime, and so are the native bindings built on its binding layer: they are Rust modules, not Perl-side declarations. (pperl’s built-in FFI::Platypus is a layer over Peta::FFI’s engine, for code written for CPAN’s FFI::Platypus.) This matters for graphics because a real graphics binding needs to manage C-allocated objects (a surface, a context) across many calls, and pperl’s model handles that the way the C library itself does.
Η έγκυρη αναφορά για το στρώμα FFI είναι η σελίδα Auto-FFI· αυτό το κεφάλαιο την εφαρμόζει ειδικά στα γραφικά.
Τα δύο επίπεδα#
Peta::FFI is best understood as two levels: raw calls you write yourself, and native bindings someone has written for you.
Ακατέργαστες κλήσεις. Ονομάζετε μια βιβλιοθήκη, μια συνάρτηση και μια συμβολοσειρά υπογραφής τύπων, και η pperl δρομολογεί την κλήση μέσω του libffi. Τίποτα δεν παράγεται εκ των προτέρων· οποιαδήποτε υπογραφή μπορείτε να γράψετε λειτουργεί.
use Peta::FFI qw(dlopen call dlclose);
my $lib = dlopen("libm.so.6");
my $r = call($lib, "sqrt", "(d)d", 2.0); # sqrt(2) = 1.4142...
dlclose($lib);
Η υπογραφή "(args)ret" χρησιμοποιεί ένα γράμμα ανά τύπο: v void, i int, l long, L unsigned long ή size_t, d double, f float, p μια συμβολοσειρά C ως είσοδο, P έναν μεταβλητό ενταμιευτή εξόδου, και o έναν αδιαφανή δείκτη που μεταφέρεται ως απλός ακέραιος. Γύρω από αυτούς τους κωδικούς βρίσκονται δύο ακόμη σύνολα πρωτογενών, και αυτά είναι που κάνουν προσβάσιμο το υπόλοιπο ενός API γραφικών, πέρα από τις συναρτήσεις που δέχονται απλά βαθμωτά: τα alloc, free, peek, peek_cstr, poke, pack_ptr και unpack_ptr για μνήμη που σας ανήκει, και τα closure και closure_free για να παραδώσετε ένα coderef της Perl στη C ως δείκτη συνάρτησης.
Native bindings. The other level is a hand-written native binding, built on Peta::FFI’s binding layer: a module that opens the library once, resolves every symbol it needs, and presents a clean Perl API, managing the C-allocated objects behind Perl values. It is the faster and more comfortable form of the same reach. Math::GMP is exactly this: use Math::GMP and you get arbitrary-precision integers with operator overloading, and nowhere did you touch dlopen or a signature string. The next chapter takes one of these that pperl already ships for a graphics library, Cairo, and shows how it works.
Τι μπορούν και τι δεν μπορούν να προσεγγίσουν οι ακατέργαστες κλήσεις#
Το μεγαλύτερο μέρος ενός API γραφικών σε C είναι προσεγγίσιμο αυτή τη στιγμή, γιατί το μεγαλύτερο μέρος του είναι βαθμωτά, χειριστές και μνήμη που ανήκει στον καλούντα:
Οι συναρτήσεις που παίρνουν και επιστρέφουν αριθμούς (ορίζουν ένα πλάτος γραμμής, μια ακτίνα, μια συνιστώσα χρώματος) αντιστοιχίζονται απευθείας στα
i,d,fκαι τα συναφή.Οι συναρτήσεις που παίρνουν μια συμβολοσειρά C (φορτώνουν μια γραμματοσειρά με το όνομά της, ανοίγουν ένα αρχείο) χρησιμοποιούν το
p.Οι συναρτήσεις που γεμίζουν έναν ενταμιευτή που παρέχει ο καλών (διαβάζουν pixel σε μνήμη που σας ανήκει) χρησιμοποιούν το
P, ή τοallocσυν τοoόταν θέλετε να κρατήσετε τον ενταμιευτή σε αρκετές κλήσεις.An opaque handle returned by the library (a surface pointer, a context pointer) is an address, and
ois its type code. Declared withhandle_type, it becomes an object of a class of your own that frees itself, as a<Class>type in the signature. The native pattern in the next chapter stores these object handles the same way: as an integer inside a blessed Perl reference.A struct, passed or returned by value (a
Color { r, g, b, a }, aRectangle) or through a pointer (a rectangle out-parameter), is reachable.recorddeclares it member by member, Peta::FFI lays it out as the C compiler does, and the signature names it as{Color}by value or*{Color}by pointer.A union (an event record) is reachable too, by pointer:
uniondeclares it asrecorddeclares a struct, every member at offset 0, and the signature passes it as*{Event}for the library to fill in. A union cannot be passed by value.Arrays and out-parameters take a Perl reference:
\@pointsfor aT[],\my $countfor a*T,\$buffor aPbuffer C fills.Ένα callback είναι προσεγγίσιμο. Το
closureτυλίγει ένα coderef ως γνήσιο δείκτη συνάρτησης C, οπότε μια βιβλιοθήκη που καλεί μέσα στον κώδικά σας (ένας συγκριτής, ένας χειριστής προόδου, ένα callback συμβάντος ή σχεδίασης) παίρνει έναν.
Where raw calls stop: anything derived from a header. Nothing reads the library’s .h files for you. You write the signature from the C prototype yourself and declare each record’s and union’s members yourself. Getting those right is your responsibility. A signature that disagrees with the real prototype is undefined behaviour rather than an error you get told about.
The practical shape of that: the mechanism for a graphics library is in place, and what a hand-written binding costs you now is the careful work of transcribing prototypes and declarations correctly. This guide’s value is telling you which side of the line a given task falls on, so you neither avoid what works nor promise what does not.
Why a native binding, not raw calls, for real work#
You could drive a graphics library entirely through raw calls, and for a quick experiment that is fine. For anything real, a native binding is the right unit, for three reasons.
First, object lifetime. A graphics context is allocated by the library and must be freed exactly once. A native binding ties that lifetime to a Perl value’s DESTROY, so the C object is released when the Perl object goes away, with no manual bookkeeping. With raw calls you declare that yourself, with handle_type for each C object type, or keep track of every o pointer by hand.
Δεύτερον, η εργονομία. Το $surface->fill διαβάζεται καλύτερα και είναι δυσκολότερο να το κάνει κανείς λάθος από ένα call με χειρόγραφη υπογραφή σε κάθε σημείο χρήσης.
Third, the signature is written once. In a native binding each C function’s exact type is declared once, in one Rust module, and the compiler checks every argument against it; every caller just uses the method. Scattering raw signatures through application code is how you get a mismatch that corrupts the stack.
Η επίδειξη του Cairo κάνει και τα τρία απτά με τη σύνδεση Cairo που αποστέλλει η pperl, μια πραγματική βιβλιοθήκη vector της οποίας το API είναι σχεδόν εξ ολοκλήρου βαθμωτά και ένας αδιαφανής χειριστής, που είναι ακριβώς το σχήμα που προσεγγίζεται καθαρά σήμερα.
Το σχήμα αυτών που ακολουθούν#
The next chapter opens up the native Cairo binding pperl ships, built the way Math::GMP is, so you can see a graphics library brought up on pperl end to end. Then the two capstones build a drawing application and a 2D game you can run, both on the native SDL2 binding, with the raw layer shown where it clarifies what the binding removes. The final chapter draws the map of what is left.