Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Just another Perl shrine
 
PerlMonks  

$PERL_USE=$PERL_USE:

by princepawn (Parson)
on Nov 17, 2000 at 14:34 UTC ( [id://42206]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

princepawn has asked for the wisdom of the Perl Monks concerning the following question:

I use Data::Dumper countless times per day on countless machines in countless files. Unfortunately, I have to manually use it everytime. Too bad Perl doesn't have a syntax similar to the one in my title to save me the trouble...

Or, alternatively, a means of auto-using modules explicitly referred to in the code.

Of course though, with EMACS, I could have it automatically place a

use strict; use Data::Dumper;
automatically at the top of every file that I create with a .pl extension.

Replies are listed 'Best First'.
Re: $PERL_USE=$PERL_USE:
by Dominus (Parson) on Nov 17, 2000 at 17:00 UTC
    Says princepawn:
    > Too bad Perl doesn't have ...
    It does. Use
    PERL5OPT='-MData::Dumper';
    If this environment variable is set, then whenever you run Perl it will load Data::Dumper before running your program.

    Why do you always phrase your questions as "Perl doesn't do X" instead of "Is there a way to make Perl do X?"

      >> Why do you always ...

      You could even boldly ask
      "I am sure there must be a way to X in Perl, can anyone please tell me how?"

      Rudif :-)
(tye)Re: $PERL_USE=$PERL_USE:
by tye (Sage) on Nov 17, 2000 at 17:12 UTC

    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")

      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? :-)

      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.

Re: $PERL_USE=$PERL_USE:
by clemburg (Curate) on Nov 17, 2000 at 17:16 UTC

    As you mentioned, an editor template would probably be the best solution to this.

    Another solution (at least under Unix) is to say:

    > alias perl='perl -MData::Dumper' > perl -e 'print Dumper({foo=>"bar"})' $VAR1 = { 'foo' => 'bar' };

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: $PERL_USE=$PERL_USE:
by arturo (Vicar) on Nov 17, 2000 at 17:22 UTC

    If you only want some (or even most) of your scripts to use a module without putting it in the code explicitly, you could also write a shell script perldd or whatever you want) that does it automatically, à la :

    #!/bin/sh perl -MData::Dumper $1

    You could then call that script instead of the "naked" perl interpreter when you need the extra functionality.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://42206]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.