Skip to content

Commit

Permalink
Added first example for libcurl NativeCall from farabi6 repo
Browse files Browse the repository at this point in the history
  • Loading branch information
azawawi committed Nov 17, 2012
1 parent 2a3ede3 commit 48bae51
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test_curl.p6
@@ -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);
}

0 comments on commit 48bae51

Please sign in to comment.