Having switched to Sub::Exporter from Exporter, I came across functionality differences. If one of your sub routine names is not defined, Sub::Exporter croaks where as Exporter does not export that name.
Currently I have a number of constants that get AUTOLOADed through:
# This AUTOLOAD is used to 'autoload' constants from the constant() XS + function. sub AUTOLOAD { my $constname; our $AUTOLOAD; ($constname = $AUTOLOAD) =~ s/.*:://; croak "&Blah::constant not defined" if $constname eq 'constant'; my ($error, $val) = constant($constname); if ($error) { croak $error; } { no strict 'refs'; *$AUTOLOAD = sub { $val }; } goto &$AUTOLOAD; }
(this being the standard h2xs boilerplate code).
Is it better practice to setup 1 sub routine for each constant such as:
foreach my $constname (@constants) { { no strict 'refs'; *$constname = sub { use strict; my ($error, $val) = constant($constname); if ($error) { croak $error; } return $val; } } }
for Sub::Exporter?
The only downside I can see is the added code overhead, but not having AUTOLOAD around seems like a win.
In reply to Using sub routines or AUTOLOAD for XS constants by aflott
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |