Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Determining directory where script was called from

by nysus (Vicar)
on Mar 13, 2019 at 02:38 UTC ( #1231192=perlquestion: print w/replies, xml ) Need Help??
nysus has asked for the wisdom of the Perl Monks concerning the following question:

I have a per script in ~/bin/ that I'm calling from another directory. I tried using `getcwd()` to get the current working directory where the script was called from but it is returning undefined. How can I get the directory the script's command is getting issued from?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

  • Comment on Determining directory where script was called from

Replies are listed 'Best First'.
Re: Determining directory where script was called from
by thanos1983 (Parson) on Mar 13, 2019 at 10:20 UTC

    Hello nysus,

    According to the documentation of File::Spec it is compatible with all OS and it will load automatically the relevant module based on the OS.

    If you want to have compatibility with all OS I think this module is a good candidate :).

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Determining directory where script was called from
by nysus (Vicar) on Mar 13, 2019 at 02:42 UTC
    NVM. It's in $ENV{PWD};

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Hello nysus,

      Glad $ENV{PWD} is working for you, but it doesn’t work for me on Windows:

      15:51 >perl -wE "say $ENV{PWD};" Use of uninitialized value in say at -e line 1. 15:51 >

      On the other hand, Cwd::getcwd() [or Cwd::cwd()] works fine for me, and is a more portable solution.

      I tried using `getcwd()` to get the current working directory where the script was called from but it is returning undefined.

      Was your call to the remote script something like this?

      printf "\n%s\n", `perl remote_dir/remote_script.pl`;

      If so, you’ll get undefined because backticks in list context returns a list (of lines; see perlop#Quote-Like-Operators). In scalar context it works as expected:

      printf "\n%s\n", scalar `perl 1984_SoPW/remote.pl`;

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231192]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2019-03-20 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (101 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!