# call הפעלת פונקציית C מספרייה פתוחה והחזרת התוצאה שלה. ## תקציר ```perl my $n = Peta::FFI::call($lib, "strlen", "(p)L", "hello"); # 5 my $r = Peta::FFI::call($lib, "sqrt", "(d)d", 2.0); # 1.4142... my $s = Peta::FFI::call($lib, "getenv", "(p)p", "PATH"); # /usr/bin:... Peta::FFI::call($lib, "srand", "(i)v", 42); # void return ``` ## ארגומנטים - `$lib` - ידית שהוחזרה על ידי `dlopen`. - `$func` - symbol name to look up in the library, or the address of a function: a pointer C handed out, such as an entry of a table of callbacks, which has no name to look up. - `$sig` - מחרוזת חתימה `(args)ret`; ראו את טבלת קודי הטיפוס ברמת המודול. - שאר הארגומנטים - ערך אחד לכל קוד טיפוס ב־`args`, המומר בכפייה לטיפוס ה־C התואם לפני הקריאה. ## מה מוחזר A single scalar whose shape depends on the return type: - the integer types, `b` - an integer; perl’s true or false. - `d` / `f` / `D` - a float (a long double rounded to one). - `cf` / `cd` - `[re, im]`. - `p` - ה־`const char *` המוחזר, מועתק למחרוזת Perl; `undef` אם פונקציית ה־C החזירה `NULL`. - `o` - the address; `0` for `NULL`. - `v` - `1`, כך שהקריאה משתרשרת באופן טבעי בהקשרים בוליאניים. - `P` - מצביע המוחזר כמחרוזת (אותם כללים כמו `p`). - `*T` - a reference to the value it points to; `T[N]` - a reference to an array of the N values; `undef` for `NULL`. - `{Class}` - a new record object; `*{Class}` - a copy of the record it points to, `undef` for `NULL`. ## buffers פלט (`P`) A `P` argument that is a reference to a scalar passes that scalar’s own bytes, and after the call the scalar holds what the callee wrote there - as many bytes as it had: `my $buf = "\0" x 64; call(..., \$buf)`. Any other `P` argument is a zeroed buffer the callee may write and nobody reads, as long as the string passed (minimum 16 bytes) or as many bytes as the number says. ## Pointers and arrays A `*T` argument is a reference to a scalar, which the callee reads and writes - an out-parameter, `\my $n` - or `undef` for `NULL`; a reference to an array passes its elements. A `T[N]` argument is a reference to an array of N values, and `T[]` one of any length; after the call the array holds what the callee left there. ## מקרי קצה - **מספר ארגומנטים שגוי** - עושה croak עם `Peta::FFI::call FNAME: expected N args, got M`. - **מחרוזת חתימה פגומה** - עושה croak עם `FFI signature error: ...` המתאר את התו הבעייתי או את ה־`)` החסר. - **סמל לא נמצא** - עושה croak עם `Peta::FFI::call: symbol not found: FNAME` או עם ההודעה שמדווחת `dlerror`. - **קודי טיפוס שגויים** - קריאה לפונקציה עם חתימה שאינה תואמת את אב־הטיפוס האמיתי שלה היא התנהגות לא־מוגדרת. יש לבדוק את קובץ ה־header של C לפני כתיבת החתימה. - **String argument contains a null byte** - croaks with `Peta::FFI::call FNAME: CString error: ...`. - **A handle of the wrong class, or a released one** - croaks before the call. `call` parses the signature, looks the symbol up and prepares the call interface every time; a function called more than a few times is better prepared once with `function` or `attach`.