Skip to content

Commit ffd9d49

Browse files
committed
Merge branch 'master' of github.com:perl6/doc
2 parents cbd9403 + e727bfd commit ffd9d49

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/Language/nativecall.pod

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,33 @@ to callbacks.
297297
298298
TBD
299299
300+
=head2 Calling into the standard library
301+
302+
If you want to call a C function that's already loaded, either from the
303+
standard library or from your own program, you can pass the C<Str> type object
304+
as the argument to C<is native>, so C<is native(Str)>.
305+
306+
For example on a UNIX-like operating system, you could use the following code
307+
to print the home directory of the current user:
308+
309+
use NativeCall;
310+
my class PwStruct is repr('CStruct') {
311+
has Str $.pw_name;
312+
has Str $.pw_passwd;
313+
has uint32 $.pw_uid;
314+
has uint32 $.pw_gid;
315+
has Str $.pw_gecos;
316+
has Str $.pw_dir;
317+
has Str $.pw_shell;
318+
}
319+
sub getuid() returns uint32 is native(Str) {};
320+
sub getpwuid(int $uid) returns PwStruct is native(Str) {};
321+
322+
say getpwuid(getuid()).pw_dir;
323+
324+
Though of course C<$*HOME> is a much easier way :-)
325+
326+
300327
=head1 Exported variables
301328
302329
Variables exported by a library -- also named "global" or "extern"

0 commit comments

Comments
 (0)