Finally you are obviously confused about how this works because you don't know whether you need to both import AUTOLOAD and inherit. The answer is that if you don't have an import method in ABC.pm, then the use statement does nothing and should be removed. If you have an import that will export AUTOLOAD, then the AUTOLOAD written for ABC.pm will fail to work in another package because it will be looking at $ABC::AUTOLOAD for the requested function, but Perl will put it in $XYZ::ABC::Scalar::AUTOLOAD instead and you won't find it properly. Therefore the use should definitely be removed.
A much cleaner solution is at the end to have something that looks like this:
This will create the 6 packages, set all of the right methods, not create any unwanted additional methods, and gets rid of the overhead of running the AUTOLOAD every time you want to access one of the tied methods. Also by moving logic out of the AUTOLOAD you make the logic that remains in the AUTOLOAD clearer. Plus if anyone has code that uses the can method for introspection, it will work on those packages.# Presumably you have 6 packages listed in this list for my $package (qw( XYZ::ABC::Scalar )) { $package =~ /(Scalar|Array|Hash)(::NoLock)?/ or die "Tie type of '$package' not understood"; my $kind = $1; my $nolock = $2; no strict 'refs'; *{$package . uc("::TIE$kind")} = sub { my $class = shift; my $ssh = shift; # Scalar/Hash/Array return bless $ssh, $class; }; # These two do nothing. *{"$package\::DESTROY"} = sub {}; *{"$package\::UNTIE"} = sub {}; # And now the rest. my @functions = qw(FETCH STORE); if ($kind eq "Array") { push @functions, qw( FETCHSIZE STORESIZE EXTEND CLEAR POP PUSH SHIFT UNSHIFT SPLICE ); } elsif ($kind eq "Hash") { push @functions, qw( STORE DELETE CLEAR EXISTS FIRSTKEY NEXTKEY SCALAR ); } for my $function (@functions) { my $subname = join "_", ($nolock ? "abca" : "abc"), lc($kind) +. lc($function); *{"$package\::$function"} = \&$subname; } }
The moral? AUTOLOAD is a big sledgehammer. Don't swing it unless you need to, and then swing it carefully.
In reply to Re^5: Autoloading tie routines
by tilly
in thread Autoloading tie routines
by cmac
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |