# 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`. ## Ενταμιευτές εξόδου (`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`. - **Λανθασμένοι κωδικοί τύπων** - η κλήση μιας συνάρτησης με υπογραφή που δεν ταιριάζει με το πραγματικό της πρωτότυπο είναι απροσδιόριστη συμπεριφορά. Ελέγξτε την κεφαλίδα 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`.