Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Does this weird piece of code do what you want? (Needless note to others: Don't use this code. Note to self: Don't write such code!)
package StupidAutoloader; use strict; use warnings; use Carp; our %Classes; sub import { my $class = shift; my @hierarchies = @_; %Classes = map {($_, undef)} @hierarchies; *UNIVERSAL::new = sub { my $proto = $_[0]; my $class = ref($proto) || $proto; croak "Could not find method new() in package '$class'." if ref($proto) or exists $INC{$class} or not in_hierarchy(\%Classes, $class); eval "use $class;"; croak "Error loading module '$class': $@" if $@; no strict 'refs'; goto &{"${class}::new"}; }; } sub in_hierarchy { my $h = shift; my $c = shift; return 1 if exists $h->{$c}; my @levels = split /::/, $c; my $accum; foreach (@levels) { $accum .= $_; return 1 if exists $h->{"${accum}::*"} } return 0; } 1;
---
#!/usr/bin/perl use strict; use warnings; use StupidAutoloader 'Data::*'; my $d = Data::Dumper->new([]); print "$d\n"; my $g = Data::Grouper->new(a => []); print "$g\n";

In reply to Re: DWIM: autoloading classes by tsee
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-23 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found