3

I tend to use Data::Dumper very often, and I end up having the following boilerplate at the top of every package in my .pl code.

use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Deparse = 1;
$Data::Dumper::Purity = 1;

Is there a way to specify that "inside the current .pl file, I want these statements to automatically assumed."

eg I would have

package foo;
    use strict;
    use warnings;
    use Data::Dumper;
    $Data::Dumper::Deparse = 1;
    $Data::Dumper::Purity = 1; 

    my @localState = (1, 2, 3, 4, 5);

    sub test {
        print Dumper \@localState;
    }

package main;
    use strict;
    use warnings;
    use Data::Dumper;
    $Data::Dumper::Deparse = 1;
    $Data::Dumper::Purity = 1;

    foo->test;

this can quickly get way too much boilerplate repetition and harm maintainability.

Sadly I can't use a function with "eval" in it to call all this boilerplate since that boilerplate would be placed into the function, not global scope; Perl does not have Lisp macros that I know of to actually have non-function bound eval-like bahavior(I could be wrong, would be so cool if Perl had lisp macros).

Does anyone know if this behavior can be achieved without writing a parser to insert the statements in for me if it is the first package being declared inside file?

Dmytro
  • 5,068
  • 4
  • 39
  • 50

1 Answers1

5

You can build your own toolbox module that turns on pragmas, loads modules and sets stuff, and just load that. The module Import::Into us great for that.

Here is a blog post that explains how to do it.

But note that the config for Data::Dumper that you are setting is actually not related to the package you're setting it in. Those are package variables in the Data::Dumper package, so they are valid all the time once set. You're essentially overwriting them with the same stuff again in your example.

In production code you should usually not put multiple packages in one file unless you have a good reason. But that doesn't change any of the advice above.

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • 1
    This is a link only answer because I wrote it from my phone. Will add code examples later. – simbabque Dec 03 '16 at 23:35
  • Very helpful already, as it stands :) I've been pondering how to avoid half-a-dozen+ same imports across dozen+ scripts in a large project. So far I found nothing that worked well enough, will be happy to check this out! – zdim Dec 04 '16 at 01:27
  • Oh ... this _actually works_ -- it does what one imagines, and it's simple. Thank you. It solved a very pesky problem in a very large project. – zdim Dec 04 '16 at 03:03
  • @zdim im glad to hear that. It's always nice to head to SO and find an answer to something that's been bothering you for a while on the first page. I've never actually used this myself for anything, but I saw [a talk about it](https://youtu.be/9aCsUxfRksE) like five times. If your job is to edit the talk videos and upload them then those kinds of things just stick in your head. :-) – simbabque Dec 04 '16 at 10:59
  • 1
    Yes it is nice, now I know. Normally I solve my problems (SO has helped in a deeper way) but learning how to do this required time I don't have now. The module you identified is spot on (and this is not the first time you do that :). It took me about half an hour, from cold start, to do what I need. (The blog was needed though. The docs should be made a feasible starting point.) Code to write is crystal clear and simple, while there is a lot of flexibility in it as well (for simpler stuff at least). I don't yet know how solid it is, specially for more complex work. Thank you again. – zdim Dec 05 '16 at 09:08
  • @zdim I'm sure Matt would appreciate a pull request with more documentation. I can basically hear him saying _well volunteered_ just now. :) – simbabque Dec 05 '16 at 09:19
  • 1
    Yeah, that sounds about right :) I'll need to first get acquinted with it well enough, to implement full changes in my code and so learn about it. Then I'll see how to do that, as I never have done it. It's a logical next step in 'giving back' for me – zdim Dec 05 '16 at 22:02