להגיע רחוק יותר: 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.פונקציות שממלאות חוצץ שסופק על ידי הקורא (קריאת פיקסלים אל זיכרון שבבעלותכם) משתמשות ב־
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 שולח, ספריית ווקטור אמיתית שה־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.