Export::XS - Replacement for Exporter.pm + const.pm in XS, with C++ API.
package MyModule; use Export::XS::Auto; sub mysub { ... } sub mysub2 { ... } 1; package Somewhere; use MyModule qw/mysub mysub2/; mysub();
package MyModule; use Export::XS CONST1 => 1, CONST2 => 'string'; say CONST1; say CONST2;
package MyModule; use Export::XS::Auto { CONST1 => 1, CONST2 => 'string', }; say CONST1; say CONST2; package Somewhere; use MyModule; say CONST1; say CONST2;
#include <xs/export.h> using namespace xs::exp; // one-by-one create_constant(stash, "STATUS_OFF", 0); create_constant(stash, "STATUS_ACTIVE", 1); create_constant(stash, "STATUS_SUSPENDED", 2); create_constant(stash, "STATUS_PENDING", 3); create_constant(stash, "DEFAULT_NAME", "john"); create_constant(stash, "CONST_NAME", value_sv); autoexport(stash); // bulk create_constants(stash, { {"STATUS_OFF", 0}, {"STATUS_ACTIVE", 1}, {"STATUS_SUSPENDED", 2}, {"STATUS_PENDING", 3}, {"DEFAULT_NAME", "john"} }); // exporting subs export_sub(from_stash, to_stash, "myfunc"); // export all constants export_constants(from, to);
It's very fast not only in runtime but at compile time as well. That means you can create and export/import a lot of constants/functions without slowing down the startup.
You can create constants by saying
use Export::XS {CONST_NAME1 => VALUE1, ...}; use Export::XS CONST_NAME1 => VALUE1, ... ;
If you want your class to able to export constants or functions, use Export::XS::Auto instead of Export::XS.
Export::XS::Auto
Export::XS
Exports specified constants and functions to caller's package.
use MyModule qw/subs list/;
Exports nothing
use MyModule();
Exports all constants only (no functions)
use MyModule;
Exports functions sub1 and sub2 and all constants
use MyModule qw/sub1 sub2 :const/;
If Export::XS discovers name collision while creating or exporting functions or constants it raises an exception. If you specify wrong sub or const name in import list an exception will also be raisen.
API is thread-safe. Sv, Stash, and so on is SVAPI classes (Perl C++ API), see XS::Framework. string_view is a panda::string_view which is implementation of C++17's string_view, see XS::libpanda.
Sv
Stash
string_view
panda::string_view
struct Constant { Constant (string_view name, const Sv& val); Constant (string_view name, string_view val); Constant (string_view name, int64_t val); };
Creates constant with name name and value value in package stash. Croaks if package already has sub/constant with that name.
name
value
stash
Creates constant with name constant.name and value <constant.value> in stash.
constant.name
Creates a constant for each element in constants.
constants
Creates a constant for each key/value pair in constants.
Creates a constant for each key/value pair in array list. It means that list[0] is a key, list[1] is a value, list[2] is a key, etc... Array should not contain empty slots and empty keys or it will croak. If elements count is odd, last element is ignored. You must pass the size of list in items.
list
items
Makes stash autoexport its constants when someone says
use ThatClass; # ThatClass is stash.name()
Exports sub/constant with name name from package from to package <to>.
from
Exports all constants from package from to package <to>.
Forces an export of a function with specified name by default (along with constants), i.e. when user says
use SomeClass;
Returns the list of all constants defined in package stash as a perl array.
Export::XS is up to 10x faster than const.pm and Exporter.pm at compile-time. The runtime perfomance is the same as it doesn't depend on this module.
Pronin Oleg <syber@cpan.org>, Crazy Panda LTD
You may distribute this code under the same terms as Perl itself.
To install Export::XS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Export::XS
CPAN shell
perl -MCPAN -e shell install Export::XS
For more information on module installation, please visit the detailed CPAN module installation guide.