Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require_ok "open" fails #889

Open
ikegami opened this issue Jan 18, 2022 · 4 comments
Open

require_ok "open" fails #889

ikegami opened this issue Jan 18, 2022 · 4 comments

Comments

@ikegami
Copy link

ikegami commented Jan 18, 2022

require_ok "open" fails by trying to use the open operator.

This can be resolved by replacing

    # Try to determine if we've been given a module name or file.
    # Module names must be barewords, files not.
    $module = qq['$module'] unless _is_module_name($module);
 
    my $code = <<REQUIRE;
package $pack;
require $module;
1;
REQUIRE

with

    # Try to determine if we've been given a module name or file.
    # Module names must be barewords, files not.
    my $code = ( _is_module_name($module)
        ? "require '$module'"
        : "use $module ()"
    );
 
    $code = <<REQUIRE;
package $pack;
$code;
1;
REQUIRE
@exodist
Copy link
Member

exodist commented Jan 18, 2022

Unfortunately this change could break things. There is a lot of code on cpan that overrides require, any code testing a require override using require_ok could potentially break if use does not call the override (I am not sure it would, though putting a sub in @inc should still work).

This is another affirmation of the Test2 decision not to implement use_ok or require_ok.

I think for back-compatibility of Test::More we should simply document this limitation and move on, the fix may be more problematic than the problem being solved.

I will leave this ticket open for a while to solicit feedback from you and anyone else who wants to weigh-in on the pros and cons. If anyone has the capacity right now to look into the effects on a require-override and/or verify against a large chunk of cpan with such a change in place I would welcome that, I do not have the time at the moment. If this fix can be proven safe for back-compatibility I will consider it.

@haarg
Copy link
Member

haarg commented Jan 19, 2022

use will also trigger require overrides, but it would change the error messages somewhat.

A better approach would probably be to translate module names into "Module/Name.pm" form before requiring them.

@exodist
Copy link
Member

exodist commented Jan 19, 2022

What is the current recommended way to do that? I usually do the naive $mod =~ s{::}{/}g; $mod .= ".pm" change. But I am aware that this could be wrong on non-unix systems, and has some potential issues with unicode sequences. Is the naive approach sufficient, or is there a core module I can depend on (with sufficient version back to 5.8) that has a working implementation? Or do I need to copy a "correct" implementation and inline it?

@haarg
Copy link
Member

haarg commented Jan 19, 2022

$mod =~ s{::}{/}g; $mod .= ".pm" is the correct approach, including on non-unix systems. The only thing extra done by 'recommended' modules like Module::Runtime is to validate that it looks like a module name first. In the case of Test::More, that's already being done with _is_module_name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants