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.
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.
ASKER
stouk,
how do you call a function?
how do you call a function?
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");
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");
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
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.
The $BIN above should be $Bin.
$CurrentPath = &CWD;
##########################
sub main::CWD
##########################
{
use FindBin qw($Bin);
# print "The script resides in the directory $Bin\n";
unless ($Bin =~ m!/$!) {$Bin .= "/"};
return $Bin;
};