Discipulus, here's an example using the autoload function from the Module::Load distribution. The autoload will bring in all available functions from the loaded module, where the load function only literally "requires" it (ie. doesn't bring in any functions):

package MyModule; use strict; use warnings; use Module::Load; my $dd_loaded; BEGIN { $dd_loaded = eval { autoload Data::Dump; 1; }; use if ! $dd_loaded, 'Data::Dumper'; } _mydump(\%INC); sub _mydump{ my ($ref) = @_; if ($dd_loaded){ print "using Data::Dump dd\n"; dd $ref; } else { print "using Data::Dumper\n"; print Dumper $ref; } }

I have Data::Dump installed, so here's my output:

using Data::Dump dd { "bytes.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/bytes.pm", "Carp.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/Carp.pm", "constant.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/constant.pm", "Data/Dump.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/site_perl/5.26.1/Data/Dump.pm", "Data/Dumper.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/x86_64-linux/Data/Dumper.pm", "Exporter.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/Exporter.pm", "File/Spec.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/x86_64-linux/File/Spec.pm", "File/Spec/Unix.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/x86_64-linux/File/Spec/Unix.pm", "if.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/if.pm", "Module/Load.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/Module/Load.pm", "overload.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/overload.pm", "overloading.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/overloading.pm", "strict.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/strict.pm", "subs.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/subs.pm", "vars.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/vars.pm", "warnings.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/warnings.pm", "warnings/register.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/warnings/register.pm", "XSLoader.pm" => "/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/XSLoader.pm", }

Now, if I change the following line in that script:

$dd_loaded = eval { autoload Data::Dump; 1; };

To this (note the "x" at the end of Data::Dump):

$dd_loaded = eval { autoload Data::Dumpx; 1; }; # ^ <-- here

...the output changes, and it falls back on Data::Dumper:

using Data::Dumper $VAR1 = { 'Carp.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26.1/li +b/5.26.1/Carp.pm', 'constant.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26. +1/lib/5.26.1/constant.pm', 'Exporter.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26. +1/lib/5.26.1/Exporter.pm', 'warnings/register.pm' => '/home/spek/perl5/perlbrew/perls/p +erl-5.26.1/lib/5.26.1/warnings/register.pm', 'Module/Load.pm' => '/home/spek/perl5/perlbrew/perls/perl-5. +26.1/lib/5.26.1/Module/Load.pm', 'XSLoader.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26. +1/lib/5.26.1/XSLoader.pm', 'vars.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26.1/li +b/5.26.1/vars.pm', 'File/Spec.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26 +.1/lib/5.26.1/x86_64-linux/File/Spec.pm', 'Data/Dumper.pm' => '/home/spek/perl5/perlbrew/perls/perl-5. +26.1/lib/5.26.1/x86_64-linux/Data/Dumper.pm', 'strict.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26.1/ +lib/5.26.1/strict.pm', 'if.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26.1/lib/ +5.26.1/if.pm', 'bytes.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26.1/l +ib/5.26.1/bytes.pm', 'warnings.pm' => '/home/spek/perl5/perlbrew/perls/perl-5.26. +1/lib/5.26.1/warnings.pm', 'File/Spec/Unix.pm' => '/home/spek/perl5/perlbrew/perls/perl +-5.26.1/lib/5.26.1/x86_64-linux/File/Spec/Unix.pm' };

I use a BEGIN block in this case, as well as a global variable to hold the status of the Data::Dump load, but this is after all, just an example.


In reply to Re: load a module or another: Dumper or dd by stevieb
in thread load a module or another: Dumper or dd by Discipulus

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.