current directory name in perl

D

dennishancy

Is there a function in perl that will tell me the directory name of the
currently running script?

In other words, I want to run the script c:\someDirName\scriptName.exe,
and I want to find a function that will return "c:\someDirName\".

I found tellDir and seekDir, but I am either using it the wrong way, or
it's not returning what I am hoping for.

Can someone shed some light on this for me?

Thanks.


Dennis Hancy
Eaton Corporation
Cleveland, OH
 
D

dennishancy

Ok, well I am getting closer.

I have the value "c:\someDirName\scriptName.pl" stored in a variable.
Now the rest should be easy (if I only knew my regular expression
syntax).

The ultimate goal is to store "c:\someDirName". So somehow I have to
chop off everything after the last \ character.

Can someone help me with the syntax? Thanks.


Dennis
 
D

Dr.Ruud

(e-mail address removed) schreef:
Ok, well I am getting closer.

I have the value "c:\someDirName\scriptName.pl" stored in a variable.
Now the rest should be easy (if I only knew my regular expression
syntax).

The ultimate goal is to store "c:\someDirName". So somehow I have to
chop off everything after the last \ character.

Can someone help me with the syntax? Thanks.

You need to read the Posting Guidelines of this group.
 
J

J. Fowler

Ok, well I am getting closer.

I have the value "c:\someDirName\scriptName.pl" stored in a variable.
Now the rest should be easy (if I only knew my regular expression
syntax).

getcwd => perldoc Cwd
 
J

Jürgen Exner

[Do not top-post/fullquote; trying to fix]
I have the value "c:\someDirName\scriptName.pl" stored in a variable.

Now what the heck are you asking for?
1: A function that returns the current working directory?
2: A function that returns the path for script?
3: The path part from an absolute file name?

Your 'description' could be asking for any of these.
1: perldoc CWD
2: perldoc FindBin
3: perldoc File::Basename

jue
 
D

dennishancy

Ok, here's the situation:

from the command line, I am typing:

c:\perl d:\someDirName\scriptName.pl



My goal is to figure out the path name where the script is located
(d:\someDirName)


Here is one solution using regular expressions:


my($dirNm, $fileNm) = $0 =~ m/^(.*\\)(.*)$/;


This will give me the full directory name in $dirNm. Open to
suggestions if there is another way.

--------------------------------------


Jürgen Exner said:
[Do not top-post/fullquote; trying to fix]
I have the value "c:\someDirName\scriptName.pl" stored in a variable.

Now what the heck are you asking for?
1: A function that returns the current working directory?
2: A function that returns the path for script?
3: The path part from an absolute file name?

Your 'description' could be asking for any of these.
1: perldoc CWD
2: perldoc FindBin
3: perldoc File::Basename

jue
 
D

Dr.Ruud

(e-mail address removed) schreef:


[don't top-post!]
My goal is to figure out the path name where the script is located
my($dirNm, $fileNm) = $0 =~ m/^(.*\\)(.*)$/;

That would fail with "perl D:../scriptName.pl".
 
J

Jürgen Exner

[Do you still instist on TOFU? Bad!]
Ok, here's the situation:

from the command line, I am typing:
c:\perl d:\someDirName\scriptName.pl

My goal is to figure out the path name where the script is located
(d:\someDirName)

As has been said before several times: The FindBin module will get you that
information
perldoc FindBin
Here is one solution using regular expressions:
my($dirNm, $fileNm) = $0 =~ m/^(.*\\)(.*)$/;
This will give me the full directory name in $dirNm. Open to
suggestions if there is another way.

But here you are trying to solve a different question: Given an absolute
file name, how to extract the path?
As has been said before several times: The File::Basename module will get
you that information.
perldoc File::Basename

Those two questions are not identical

jue
 
A

A. Sinan Unur

Ok, here's the situation:

from the command line, I am typing:

c:\perl d:\someDirName\scriptName.pl

Really? You have the perl binary installed in the root directory of the C
drive?
My goal is to figure out the path name where the script is located
(d:\someDirName)

perldoc FindBin
Here is one solution using regular expressions:


my($dirNm, $fileNm) = $0 =~ m/^(.*\\)(.*)$/;

perldoc File::Basename

Anyway, I am probably talking to myself, you are still top-posting, full-
quoting, and ignoring the obvious solutions given (I don't think GG has
killfile functionality), so here you go: *PLONK*

Sinan
 
T

Ted Zlatanov

Ok, here's the situation:

from the command line, I am typing:

c:\perl d:\someDirName\scriptName.pl



My goal is to figure out the path name where the script is located
(d:\someDirName)


Here is one solution using regular expressions:


my($dirNm, $fileNm) = $0 =~ m/^(.*\\)(.*)$/;


This will give me the full directory name in $dirNm. Open to
suggestions if there is another way.

I think you want to use File::Basename:

perldoc File::Basename

It's really simple, just use fileparse(), or basename() and dirname()
separately in the example above.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top