Link to home
Start Free TrialLog in
Avatar of mbasov
mbasov

asked on

Proper Path

I have a Perl program that reads one file and writes to another file. Both files located in the same directory where the program is located c:\test lets say. If I am running the program by calling it from c:\perl test.pl it looks for files in c instead of c:\test.

How can resolve this issue?

use Cwd;
$PATH = cwd();
print "$PATH"

open(SRC, "$PATH/mun.txt") || die ("Cannot find the file");
open(DAT, ">$PATH/mun1.csv") || die("Cannot Open File");

Thank you.
Avatar of Sergy Stouk
Sergy Stouk
Flag of Canada image

I have been using the following method without any problems:


$CurrentPath = &CWD;

########################################################################
sub main::CWD
########################################################################
{
use FindBin qw($Bin);
# print "The script resides in the directory $Bin\n";
unless ($Bin =~ m!/$!) {$Bin .= "/"};
return $Bin;
};
Avatar of mbasov
mbasov

ASKER

stouk,
how do you call a function?
Avatar of mbasov

ASKER

$CurrentPath = &CWD;

sub main::CWD
{
use FindBin qw($Bin);
# print "The script resides in the directory $Bin\n";
unless ($Bin =~ m!/$!) {$Bin .= "/"};
return $Bin;
};

open(SRC, main::CWD+"/mun.txt") || die ("Cannot find the file");
open(DAT, main::CWD()+"/>mun1.csv") || die("Cannot Open File");

it gives me an error on the line below
open(DAT, main::CWD()+"/>mun1.csv") || die("Cannot Open File");
$CurrentPath = &CWD;

my($ReadFile) = $CurrentPath  . "mun.txt";
my($WriteFile) = $CurrentPath  . "mun1.txt";
open(SRC, $ReadFile) || die ("Cannot find the file");
open(DAT, $WriteFile) || die("Cannot Open File");

Sorry forgot the redirection for reading/writing:

open(SRC, "< $ReadFile") || die ("Cannot find the file");
open(DAT, "> $WriteFile") || die("Cannot Open File");
ASKER CERTIFIED SOLUTION
Avatar of Sergy Stouk
Sergy Stouk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
No need for subroutines.  A much more readable format is:

use FindBin qw($Bin);

open SRC, "$Bin/mun.txt" or die "Can not open $Bin/mun.txt $!\n";
open DAT, ">$BIN/mun1.txt" or die "Can not open $Bin/mun1.txt $!\n";


Sorry.

The $BIN above should be $Bin.