?
Solved

Proper Path

Posted on 2005-05-17
8
Medium Priority
?
189 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:mbasov
  • 4
  • 2
  • 2
8 Comments
 
LVL 6

Expert Comment

by:sstouk
ID: 14019240
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
 

Author Comment

by:mbasov
ID: 14019338
stouk,
how do you call a function?
0
 

Author Comment

by:mbasov
ID: 14019430
$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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:sstouk
ID: 14022409
$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
 
LVL 6

Expert Comment

by:sstouk
ID: 14022433

Sorry forgot the redirection for reading/writing:

open(SRC, "< $ReadFile") || die ("Cannot find the file");
open(DAT, "> $WriteFile") || die("Cannot Open File");
0
 
LVL 6

Accepted Solution

by:
sstouk earned 2000 total points
ID: 14022441
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
 
LVL 48

Expert Comment

by:Tintin
ID: 14023379
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
 
LVL 48

Expert Comment

by:Tintin
ID: 14023381
Sorry.

The $BIN above should be $Bin.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question