Michael G Schwern wrote:
> On Thu, Aug 01, 2002 at 06:22:08PM +0800, Stas Bekman wrote:
> 
>>do we have some place where this trick (technique) can be documented? 
>>I'll submit a patch.
>>
>>or should we start something like perlmmtut.pod? where this kind of 
>>tricks can go to?
> 
> 
> Starting ExtUtils::MakeMaker::FAQ would be nice.

Please find the attached FAQ.pod.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com
package ExtUtils::MakeMaker::FAQ;

our $VERSION = '0.01';

1;
__END__

=head1 NAME

ExtUtils::MakeMaker::FAQ - Frequently Asked Questions About MakeMaker

=head1 SYNOPSIS

=head1 DESCRIPTION

Document tips and tricks for C<ExtUtils::MakeMaker>.

=head1 How to Make Two and More XS files Coexist In The Same Directory?

Sometimes you need to have two and more XS files in the same package.
One way to go is to put them into separate directories, but sometimes
this is not the most suitable solution. The following technique allows
you to put two (and more) XS files in the same directory.

Let's assume that we have a package C<Cool::Foo>, which includes
C<Cool::Foo> and C<Cool::Bar> modules each having a separate XS
file. First we use the following I<Makefile.PL>:

  use ExtUtils::MakeMaker;
  
  WriteMakefile(
      NAME              => 'Cool::Foo',
      VERSION_FROM      => 'Foo.pm',
      OBJECT              => q/$(O_FILES)/,
      # ... other attrs ...
  );

Notice the C<OBJECT> attribute. MakeMaker generates the following
variables in I<Makefile>:

  # Handy lists of source code files:
  XS_FILES= Bar.xs \
        Foo.xs
  C_FILES = Bar.c \
        Foo.c
  O_FILES = Bar.o \
        Foo.o

Therefore we can use the C<O_FILES> variable to tell MakeMaker to use
these objects into the shared library.

That's pretty much it. Now write I<Foo.pm> and I<Foo.xs>, I<Bar.pm>
and I<Bar.xs>, where I<Foo.pm> bootstraps the shared library and
I<Bar.pm> simply loading I<Foo.pm>.

The only issue left is to how to bootstrap I<Bar.xs>. This is done
from I<Foo.xs>:

  MODULE = Cool::Foo PACKAGE = Cool::Foo
  
  BOOT:
  # boot the second XS file
  boot_Cool__Bar(aTHX_ cv);

If you have more than two files, this is the place where you should
boot extra XS files from.

The following four files sum up all the details discussed so far.

  Foo.pm:
  -------
  package Cool::Foo;
  
  require DynaLoader;
  
  our @ISA = qw(DynaLoader);
  our $VERSION = '0.01';
  bootstrap Cool::Foo $VERSION;
  
  1;
  
  Bar.pm:
  -------
  package Cool::Bar;
  
  use Cool::Foo; # bootstraps Bar.xs
  
  1;

  Foo.xs:
  -------
  #include "EXTERN.h"
  #include "perl.h"
  #include "XSUB.h"
  
  MODULE = Cool::Foo  PACKAGE = Cool::Foo
  
  BOOT:
  # boot the second XS file
  boot_Cool__Bar(aTHX_ cv);
  
  MODULE = Cool::Foo  PACKAGE = Cool::Foo  PREFIX = cool_foo_
  
  void
  cool_foo_perl_rules()
  
      CODE:
      fprintf(stderr, "Cool::Foo says: Perl Rules\n");

  Bar.xs:
  -------
  #include "EXTERN.h"
  #include "perl.h"
  #include "XSUB.h"
  
  MODULE = Cool::Bar  PACKAGE = Cool::Bar PREFIX = cool_bar_
  
  void
  cool_bar_perl_rules()
  
      CODE:
      fprintf(stderr, "Cool::Bar says: Perl Rules\n");

And of course a very basic test:

  test.pl:
  --------
  use Test;
  BEGIN { plan tests => 1 };
  use Cool::Foo;
  use Cool::Bar;
  Cool::Foo::perl_rules();
  Cool::Bar::perl_rules();
  ok 1;

This tip has been brought to you by Nick Ing-Simmons and Stas Bekman.

=head1 AUTHOR

=head1 SEE ALSO

perl(3)

=cut

Reply via email to