Perl Executable and Script Name -- NOT Args

K

Kevin Mooney

Walt said:
I'm stumbling trying to find out if this is even possible with Perl.
Anyone know?

In a C program, it's possible to get the executable's name as argv[0]
-- a trick that's useful when you want one executable and a bunch of
symbolic links, but the program's behavior to change based on what it
was called. You'll note some crypto packages do this, as it is
simplier to have multiple man pages.

Since the Perl engine is the executable in reality, I figure the best
I'd get is the fully qualified path name of the running executable --
which might be useful if I have more than one version of Perl
installed.

Additionally, if I wanted a program to know about it's own source
file, it might be interesting to see what was passed to Perl. This is
_different_ than command line arguments being passed to a Perl script.
I'd like to know if the Perl engine itself exposes what was passed to
it.

Anyone know if there are some magic globals or environment variables
which contain either of these two pieces of information?

-Walt Stoneburner, (e-mail address removed)

For your first question, I believe $0 is what you are looking for.
Unfortunately I have no idea as far as your second question goes.
 
W

Walt Stoneburner

I'm stumbling trying to find out if this is even possible with Perl.
Anyone know?

In a C program, it's possible to get the executable's name as argv[0]
-- a trick that's useful when you want one executable and a bunch of
symbolic links, but the program's behavior to change based on what it
was called. You'll note some crypto packages do this, as it is
simplier to have multiple man pages.

Since the Perl engine is the executable in reality, I figure the best
I'd get is the fully qualified path name of the running executable --
which might be useful if I have more than one version of Perl
installed.

Additionally, if I wanted a program to know about it's own source
file, it might be interesting to see what was passed to Perl. This is
_different_ than command line arguments being passed to a Perl script.
I'd like to know if the Perl engine itself exposes what was passed to
it.

Anyone know if there are some magic globals or environment variables
which contain either of these two pieces of information?

-Walt Stoneburner, (e-mail address removed)
 
P

Paul Lalli

Walt Stoneburner said:
In a C program, it's possible to get the executable's name as argv[0]
-- a trick that's useful when you want one executable and a bunch of
symbolic links, but the program's behavior to change based on what it
was called. You'll note some crypto packages do this, as it is
simplier to have multiple man pages.

Since the Perl engine is the executable in reality, I figure the best
I'd get is the fully qualified path name of the running executable --
which might be useful if I have more than one version of Perl
installed.

The currently running executable is stored in $^X. perldoc perlvar
warns you that depending on your OS, this may be a relative or absolute
path.
Additionally, if I wanted a program to know about it's own source
file, it might be interesting to see what was passed to Perl. This is
_different_ than command line arguments being passed to a Perl script.
I'd like to know if the Perl engine itself exposes what was passed to
it.

The currently executing perl script is stored in $0
Anyone know if there are some magic globals or environment variables
which contain either of these two pieces of information?

perldoc perlvar is the place to go to find a list of all global Perl
variables.

Paul Lalli
 
C

Christopher Nehren

perldoc perlvar is the place to go to find a list of all global Perl
variables.

Note that it covers more than just _global_ variables; it also documents
package variables such as $a and $b, and the dynamically-scoped regular
expression match variables -- among others.
 
P

Paul Lalli

Christopher Nehren said:
Note that it covers more than just _global_ variables; it also documents
package variables such as $a and $b, and the dynamically-scoped regular
expression match variables -- among others.

True. That was a poor choice of words on my part. "built-in Perl
variables" may be better. Basically, all the variables that exist by
default without the programmer creating them.

Paul Lalli
 
B

Ben Morrow

Quoth "Paul Lalli said:
The currently executing perl script is stored in $0

....but it may (under some OSen) be just a name, with no path. The
FindBin module is supposed to solve this problem for you, though I have
found it a little unreliable in the past; I believe some work has been
done on it recently, though.

Ben
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top