txpsj has asked that I explain how this works, so here goes.

All packages implicitly inherit from the UNIVERSAL package. This is like every package's @ISA always ending in 'UNIVERSAL' no matter what you set a package's @ISA to be.

So if you do Data::Dumper->new(), then Perl looks for "sub Data::Dumper::new". If you've already done use Data::Dumper or require Data::Dumper, then Perl finds it and things progress as normal.

But, if Perl doesn't find it, it looks in @Data::Dumper::ISA for packages that Data::Dumper inherits from in case the new() method is supposed to be inherited. But no Data::Dumper::new() means no @Data::Dumper::ISA yet so it is empty. But Perl still implicitly includes the UNIVERSAL package in the list and so looks for "sub UNIVERSAL::new". Well, we've given Perl one to find.

So Data::Dumper->new( "x", "y" ) becomes UNIVERSAL::new( "Data::Dumper", "x", "y" ). So we pull the package name off the argument list and do a require on it. This should define a real "sub Data::Dumper::new" which we then call.

All this brings to my mind some improvements I should make. First, it would be nice to "goto" the real new() so that the call stack doesn't show the extra UNIVERSAL::new() in case new() wants to look at caller(), for example. Also, if require Data::Dumper didn't define "sub Data::Dumper::new", then we'd be in a infinite loop.

So here is a better version (updated):

package UNIVERSAL; use strict; sub new { my( $pkg )= @_; eval "require $pkg; 1" or die "$@"; my $meth= $pkg->can( "new" ); die "Package $pkg defines no new() method" if $meth == \&UNIVERSAL::new; goto &$meth; } 1;
Now put this in AutoNew.pm and change the two lines you always start all of your scripts with to:
#!/usr/bin/perl -w -mAutoNew use strict;
Now you can create whatever objects you want without predeclaring what packages you need. This, of course, makes you code harder to maintain and I'd strongly advise against it for production code. But it was fun.

        - tye (but my friends call me "Tye")

In reply to (tye)Re2: $PERL_USE=$PERL_USE: by tye
in thread $PERL_USE=$PERL_USE: by princepawn

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.