| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use FFI::Platypus 2.00; |
| 4 |
use FFI::CheckLib qw( find_lib_or_die ); |
| 5 |
use FFI::Platypus::Buffer qw( window ); |
| 6 |
use constant CURLOPT_URL => 10002; |
| 7 |
use constant CURLOPT_WRITEFUNCTION => 20011; |
| 8 |
|
| 9 |
my $ffi = FFI::Platypus->new( |
| 10 |
api => 2, |
| 11 |
lib => find_lib_or_die(lib => 'curl'), |
| 12 |
); |
| 13 |
|
| 14 |
my $curl_handle = $ffi->function( 'curl_easy_init' => [] => 'opaque' ) |
| 15 |
->call; |
| 16 |
|
| 17 |
$ffi->function( 'curl_easy_setopt' => [ 'opaque', 'enum' ] => ['string'] ) |
| 18 |
->call($curl_handle, CURLOPT_URL, "https://pl.atypus.org" ); |
| 19 |
|
| 20 |
my $html; |
| 21 |
|
| 22 |
my $closure = $ffi->closure(sub { |
| 23 |
my($ptr, $len, $num, $user) = @_; |
| 24 |
window(my $buf, $ptr, $len*$num); |
| 25 |
$html .= $buf; |
| 26 |
return $len*$num; |
| 27 |
}); |
| 28 |
|
| 29 |
$ffi->function( 'curl_easy_setopt' => [ 'opaque', 'enum' ] => ['(opaque,size_t,size_t,opaque)->size_t'] => 'enum' ) |
| 30 |
->call($curl_handle, CURLOPT_WRITEFUNCTION, $closure); |
| 31 |
|
| 32 |
$ffi->function( 'curl_easy_perform' => [ 'opaque' ] => 'enum' ) |
| 33 |
->call($curl_handle); |
| 34 |
|
| 35 |
$ffi->function( 'curl_easy_cleanup' => [ 'opaque' ] ) |
| 36 |
->call($curl_handle); |
| 37 |
|
| 38 |
if($html =~ /<title>(.*?)<\/title>/) { |
| 39 |
print "$1\n"; |
| 40 |
} |