Perl: mocking -d -f and friends. How to put them into CORE::GLOBAL

前端 未结 5 1578
遇见更好的自我
遇见更好的自我 2020-12-10 04:08

The CORE documentation has shown me how to merrily mock various built Perl functions. However, I\'m not really sure how to replace \'-d\' &c. with my methods. So this is

5条回答
  •  孤城傲影
    2020-12-10 05:05

    Thanks all for your answers.

    What I wound up doing is, on a per module/test target basis, I factored out the code with the "-d" into it into it's own function. Like so...

    # Because I cannot mock -d directly
    sub dirExists {
        return -d shift;
    }
    

    Then I can replace this function in the test module with like

    my $doesDirExist = 1;
    *MyModule::dirExists   = \&main::mock_dirExists;
    
    sub mock_dirExists {
        return $doesDirExist;
    }
    

    It's pretty ugly but i didn't want to get hung up on this too long and it works well enuf for my purposes

提交回复
热议问题