Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

require and use strict vars

by karlberry (Acolyte)
on Dec 02, 2021 at 17:58 UTC ( #11139324=perlquestion: print w/replies, xml ) Need Help??

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

Occasionally I want to "require" a module, rather than "use" it (to avoid loading it at compile-time) and still "use strict". What is the best way to handle variable references into the module? My example:
use strict; use warnings; require Data::Dumper; local $Data::Dumper::Indent = 0;
yields Name "Data::Dumper::Indent" used only once: possible typo. I am not surprised by the warning, require happening at runtime and all; the question is how to get rid of it.

Clearly "use vars" and "our" do not help, since they don't take variable names in another module. Specifying "no warnings 'once'" works, but I don't want to turn off these warnings in general, just because I want to set a module's configuration variable.

The only way I have found to avoid the warning is to add some additional null usage, as in:

$Data::Dumper::Indent = $Data::Dumper::Indent;
This does not seem like it should be necessary. I feel like I'm missing something obvious, but I'm failing to discern the answer. Documentation reading and web searches have not been successful. Thanks for any info ...

Replies are listed 'Best First'.
Re: require and use strict vars
by ikegami (Patriarch) on Dec 02, 2021 at 19:09 UTC
    # Compensate for delayed/conditional load of Data::Dumper. package Data::Dumper { our $Indent; # Avoids `used only once` warnings. sub Dumper; # Avoids having to use parens. } # Demo require Data::Dumper; $Data::Dumper::Indent = 0; print Data::Dumper::Dumper $data;
    If you want to import,
    # Compensate for delayed/conditional load of Data::Dumper. package Data::Dumper { our $Indent; # Avoids `used only once` warnings. } sub Dumper; # Avoids having to use parens. # Demo require Data::Dumper; import Data::Dumper; $Data::Dumper::Indent = 0; print Dumper $data;

    The sub declarations can also be used to provide the prototype.

Re: require and use strict vars
by LanX (Sage) on Dec 02, 2021 at 18:09 UTC
    $Data::Dumper::Indent is fully qualified , you don't need resp. can't declare it.

    Workarounds:

    • you can disable the specific warning
    • eval the use and your settings
    D:\tmp>perl use strict; use warnings; eval <<'__CODE__'; use Data::Dumper; $Data::Dumper::Indent = 0; __CODE__ __END__ D:\tmp>

    update

    Full demo:

    NB: in order to use Dumper w/o parens or leading '&'-sigil you will need to declare it at compile time with subs

    D:\tmp>perl use strict; use warnings; #use subs qw/Dumper/; eval <<'__CODE__'; use Data::Dumper; $Data::Dumper::Indent = 0; __CODE__ print Dumper( [map {{$_=>$a++}} "a".."d"] ); __END__ $VAR1 = [{'a' => 0},{'b' => 1},{'c' => 2},{'d' => 3}]; D:\tmp>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      wow. would not have come up with that. thanks.
Re: require and use strict vars
by Anonymous Monk on Dec 02, 2021 at 19:49 UTC

    Also possible (since There's More Than One Way To Do It):

    require Data::Dumper;
    no warnings 'once';
    local $Data::Dumper::Indent = 0;
    use warnings 'once';
    

    If the local is in a sufficiently-small scope, omit the use warnings.

    Shameless plug: See also My Favorite Warnings: once.

Re: require and use strict vars (Data Dumper for DOOP)
by Anonymous Monk on Dec 03, 2021 at 11:07 UTC
    Use your own dumper :) its only natural :D
    sub Dumper { use Data::Dumper(); print Data::Dumper->new([@_]) ->Sortkeys(1) ->Indent(1) ->Useqq(1)->Dump . "\n"; }
      This would still use the module at compile time, not when the sub is called.
        So? switch the use to require

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11139324]
Approved by LanX
Front-paged by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-05-22 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (79 votes). Check out past polls.

    Notices?