Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first example for libcurl NativeCall from farabi6 repo
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env perl6 | ||
use v6; | ||
|
||
use NativeCall; | ||
|
||
constant LIB = 'libcurl.so'; | ||
|
||
sub curl_easy_init() returns OpaquePointer is native(LIB) { ... }; | ||
sub curl_easy_cleanup(OpaquePointer) is native(LIB) { ... }; | ||
sub curl_easy_setopt(OpaquePointer, int, Str) is native(LIB) { ... }; | ||
sub curl_easy_perform(OpaquePointer) returns int is native(LIB) { ... }; | ||
sub curl_easy_strerror(int) returns Str is native(LIB) { ... }; | ||
|
||
constant CURLOPT_URL = 10002; | ||
constant CURLE_OK = 0; | ||
|
||
my $curl; | ||
my $res; | ||
|
||
$curl = curl_easy_init; | ||
if $curl { | ||
curl_easy_setopt($curl, CURLOPT_URL, "http://example.com"); | ||
|
||
# Perform the request, res will get the return code | ||
$res = curl_easy_perform($curl); | ||
|
||
# Check for errors | ||
if $res != CURLE_OK { | ||
say "Error: curl_easy_perform() failed: {curl_easy_strerror($res)}\n"; | ||
} else { | ||
say "Got a response!"; | ||
} | ||
say "libcurl initialized!"; | ||
|
||
# always cleanup | ||
curl_easy_cleanup($curl); | ||
} |