Re: Determine script full path.



tower.grv@xxxxxxxxx wrote:
In my Perl script I need to determine where this script is .
Ex.script.pl has full path /tmp/1/script.pl Whet it starts it must to
know where script file is (/tmp/1).

I have found the solution.

No you haven't.

use Cwd;
$dir = cwd();

"Cwd" stands for "current working directory". That is not necessarily
the same as the directory in which your script resides.

Put your script in /tmp/1, as you have it. Change to your own home
directory, and execute the script as /tmp/1/script.pl, and you will see
that your script assigns $dir to your home directory, not /tmp/1

The correct answer is to use the FindBin module, not the Cwd module:

$ cat temp/script.pl
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use FindBin;
print "Cwd: ", cwd(), "\n";
print "FindBin: ", $FindBin::Bin, "\n";
__END__

$ temp/script.pl
Cwd: /home/plalli
FindBin: /home/plalli/temp


Paul Lalli

.



Relevant Pages

  • Re: multiple processes, private working directories
    ... script; call the script in separate threads. ... You want to provide the cwd argument above. ... Then once you have launched all your n processes, run thru' a loop ... # cmds is a list of dicts providing details on what processes to run.. ...
    (comp.lang.python)
  • Re: Permission Denied error when moving files - Perl
    ... files with a *.PDF extension. ... & delete I get a Permission Denied error from the script. ... my $cwd = getcwd; ...
    (comp.lang.perl.misc)
  • Re: oddity regarding execution
    ... and fired off a test message. ... > Well, when the alias fed the message to it, it barked. ... I modified the script to ... And is there anything I can do so the cwd will be the dir the ...
    (freebsd-questions)
  • Re: dynamically importing a module and function
    ... i just wanted to stick to primitive python data types. ... cwd = data ... Try this in your script so that you can see exactly what it is doing, instead of comparing it to a strawman: ...
    (comp.lang.python)
  • Re: PHP 5 function redeclared error
    ... >to chdir(), the cwd is the same as the script directory. ... cwd is searched first, ...
    (comp.lang.php)