aflott has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using sub routines or AUTOLOAD for XS constants
by chromatic (Archbishop) on Jan 01, 2010 at 20:23 UTC | |
by ikegami (Patriarch) on Jan 01, 2010 at 22:23 UTC | |
by chromatic (Archbishop) on Jan 02, 2010 at 10:25 UTC | |
by ikegami (Patriarch) on Jan 02, 2010 at 19:55 UTC | |
by aflott (Beadle) on Jan 01, 2010 at 20:54 UTC |