in reply to load a module or another: Dumper or dd

I'd say slightly Byzantine. I would use Data::Dumper for public modules. But there is Module::Load::Conditional which is in core, and also:

eval { require Data::Dump; 1} or require Data::Dumper;

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: load a module or another: Dumper or dd
by tobyink (Canon) on Jan 10, 2019 at 09:44 UTC

    require always returns a true value when it succeeds. (You know how Perl modules have to end in a true value? It returns that; though repeated use of require to load the same module just returns 1.) So the "1" in your code is completely unnecessary.

    eval { require Data::Dump } or require Data::Dumper;
Re^2: load a module or another: Dumper or dd
by Discipulus (Canon) on Jan 09, 2019 at 22:42 UTC
    Thanks 1nickt and yes Byzantine ;)

    anyway:

    package MyModule; use strict; use warnings; eval { require Data::Dump; 1} or require Data::Dumper; _mydump('this is %INC',\%INC); sub _mydump{ my ( $msg, $ref) = @_; print "$msg:\n"; { local $@; eval {dd $ref}; print Dumper $ref if $@; } } #out perl sopw.pl Name "MyModule::Dumper" used only once: possible typo at sopw.pl line +15. this is %INC: print() on unopened filehandle Dumper at sopw.pl line 15.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hi Discipulus,

      The last message is precedence with print() -- the first is because you don't restrict that code path to the appropriate context. You could use goto conditionally:

      package MyModule; use strict; use warnings; my $Dump; if (eval { require Data::Dump; 1}) { $Dump = sub { goto &Data::Dump::dd }; } else { require Data::Dumper; $Dump = sub { goto &Data::Dumper::Dumper }; } print "this is %INC:\n"; print $Dump->(\%INC); __END__
      Output:
      this is %INC: { "Data/Dump.pm" => "/usr/share/perl5/Data/Dump.pm", "Exporter.pm" => "/usr/share/perl/5.26/Exporter.pm", "overload.pm" => "/usr/share/perl/5.26/overload.pm", "overloading.pm" => "/usr/share/perl/5.26/overloading.pm", "strict.pm" => "/usr/share/perl/5.26/strict.pm", "subs.pm" => "/usr/share/perl/5.26/subs.pm", "vars.pm" => "/usr/share/perl/5.26/vars.pm", "warnings.pm" => "/usr/share/perl/5.26/warnings.pm", "warnings/register.pm" => "/usr/share/perl/5.26/warnings/register.pm +", }


      The way forward always starts with a minimal test.