Are you aware of everything that the AUTOLOAD has done with those 6 packages? Every constant in ABC, every method in ABC, all are inherited by XYZ::ABC::Scalar. Is this really what you want? Furthermore ABC looks like an example of the God module anti-pattern. That is a code smell right there.

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:

# 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; } }
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.