http://www.perlmonks.org?node_id=1000973


in reply to Best way to implement Inline::C/Pure Perl function in a module?

I think I would do it thusly:
use strict; use warnings; eval { require Inline; Inline->import (C => Config => BUILD_NOISY => 1); require Inline; Inline->import (C =><<' EOC'); int foo() { warn("Using Inline\n"); return 42; } EOC }; # If Inline is unavailable, foo() simply calls # the sub bar() pure perl implementation. if($@) { *foo =\&bar; } sub bar { warn("Using Pure Perl Implementation\n"); return 42; } my $x = foo(); print "$x\n";
If Inline::C is not available, that uses the pure-perl implementation of foo() and outputs:
Using Pure Perl Implementation 42
Otherwise, it uses the the Inline::C implementation of foo() and outputs:
Using Inline 42
NOTE: Running that script with Inline support will also output the C compilation report - but only for the *first* running, of course.
Note also that those who have Inline::C can still access the pure-perl implementation of foo() if they wish, by directly calling bar().

Cheers,
Rob