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

Request: add override for @PATHEXT #40

Open
zmughal opened this issue Oct 11, 2022 · 4 comments
Open

Request: add override for @PATHEXT #40

zmughal opened this issue Oct 11, 2022 · 4 comments

Comments

@zmughal
Copy link

zmughal commented Oct 11, 2022

As per discussion at PDLPorters/pdl#412 (comment) regarding finding a shell script on MSYS2/MinGW64, it might be useful to use File::Which's logic to find scripts that would be run by sh.exe which do not have any suffix.

This override can be similar in function to $IMPLICIT_CURRENT_DIR.

@plicease
Copy link
Member

Another way to handle this would have an option to say "sh mode" which would take these into account. It might make the calling code simpler. It's not really in keeping with the $IMPLICIT_CURRENT_DIR interface tbh, if we did go down this route there should also probably be a "powershell mode".

On the other hand I am tempted by your suggestion because it is less work and it is in keeping with what we have done so far.

@zmughal
Copy link
Author

zmughal commented Oct 12, 2022

Perhaps this can be simplified by introducing an object that encapsulates all
the external %ENV{qw(PATH PATHEXT)} and options. Then which() would just be
a functional interface to the default object.

The object could control behaviour like

# my $is_msys2 = ...;
my $which = File::Which::Object->new(
    # where `File::Which::Object->default_paths` does what which()
    # currently does from $ENV{PATH}
    paths =>
          $is_msys2
        ? My::Tools->mingw_paths
        : File::Which::Object->default_paths,

    # empty pathexts gets turned into [''] internally
    # (if `allow_empty_pathext_suffix`).
    # Could also have `File::Which::Object->default_pathexts`
    # which does what my @PATHEXT = ... does.
    pathexts => IS_WIN ? [qw(.exe .bat .com)] : [],

    # do not filter out '' from pathexts
    allow_empty_pathext_suffix => $is_msys2
);
my $ls = $which->which('ls');
system($ls);

or have

shell => 'bourne (name from Shell::Guess) on MSWin32 set allow_empty_pathext_suffix => 1
while shell => 'cmd' sets allow_empty_pathext_suffix => 0.

Or maybe allow_empty_pathext_suffix should be an internal option
(_allow_empty_pathext_suffix) with only the shell-type being exposed as part
of the public API.

@plicease
Copy link
Member

I like the idea of having an OO interface that doesn't necessarily care about the local operating system, because it will make writing regression tests possible for platforms that we can't easily or regularly test in those real environments. I think the main interface should be something like

# actual names should in no way be considered final or even
# in anyway well thought out.
my $which = File::Which::Object->new(  os => 'msys2_sh' );

So that the each calling code doesn't need to think about the details. I think it is fine to also have overrides for pathext etc for when you need to do that.

I'm not in love with File::Which::Object as the name of the class. File::Which->new might actually work, if we are carefuly to make sure that if File::Which::which is called with just one argument it works in the old way.

@zmughal
Copy link
Author

zmughal commented Oct 13, 2022

Sounds good! I'll take an initial go at this.

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

2 participants