http://www.perlmonks.org?node_id=1168156

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

So I've done a bad thing (TM) I am certain. I wanted to easiliy create a slew of "data files" from exisiting code that could be read back in and executed and later allow us to add many, many more test files. So I opted to use Data::Dumper to create the initial files, fixed them up a bit, then used the following to set the needed values for each test:

{ no strict 'vars'; local $/; my $in; open $in, '<', $test_env or die "..."; my $rc = eval <$in>; close $in; # set various values from that data structure. };

The above is essentially in a loop, reading in each test data file.

Besides the obvious of redesigning the data files as XML or some other, is there anyway to accomplish the above in a way that perlcritic is happy with? I have a bazillion test_xxx.pl files I'd rather not recreate at the moment.

Replies are listed 'Best First'.
Re: perlcritic compliant way to eval?
by haukex (Archbishop) on Jul 20, 2016 at 17:37 UTC

    Hi KeighleHawk,

    The default Perl::Critic doesn't seem to have a policy against the "do FILE" form of invoking code.

    But Perl::Critic is also just a set of hints that are sometimes very helpful, but sometimes the programmer knows better. If you know what you're doing with eval (security-wise etc.), I see no problem with sticking a ## no critic (ProhibitStringyEval) on that line.

    Lastly, if I may shamelessly plug one of my own modules, see my post from a little while ago: Undumping Perl, depending on what your data files look like it might work for you.

    Hope this helps,
    -- Hauke D

      Hauke D

      Thanks for the info. Using 'do' like that had fallen off my knowledge stack. It seemed to work at first for me but is acting wonky, so I'll have to look at it more as well as your other post. Particularly for future use.

      As for bypassing the lint checks, I'll admit, I do have a few coding style quirks of my own. I cuddle my elses and line up my commas different from the rest of the universe. But I've "fixed" enough buggy code just by de-linting them I've become a huge fan such tools and am generally willing to give up my own quirks if I can get the rest of the team to use said tools as well.

      In this case, I am getting ready to turn some code over so would like it to be as clean as possible with stock tools to my successor.

      Thanks again for the pointer to your previous post.

      -- Robert

        Hi Robert,

        'do' ... seemed to work at first for me but is acting wonky

        There are some differences between do and eval, maybe that's the problem?

        "do 'stat.pl'; is largely like eval `cat stat.pl`; except that it's more concise, runs no external processes, keeps track of the current filename for error messages, searches the @INC directories, and updates %INC if the file is found. ... It also differs in that code evaluated with do FILE cannot see lexicals in the enclosing scope; eval STRING does."

        Also do requires some more error checking to figure out what went wrong if the file doesn't compile/run, see do.

        Hope this helps,
        -- Hauke D

Re: perlcritic compliant way to eval?
by ikegami (Patriarch) on Jul 20, 2016 at 20:17 UTC

    You want to do exactly what it warns you is dangerous to do, so the appropriate solution is to simply silence the critic using the directive that does so. Trying to hide that you are doing something dangerous by fooling perlcritic into not noticing is not appropriate.


    Independently, you might find using do($test_env) or die($@ || $!); simpler to use in this case. I don't know if perlcritic warns about that, but that's besides the point.

Re: perlcritic compliant way to eval?
by BrowserUk (Patriarch) on Jul 20, 2016 at 20:46 UTC

    That's a bit like asking: what is an ultra-orthodox acceptable way to secularism. If you join the cult; you have to obey the rules.

    Allowing a bunch of faceless, nameless, unqualified persons to define your coding standards according to their collective failings, misunderstandings, foibles and holy cows; is to guarantee your code is not the sum of all fears; but the product thereof.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      They're named in the documentation from which one can derive faces. Qualified is as qualified does. House style is the prerogative of the house. The packages are entirely, nearly endlessly, configurable. Any critique of the Critic that does not acknowledge this is hogswallow.

        It isn't about the configurability; but rather, the default configuration.

        Facebook offers secure messaging, but the default is insecure; and to use it, you have to reconfigure it every time. Why? Because they don't want you to use it.

        And so it is for the Critic. The defaults are such that they neuter Perl, rendering it verbose, stilted and gutless; Java with sigils.

        But far worse; it gives a stick to those, without the knowledge or judgement to make informed decisions, with which to beat those who could. It sets an arbitrary and capricious standard by which those, without the capacity and drive to learn the language, can hamstring those who have taken the time and effort to do so.

        And worst of all, it implies without stating, some utopian view that compliant code will be good code; and non-compliant code is "bad code".

        Yet, it is easily demonstrated that you can take code that doesn't work and make it fully compliant; and it still won't work.

        And equally, take thoroughly tested and exercised working code; and it will flag up a gazillion red-herrings.

        It is dumb pretending to be smart; rote pretending to be intelligence; it is the failed writer turned film critic; the failed sportsman yelling advice to the professional and berating the referee/umpire.

        It has done more harm to Perl -- by confirming and legitimising every uninformed and malicious opinion of the write-only language -- than any other single thing barring only PBP.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.