Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Getting logical path from Perl?

by Tux (Canon)
on Mar 08, 2021 at 15:47 UTC ( [id://11129342]=note: print w/replies, xml ) Need Help??


in reply to Getting logical path from Perl?

I interpret the question as : I only want to run this script if it is invoked from the symlinked folder.

Use Path::Tiny if you want to stay portable (as posted earlier).

Check -l, readlink, and Cwd::abs_path to fill your needs.

#!perl use Cwd qw( getcwd abs_path ); my ($DIR, $CMD) = $0 =~ m{^(.*)/([^/]+)$}; ! $CMD && ! -d $DIR and ($DIR, $CMD) = (getcwd, $DIR); # "." in $PATH +and script invoked without ./ -l $DIR or die "$DIR is not the symlinked folder. Running like this is + dangerous!\n"; say "Current dir : ", getcwd; say "Invoking folder: ", $DIR; say "Symlink : ", readlink $DIR; say "Abs path: : ", abs_path $DIR;
$ mkdir -p tmp/test-a $ ln -s test-a tmp/test $ ln -s tmp/test test $ vi tmp/test/test.pl -- paste above code and save $ perl test/test.pl Current dir : /home/tux Invoking folder: test Symlink : tmp/test Abs path: : /home/tux/tmp/test-a

Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129342]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-19 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found