in reply to Procedural and object oriented interface in Perl/XS

Don't write Perl code in XS. Write in XS only the parts that wrap the C library. And write the XS wrapper using a C-friendly interface (you might even want to use Inline::C to write the wrapper to encourage not trying to write Perl code in XS). Then make a nice Perl-like interface over that by writing that interface in Perl.

This will reduce how buggy your code is (XS code is so easy to write bugs in), will surely give you a more robust and Perl-friendly interface, will make it easier to maintain, enhance, and debug your code.

And even with this approach, I would avoid writing subs that try to act as both a method and as a non-method. If you want to support exporting non-method subroutines, then just have exportable tiny wrappers that call the methods (or, if there is absolutely no object data, then you can make the methods be tiny wrappers around the non-methods).

double cos( double x ) /* No need to write the trivial XS wrapper, just the declaration */
package Math::MyWrapper; use Math::MyWrapper::XS(); use Exporter 'import'; our @EXPORT_OK = qw< cos ... >; sub new { require Math::MyWrapper::Class; return "Math::MyWrapper::Class"; } sub cos { my( $x ) = @_; return Math::MyWrapper::XS::cos( $x ); } ...
package Math::MyWrapper::Class; use Math::MyWrapper(); for my $sub ( @Math::MyWrapper::EXPORT_OK ) { eval "sub $sub { Math::MyWrapper::$sub( \@_[1..\$#_] ) }; 1" or die "Error building $sub method: $@\n"; } 1

- tye