Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hybrid Module Question

Reply
Thread Tools

Hybrid Module Question

 
 
Mark Shelor
Guest
Posts: n/a
 
      05-30-2004
I'm the author of a CPAN module that contains XSUBs written in C. In
other words, users are required to have a C compiler to build the module.

In the future, I'd like to also include pure Perl versions of these
subroutines so that the module can still be used even if a C compiler
isn't present. I assumed there was a more-or-less standard way to go
about writing a hybrid module, but I've yet to find specific guidelines
in the Perl documentation I've looked at.

Basically, I want to set things up such that the C-language XSUBs are
automatically used if the build platform has a C compiler. Otherwise,
the pure Perl versions of the routines would be installed.

Could anyone point me to relevant examples or how-to documentation?

Thanks, Mark
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      05-30-2004
>>>>> "MS" == Mark Shelor <> writes:

MS> Basically, I want to set things up such that the C-language XSUBs are
MS> automatically used if the build platform has a C compiler. Otherwise,
MS> the pure Perl versions of the routines would be installed.

MS> Could anyone point me to relevant examples or how-to documentation?

i do this exact thing in stem. it is very simple. you make the C and
perl versions into sibling classes and have a wrapper class above
them. something like this:

Foo.pm
Foo/Perl.pm
Foo/C.pm

the user always creates a Foo but inside that it tries to load Foo/C.pm
and it uses that if it can. else it tries to load Foo/Perl.pm and that
had better succeed or you die.

here is my code that does this. it tries to load the event loop you
request which is normally the c module Event.pm and falls back to my
Perl event loop. on winblows it always uses the Perl one since Event.pm
is unix specific.

the can't locate a module is the only error allowed. then it
will do the forced load of Perl.pm

my $loop_type = $Stem::Vars::Env{ 'event_loop' } ||
($^O =~ /win32/i ? 'perl' : 'event' );

my $loop_class = $loop_to_class{ $loop_type } || 'Stem::Event:erl' ;

unless ( eval "require $loop_class" ) {
die "can't load $loop_class: $@" if $@ && $@ !~ /locate/ ;

eval 'require Stem::Event:erl' ;
die "can't load default event loop Stem::Event:erl $@" if $@ ;
}

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      05-30-2004
Mark Shelor () wrote:
: I'm the author of a CPAN module that contains XSUBs written in C. In
: other words, users are required to have a C compiler to build the module.

: In the future, I'd like to also include pure Perl versions of these
: subroutines so that the module can still be used even if a C compiler
: isn't present. I assumed there was a more-or-less standard way to go
: about writing a hybrid module, but I've yet to find specific guidelines
: in the Perl documentation I've looked at.

: Basically, I want to set things up such that the C-language XSUBs are
: automatically used if the build platform has a C compiler. Otherwise,
: the pure Perl versions of the routines would be installed.

: Could anyone point me to relevant examples or how-to documentation?

MIME::Base64 does this.
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      05-30-2004
Also sprach Malcolm Dew-Jones:

> Mark Shelor () wrote:
>: I'm the author of a CPAN module that contains XSUBs written in C. In
>: other words, users are required to have a C compiler to build the module.
>
>: In the future, I'd like to also include pure Perl versions of these
>: subroutines so that the module can still be used even if a C compiler
>: isn't present. I assumed there was a more-or-less standard way to go
>: about writing a hybrid module, but I've yet to find specific guidelines
>: in the Perl documentation I've looked at.
>
>: Basically, I want to set things up such that the C-language XSUBs are
>: automatically used if the build platform has a C compiler. Otherwise,
>: the pure Perl versions of the routines would be installed.
>
>: Could anyone point me to relevant examples or how-to documentation?
>
> MIME::Base64 does this.


The 2.x releases at least do it. It's quite a clutch, though. It works
by having the user delete Base64.xs before building it. Not something
that I would call very elegant.

I think it should be possible with three distinct Makefile.PLs. The
top-level one checks for the existance of a C compiler. If one is found,
it calls, for example, xs/Makefile.PL. Otherwise it will call
perl/Makefile.PL. The pure Perl version and the XS version would then
have to live in separate sub directories.

It has to be ensured that the correct Makefile is called in the correct
directory. So another approach would be to have the top-level
Makefile.PL create the correct Makefile. When doing 'make', things will then
descend into one of the two directories and trigger the standard
procedure 'perl Makefile.PL; make; make test; make install'.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
battery question - nimh vs hybrid nimh spoon2001 Digital Photography 10 06-30-2007 11:50 PM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
TerraTec Cinergy Hybrid USB XS DVB-T/Analog TV Tuner Silverstrand Front Page News 0 10-03-2005 10:39 PM
ECS PF88 Extreme Hybrid Intel/AMD Motherboard Review Silverstrand Front Page News 0 08-03-2005 11:23 PM
accesss denied on downstairs computer. what is node type "hybrid" =?Utf-8?B?S3VlMg==?= Wireless Networking 1 06-30-2005 02:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57