dbmears has asked for the wisdom of the Perl Monks concerning the following question:

I've found (barely) enough documentation to be able to use perl_parse from a C program (along with all the other necessary "embed" code), but nothing to give great details about what happens between the call to perl_parse and the Perl code accessing a value for $0.

Without going into too much detail (yet) -- briefly -- I'm looking at being able to have a Perl script which can be run in one of two ways: directly, or embedded from another C program. In the C program, I'm using the normal embedding calls and calling perl_parse with the name of the perl script and properly shifted argc/argv values passed to the C program. As expected by default, $0 is being set to the leaf name of the script loaded by perl_parse.

I'm wondering if there is any way to have $0 be set to some other value than the perl script file name itself under the condition that it is parsed/loaded by my C program. (I.e. I want $0 to have a different value when loaded from the embedding code than it has when run directly.) Oh, and BTW, the value of $0 just happens to be used in BEGIN blocks so it needs to be set during the compile phase, not after the perl_parse call returns (i.e. so I can't just set it using sv_setpv_mg (or something similar) later on.

I suppose it's entirely possible that perl_parse allows no way to have a different value set for $0, but I thought I'd better ask. And see if anyone knows of other documentation with such details than what searches at many search engines have been able to find.

Thanks!

Replies are listed 'Best First'.
Re: using perl_parse and setting $0
by cdarke (Prior) on Apr 20, 2006 at 09:42 UTC
    I notice the lack of replies. Here is a hack I have used in the past, assuming you are on *nix. Setup a symbolic link to the script, use the 'real' name in one instance, and the link name in the other.
    I appreciate it doesn't answer your question about altering $0!
A reply falls below the community's threshold of quality. You may see it by logging in.