package Graph::igraph; use strict; use warnings; use 5.012; use Carp; use autodie; use utf8; use Alien::igraph; use FFI::Platypus; use FFI::Platypus::API; my $ffi = FFI::Platypus->new; $ffi->lib(Alien::igraph->dynamic_libs); $ffi->type('int' => 'igraph_integer_t'); $ffi->type('int' => 'igraph_bool_t'); $ffi->type('double' => 'igraph_real_t'); $ffi->type('opaque' => 'igraph_t'); $ffi->load_custom_type('::StringPointer' => 'string_pointer'); $ffi->attach([igraph_empty => 'new'] => [qw/igraph_t igraph_integer_t igraph_bool_t/] => 'int', sub { my ($xsub, $class, %options) = @_; my $graph; if ($options{undirected}) { say 'undirected -- ' . $xsub->(\$graph, 0, 0); } else { say 'directed -- ' . $xsub->(\$graph, 0, 1); } say 'weird: this line says "Use of uninitialized value in say"'; return bless \$graph, $class; }); $ffi->attach([igraph_destroy => 'DESTROY'] => [qw/igraph_t/] => 'int'); $ffi->attach(igraph_vcount => ['igraph_t'] => 'igraph_integer_t'); $ffi->attach(igraph_add_vertices => [qw/igraph_t igraph_integer_t/] => 'int'); $ffi->attach(igraph_version => [qw/string_pointer int* int* int*/] => 'int'); sub version { my ($version, $major, $minor, $patch); igraph_version(\$version, \$major, \$minor, \$patch); return ($version, $major, $minor, $patch); } sub add_vertex { my $self = shift; igraph_add_vertices($self, 1); } sub count_vertices { my $self = shift; igraph_vcount($self); } 1; #### say "version: " . join(' ', Graph::igraph::version()); # prints 0.7.1 0 7 1 #### say "version: " . join(' ', Graph::igraph::version()); my $graph = Graph::igraph->new(directed => 1); use Data::Dumper; print Dumper($graph); $graph->add_vertex('foobar'); say $graph->count_vertices; #### version: 0.7.1 0 7 1 directed -- 0 Use of uninitialized value in say at lib/Graph/igraph.pm line 34. Attempt to free unreferenced scalar: SV 0x21213d8 at /home/fgabolde/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/x86_64-linux/FFI/Platypus.pm line 331. $VAR1 = bless( do{\(my $o = undef)}, 'Graph::igraph' ); *** Error in `perl': realloc(): invalid next size: 0x000000000223f850 ***