<?xml version="1.0" encoding="windows-1252"?>
<node id="1000973" title="Re: Best way to implement Inline::C/Pure Perl function in a module?" created="2012-10-25 20:06:34" updated="2012-10-25 20:06:34">
<type id="11">
note</type>
<author id="524150">
syphilis</author>
<data>
<field name="doctext">
I think I would do it thusly:
&lt;c&gt;
use strict;
use warnings;

eval {
 require Inline; Inline-&gt;import (C =&gt; Config =&gt;
    BUILD_NOISY =&gt; 1);

 require Inline; Inline-&gt;import (C =&gt;&lt;&lt;' 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 =\&amp;bar; 
}

sub bar {
  warn("Using Pure Perl Implementation\n");
  return 42;
}

my $x = foo();
print "$x\n";
&lt;/c&gt;
If Inline::C is not available, that uses the pure-perl implementation of foo() and outputs:
&lt;c&gt;
Using Pure Perl Implementation
42
&lt;/c&gt;
Otherwise, it uses the the Inline::C implementation of foo() and outputs:
&lt;c&gt;
Using Inline
42
&lt;/c&gt;
NOTE: Running that script with Inline support will also output the C compilation report - but only for the *first* running, of course.&lt;br&gt;
Note also that those who have Inline::C can still access the pure-perl implementation of foo() if they wish, by directly calling bar(). &lt;br&gt;&lt;br&gt;
Cheers,&lt;br&gt;Rob</field>
<field name="root_node">
1000947</field>
<field name="parent_node">
1000947</field>
</data>
</node>
