Perl drops first argv[0] parameter when being called via execv()

C

Clint O

Even though Perl offers the exec { path } @args call, I notice it drops thefirst parameter in @args: It's not captured in $0 nor is it available in @ARGV. Is this value available anywhere? I also checked $^X, but this usually is the interpreter at the top of the Perl script (#!/path/to/interpreter).

Thanks,

-Clint

% cat /tmp/foo
#!/home/utils/perl-5.10/5.10.1-nothreads-64/bin/perl

exec { "$ARGV[0]" } @ARGV[1..$#ARGV];

print "$!\n";

% cat /tmp/args.c
#include <stdio.h>

int main(int argc, char *argv[])
{
printf("I am %s\n", argv[0]);

return 0;
}

% cat /tmp/args.pl
% cat /tmp/args.pl
#!/home/utils/perl-5.10/5.10.1-nothreads-64/bin/perl

use strict;

use warnings;

print "I am $0 ", @ARGV ? @ARGV : "", "\n";

% /tmp/foo /tmp/args hoohah boohah
I am hoohah

% /tmp/foo /tmp/args.pl hoohah boohah
I am /tmp/args.pl boohah


So, in the Perl case, I’m missing “hoohah”…

-Clint
 
R

Randal L. Schwartz

Clint> % cat /tmp/foo
Clint> #!/home/utils/perl-5.10/5.10.1-nothreads-64/bin/perl

Clint> exec { "$ARGV[0]" } @ARGV[1..$#ARGV];

This is wrong. I think you want:

exec { $ARGV[0] } $0, @ARGV[1.$#ARGV];

That is, if you want:

foo.pl /some/other/prog aaa bbb ccc

to invoke /some/other/prog as if it was being invoked as

foo.pl aaa bbb ccc.

print "Just another Perl hacker,"; # "the original"
 
I

Ilya Zakharevich

Even though Perl offers the exec { path } @args call, I notice it drops the first parameter in @args: It's not captured in $0 nor is it available in @ARGV. Is this value available anywhere? I also checked $^X, but this usually is the interpreter at the top of the Perl script (#!/path/to/interpreter).

I noticed that your question is so badly formulated that people who do
not read your code carefully misinterpret it.

So the question is: suppose that

/bin/perl myscript.pl

is CALLED with C's argv[0] being "baz". The question is not about how
to implement this call in Perl. It is: How can myscript.pl find the
value of "baz"?

My conjectural answer: if it is not in $^X, tough luck...

Ilya
 
P

Peter J. Holzer

Even though Perl offers the exec { path } @args call, I notice it
drops the first parameter in @args: It's not captured in $0 nor is it
available in @ARGV. Is this value available anywhere? I also
checked $^X, but this usually is the interpreter at the top of the
Perl script (#!/path/to/interpreter).

I noticed that your question is so badly formulated that people who do
not read your code carefully misinterpret it.

So the question is: suppose that

/bin/perl myscript.pl

is CALLED with C's argv[0] being "baz". The question is not about how
to implement this call in Perl. It is: How can myscript.pl find the
value of "baz"?

I think this is impossible. If the C program calls

execl("myscript.pl", "baz", "arg1", "arg2", NULL);

the kernel will notice the shebang in "myscript.pl" and invoke

execl("/bin/perl", "myscript.pl", "arg1", "arg2", NULL);

instead (well, execve actually, but you get the point).

So the information that argv[0] was "baz" originally is lost and there
is no way to restore it.

If myscript.pl is invoked by a shell, you may find the original argv[0]
in the environment variable "_", but that's a convention of some shells,
not something enforced by the OS.

hp
 

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top