Front page | perl.perl6.users |
Postings from April 2021
Re: locations relative to script location
Thread Previous
From:
Joseph Brenner
Date:
April 1, 2021 05:29
Subject:
Re: locations relative to script location
Message ID:
CAFfgvXWr8=K=qAw2oyO_2c+1w6CYDfm=EfBmmaJ1WioUWt0g1A@mail.gmail.com
Thanks, that's a good detail I'd missed:
> You can also use .parent(3) as a shorthand for .parent.parent.parent or .add('../../../').resolve
And I'd never even noticed ^add_fallback existed... that's a peculiar
one. And not classified as a MONKEY trick, eh?
On 3/30/21, Matthew Stuckwisch <mateu@softastur.org> wrote:
> Using $*PROGRAM.parent for the directory of the current script is what I've
> always used, and it works well.
>
> Note that if you do .add('../../../'), you may need to also call .resolve at
> some point, but I generally just use .parent several times. (I think at
> worst case I've only ever gone up two levels). You can also use .parent(3)
> as a shorthand for .parent.parent.parent or .add('../../../').resolve
>
> Also this just gave me the idea for having some fun with FALLBACK
>
> IO::Path.^add_fallback(
> anon method condition ($name) {
> so $name ~~ /^(great)*grandparent$/
> },
> anon method calculator ($name) {
> $name ~~ /^(great)*grandparent$/;
> my $levels = 2 + $0;
> anon method { self.parent($levels) }
> }
> );
>
> my $file = IO::Path.new: "/etc/foo/bar/abc/xyz.txt";
>
> say $file.parent; # "/etc/foo/bar/abc".IO
> say $file.grandparent; # "/etc/foo/bar".IO
> say $file.greatgrandparent; # "/etc/foo".IO
> say $file.greatgreatgrandparent; # "/etc".IO
> say $file.greatgreatgreatgrandparent; # "/".IO
>
>
>
>> On Mar 29, 2021, at 7:50 PM, Joseph Brenner <doomvox@gmail.com> wrote:
>>
>> I'm looking for the canoncial way to get the location
>> of the currently running script (similar to perl's
>> FindBin), so that I can specify other locations
>> relative to the script location.
>>
>> This does what I want, but I see that the docs say
>> that it's deprecated:
>>
>> my $loc = $*PROGRAM.chdir('..');
>> chdir( $loc );
>>
>> The docs suggest using the .add method instead,
>> But this does not at all do what I want:
>>
>> my $loc = $*PROGRAM.add('..');
>> chdir( $loc );
>>
>> That tries to change the current location to:
>>
>> "/home/bozo/bin/mah_script.raku/.."
>>
>> And that fails, because the script name isn't a directory.
>>
>> Note: I could of course do my own surgey on $*PROGRAM
>> to get the path to the script, but that at least used
>> to be regarded as poor form for portability (e.g. the
>> windows backslash separator vs the unix slash).
>>
>> This works fairly well:
>>
>> my $dir = $*PROGRAM.dirname;
>> chdir( $dir );
>>
>> But it's a little disappointing I can't do this:
>>
>> my $dir = $*PROGRAM.dirname.add('../..');
>>
>> Because apparently "dirname" literally returns the
>> name in string form:
>>
>> No such method 'add' for invocant of type 'Str'
>>
>> Ah, but I guess this works:
>>
>> my $new_loc = $*PROGRAM.parent.add('../../dat');
>> chdir( $new_loc );
>>
>> So, does that look anything like the Right Way?
>
>
Thread Previous