develooper Front page | perl.perl6.users | Postings from March 2021

locations relative to script location

Thread Next
From:
Joseph Brenner
Date:
March 30, 2021 19:13
Subject:
locations relative to script location
Message ID:
CAFfgvXUhyPEio09W5fFTwfchP65Ez2-=kHZpJFJWuFBV9QEdPg@mail.gmail.com
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 Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About