dagolden / Path-Tiny

File path utility
41 stars 59 forks source link

proposal: `home` function #294

Closed karenetheridge closed 1 month ago

karenetheridge commented 1 month ago

File::HomeDir::Tiny was created to simplify the code from File::HomeDir used to get the calling user's home directory.

Unfortunately it has two flaws:

The correct implementation can be defined thusly:

sub home {
  $^O eq 'MSWin32' && "$]" < 5.016
    ? $ENV{HOME} || $ENV{USERPROFILE}
    : (<~>)[0]
}

Would an interface capturing this logic be suitable for Path::Tiny?

ap commented 1 month ago

Maybe even along these lines?

use constant _ENV_HOME_ONLY => $^O eq 'MSWin32' && "$]" < 5.016;
sub home { _ENV_HOME_ONLY ? _path( $ENV{HOME} || $ENV{USERPROFILE} ) : path('~') }

Path::Tiny loads constant already, so doing the same compile-timing of the condition as File::HomeDir::Tiny does, but cleanly, costs it nothing.

xdg commented 1 month ago

This feels out of scope for Path::Tiny, which already support path('~'). The rest of the functionality of OS-specific ENV var knowledge, which isn't really about path manipulation. I'm going to pass.

ap commented 1 month ago

That’s not quite right. On perls since 5.16, glob itself checks these environment variables on Windows, and so on perls since 5.16, path('~') will DTRT on Windows. On older perls, glob doesn’t do that, and so path('~') doesn’t quite work right on Windows. It’s not really OS-specific env var knowledge, it’s basically a glob polyfill.

I agree that path('~') is the Path::Tiny-ish way of doing this though. So on second look, if something like this were to go in, the right place would be (basically) to replace this sub path line:

my ($homedir) = File::Glob::bsd_glob($escaped);

with something like

use constant HAVE_INCOMPLETE_DOSISH_GLOB => "$]" < 5.016;
local $File::Glob::ERROR;
my ($homedir) = IS_WIN32() && HAVE_INCOMPLETE_DOSISH_GLOB() && $tilde eq '~'
    ? $ENV{HOME} || $ENV{USERPROFILE}
    : File::Glob::bsd_glob($escaped);

(just for brevity of illustration – actually that scope should be rearranged to make more sense of course).

That would then allow path('~') to fully obviate File::HomeDir::Tiny.

xdg commented 1 month ago

That's definitely better, but I think I'm going to stick with saying "no". I strongly suspect that Perl users on Windows with Perl < 5.16 are not likely to be installing the latest Path::Tiny from CPAN or have already worked around these problems in their applications.