dagolden / Path-Tiny

File path utility
41 stars 59 forks source link

Calling file relative path creation using scalar references. #289

Closed drclaw1394 closed 7 months ago

drclaw1394 commented 7 months ago

Using a reference to an unblessed scalar, create a path relative to the calling file instead of the current working directory. This makes it easy to reference file located relative to the calling .pm or script file in a very concise way.

For example if a script located at "~/jsmith/test.pl" creates a Path::Tiny object like so: my $path=path(\"foo.txt"); # or Path::Tiny->new("foo.txt")

The resulting $path will be ~/jsmith/foo.txt, regardless of the current working dir.

Added two tests for path(...) and Path::Tiny->new(...) path construction.

Thanks for your consideration

Ruben

xdg commented 7 months ago

I appreciate the offer -- coming in the form of a PR and with tests(!), but I think this behavior is too magical and a bit obscure for what I'm trying to preserve as a "Tiny" library. On that basis, I feel I need to say no to the suggestion.

I'm not entirely sure I understand the semantics you want, but I would solve this with sibling like so:

my $path=path($0)->sibling("path/to/somefile.txt");

Given $0 of ~/jsmith/test.pl, the resulting path would be ~/jsmith/path/to/somefile.txt.

ap commented 7 months ago

I'm not entirely sure I understand the semantics you want

Based on a reading of the code and a handful of tests to confirm, the precise equivalent is

path(__FILE__)->sibling("path/to/somefile.txt")

The code in the patch looks for the first call frame outside of Path::Tiny and gets the filename from that, i.e. whatever __FILE__ is at the call site that called path(). (Except when that isn’t a file, in which case it ends up as just path("path/to/somefile.txt") with extra steps.)

But comparing how utterly obvious the meaning of that line is vs this line…

path(\"path/to/somefile.txt")

… and that this 18-character reduction in length and clarity comes at a cost of 20 lines of internals-y code in Path::Tiny, plus a paragraph of docs and several tests… 🤷‍♀️

Design is hard. 😐

drclaw1394 commented 7 months ago

Appreciate the feedback,

Sure, sporadic use of path(FILE)->sibling(“…”) works just fine. However it is very long for .pm/.pl relative files I use all the time.

Some examples of why I think this is very useful: — HTML template system with lots of images relative to the ‘processing’ .pm file or script. Saves a lot of typing and noise in the template. — Partial server/module configurations loading several local files. Again less noise to make the code easy to read and extract the meaning.

As for the conveyed meaning of the line, I would argue I would be no harder to understand than ’slurp’ and ’spew’ , which is only obvious (to me) after reading the docs.

Perhaps the implementation could be improved, but docs and tests are a good thing surely.

Anyway, it’s not just 18 characters to me so I thought it might be useful to other people also.

Ruben

On 4 Mar 2024, at 6:34 am, Aristotle Pagaltzis @.***> wrote:

I'm not entirely sure I understand the semantics you want

Based on a reading of the code and a handful of tests to confirm, the precise equivalent is

path(FILE)->sibling("path/to/somefile.txt") The code in the patch looks for the first call frame outside of Path::Tiny and gets the filename from that, i.e. whatever FILE is at the call site that called path(). (Except when that isn’t a file, in which case it ends up as just path("path/to/somefile.txt") with extra steps.)

But comparing how utterly obvious the meaning of that line is vs this line…

path(\"path/to/somefile.txt") … and that this 18-character reduction in length and clarity comes at a cost of 20 lines of internals-y code in Path::Tiny, plus a paragraph of docs and several tests… 🤷‍♀️

Design is hard. 😐

— Reply to this email directly, view it on GitHub https://github.com/dagolden/Path-Tiny/pull/289#issuecomment-1975312590, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5ZTKJD7TTPBBXMFREYXKLYWOCMBAVCNFSM6AAAAABED45Q3KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZVGMYTENJZGA. You are receiving this because you authored the thread.

drclaw1394 commented 7 months ago

No problem. I use .pm/.pl relative paths a lot and though this might be of use to others.

Ruben

On 3 Mar 2024, at 11:47 pm, David Golden @.***> wrote:

I appreciate the offer -- coming in the form of a PR and with tests(!), but I think this behavior is too magical and a bit obscure for what I'm trying to preserve as a "Tiny" library. On that basis, I feel I need to say no to the suggestion.

I'm not entirely sure I understand the semantics you want, but I would solve this with sibling like so:

my $path=path($0)->sibling("path/to/somefile.txt"); Given $0 of ~/jsmith/test.pl, the resulting path would be ~/jsmith/path/to/somefile.txt.

— Reply to this email directly, view it on GitHub https://github.com/dagolden/Path-Tiny/pull/289#issuecomment-1975170177, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5ZTKPJQSM4WNTTZ6XMNZLYWMSYBAVCNFSM6AAAAABED45Q3KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZVGE3TAMJXG4. You are receiving this because you authored the thread.