Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

(tye)Re: $PERL_USE=$PERL_USE:

by tye (Sage)
on Nov 17, 2000 at 22:12 UTC ( [id://42252]=note: print w/replies, xml ) Need Help??


in reply to $PERL_USE=$PERL_USE:

Okay, I think you are asking for Data::Dumper->new() to automatically do use Data::Dumper if you haven't already. Okay, that isn't so hard:

package UNIVERSAL; sub new { my $pkg= shift @_; eval "require $pkg; 1" or die "$@"; return $pkg->new( @_ ); } 1;
Just add this function to your existing UNIVERSAL.pm file or put it in AutoNew.pm and set PERL5OPT="-mAutoNew".

Update: Note that putting it in UNIVERSAL.pm probably isn't enough since I think that file isn't necessarilly parsed despite its name, though PERL5OPT="-mUNIVERSAL" is particularly appealing to me.

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

Replies are listed 'Best First'.
(tye)Re2: $PERL_USE=$PERL_USE:
by tye (Sage) on Nov 17, 2000 at 22:38 UTC

    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")
      Going down this path is dangerous. But since you are going down it, may I suggest the additional hack to use CPAN.pm to fetch the specified module if the require fails? :-)
Re: (tye)Re: $PERL_USE=$PERL_USE:
by johannz (Hermit) on Nov 17, 2000 at 23:48 UTC

    In this node RE: Factory Pattern for Class Heirarchy, I took this concept to an extreme and wrote up a UNIVERAL::AUTOLOAD method that will load the package when you refer to ANY method in the package. It's a horrible abuse of the UNIVERSAL and AUTOLOAD methods but it was education to learn how they worked and interacted.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://42252]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found