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.
mbasovAsked:
Who is Participating?
 
sstoukCommented:
So, your script would look like:

$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");

# Do What You Need with the files here


exit;

sub main::CWD
{
use FindBin qw($Bin);
# print "The script resides in the directory $Bin\n";
unless ($Bin =~ m!/$!) {$Bin .= "/"};
return $Bin;
};
0
 
sstoukCommented:
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;
};
0
 
mbasovAuthor Commented:
stouk,
how do you call a function?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mbasovAuthor Commented:
$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");
0
 
sstoukCommented:
$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");
0
 
sstoukCommented:

Sorry forgot the redirection for reading/writing:

open(SRC, "< $ReadFile") || die ("Cannot find the file");
open(DAT, "> $WriteFile") || die("Cannot Open File");
0
 
TintinCommented:
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";


0
 
TintinCommented:
Sorry.

The $BIN above should be $Bin.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.