I can see from my original node that I (once again) was not terribly clear. The intent of this idea was to ensure that I had a general purpose module that I could point to any namespace and it would recursively travel through the directories (via File::Find or File::Find::Rule) and prepare those subroutines as I outlined above. Thus, I wouldn't have to change code in any of the classes found.

Further, by just adopting the syntax you listed in Re: The costs of packages, I think I might get exporting to work, also. What follows is a mish-mash of pseudo-code and Perl:

package Class::WhenNeeded; use File::Find; sub import { my ($class, $top_package, $no_top_level) = @_; my @dirs = map { find_top_level_dir($_, $top_package) } @INC; foreach my $dir (@dirs) { find({ wanted => \&create_package_sub($no_top_level), preprocess => \&perl_packages, }, $dir); } } sub create_package_sub { my $strip_top_level = shift; my $module = make_module_name($File::Find::name, $strip_top_ +level); my $call_pack = (caller(1))[1]; if (defined &{$module}) { require Carp; Carp::croak "Function $module already defined"; } *{$module} = sub { undef *{$module}; eval <<" END_EVAL"; package $call_pack; use $module; # export goes to calling package? END_EVAL if ($@) { require Carp; Carp::croak "Could not load module: $module"; } return $module; } }

Hopefully that clears things up.

Cheers,
Ovid

New address of my CGI Course.


In reply to Re: Re: DWIM: autoloading classes by Ovid
in thread DWIM: autoloading classes by Ovid

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.