Pradyot
Pradyot

Reputation: 3059

How can I invoke a script in the same directory from my perl script

I am trying to invoke a script from within a perl script. I can see 2 ways of doing this..

using ./somescript or specifying the full directory path. Neither is ideal, since "./" will only work as long as the invoker cd's into the directory. Full path, is not relative but absolute. Anyway around this? thanks

Upvotes: 0

Views: 383

Answers (3)

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

Use FindBin module if you want find directory where your original script is, and use Cwd if you want to find your current working directory.

Upvotes: 1

Pradyot
Pradyot

Reputation: 3059

One way to do this is to determine the directory your script belongs in , and using that How do I get the full path to a Perl script that is executing?

provides 2 ways to accomplish this.

Upvotes: 1

Soz
Soz

Reputation: 963

To get the full path to the script you're currently executing, you can use either of the following chunks of code from PerlMonks. File::Basename:

#!/usr/bin/perl -w
use strict;
use File::Spec::Functions qw(rel2abs);
use File::Basename;
print dirname(rel2abs($0));

or CWD can be used for this purpose. CWD example:

use Cwd;
my $dir = getcwd;

It should be possible to build up a full path using the base directory, plus your knowledge of the relative placement of the initial script and the second script you'd like to invoke. That way, while you will be using an absolute path to invoke the second script, you're only hardcoding a relative path?

Upvotes: 0

Related Questions