John Kirk on Fri, 14 Apr 2000 19:23:53 -0400 (EDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: shebang pointing to script?


Hi Robert, and all,

On Fri, Apr 14, 2000 at 04:26:01PM -0400, Robert Spier wrote:
> You're expecting $0 to be equivalent to C's argv[0] -- but it's not.
> They are similar and have some of the same capabilities, but are different.
> (from perlvar.pod)   ...

  I think I got all that.  I believe the most immediate difficulty
for me is the behavior of the exec() function, when called the way
I was doing it.  Doesn't look to me as if it's doing what the camel
book said.  I guess you're right, though.  I do entertain the
expectation you describe.  I haven't seen precisely how they're
different, in any perl documentation.  Do you know where I should
look for more detailed specification of perl's implementation of
this?

  Some of my test cases just confirmed that things work the way
I would have expected, i.e. tst02 and tst03 behave the same;
similarly for tst05 and tst06.

  The key strangeness to me is that tst02 doesn't behave like
tst04 as evidenced especially in the first line of output, as
you've replicated in your cases below.  Once I got that working,
I'd want to make tst02 refer back to itself, rather than to tst01.
Then, I'd want to figure out how to get it all into the shebang
line, so no perl code would have to be after that, in tst02.
Surely there's a way.

  Unix provides the mechanism for this:  I'm trying to find out
how to access the capability in perl.

> --- t1.pl ---
> #!/usr/bin/perl
> print "I am t1.pl -- but \$0 is $0\n";

  This is just like my tst00, leaving off the copy of
the file contents.  It shows "... is tst04" if the
procedure I cited for tst04 is followed, i.e. making
tst04 a soft link to the script and invoking tst04 at
the command line.

> --- t2.pl ---
> #!/usr/bin/perl
> exec './t1.pl' '-george';

  This is just like my tst02, and I read the camel book
passage I cited as saying it should execute ./t1.pl, but
make it think it was invoked as -george instead.  This is
the behavior comparable to the soft link way of invoking
it, no?  (As in, accessing the same implementation behind
the scenes)

> --- t3.pl ---
> #!/usr/bin/perl
> exec './argv0' '-george';
> --- argv0.c ---
> #include <stdio.h>
> int main(int argc,char **argv) {
>   printf("argv[0] = %s\n",argv[0]);
> }

  This gives the behavior I expect.  Do we know exactly
what the differences are between $0 in perl and argv[0]
in C?  I had the impression that they were both accessing
the same info from the OS.

> ./t1.pl --> 
> I am t1.pl -- but $0 is ./t1.pl
> 
> ./t2.pl -->      
> I am t1.pl -- but $0 is ./t1.pl
> 
> ./t3.pl -->
> argv[0] = -george
> 
> So... I don't think you can do what you want with $0.
> It's also exec, which will actually replace the current process with
> the new one.  So it's actually starting up a completely new
> /usr/bin/perl process.

  That's what my empirical observations indicate.  My problem
is that I understood the documentation of perl and the
features provided by the implementation of *nix to indicate
that this kind of thing is available.

> Maybe what you want can be achived with C< do >?

  I don't know what you're suggesting, here.  Could you
elaborate on the idea.

       regards,  -- John Kirk
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**