Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Poll: Is your $^X an absolute path? (argv[0])

by tye (Sage)
on Aug 18, 2006 at 05:13 UTC ( [id://568075]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Poll: Is your $^X an absolute path? (argv[0])
in thread Poll: Is your $^X an absolute path?

Win32 Perl has always had GetModulePathName()
At least my cursory grep didn't find that function in the perl source

Sorry, my transcription error (s/Path/File/):

perllib.c:171: GetModuleFileName(NULL, szModuleName, sizeof(szModul +eName)); win32.c:135: GetModuleFileName((HMODULE)((w32_perldll_handle == INV +ALID_HANDLE_VALUE) win32.c:136: ? GetModuleHandle(NULL)
It looks like it's just getting the name from whatever the compiler sets up as argv[0].

No, Win32 Perl has never figured out $^X based on argv[0] (except before it was released). The function called is os2_execna­me(aTHX) (yes, Win32 is consider OS/2-ish).

- tye        

Replies are listed 'Best First'.
Re^4: Poll: Is your $^X an absolute path? (argv[0])
by xdg (Monsignor) on Aug 18, 2006 at 11:57 UTC
    No, Win32 Perl has never figured out $^X based on argv[0]

    I freely admit to being very new to reading the Perl source (what's that line about 'a maze of twisty passages'?) but what about this excerpt from perllib.c around the line you quoted (for Perl 5.8.8 anyway):

    EXTERN_C DllExport int RunPerl(int argc, char **argv, char **env) { int exitstatus; PerlInterpreter *my_perl, *new_perl = NULL; #ifndef __BORLANDC__ /* XXX this _may_ be a problem on some compilers (e.g. Borland) th +at * want to free() argv after main() returns. As luck would have i +t, * Borland's CRT does the right thing to argv[0] already. */ char szModuleName[MAX_PATH]; GetModuleFileName(NULL, szModuleName, sizeof(szModuleName)); (void)win32_longpath(szModuleName); argv[0] = szModuleName; #endif

    It looks like win32 Perl fixes up argv[0] itself -- and that this is what is used later by S_set_caret_X (a new function as of 5.8.7).

    However, that perllib.c line is a great find for Vanilla Perl -- it means we should be able to use relocatable perl pretty easily in most circumstances. (I'm not going to worry about mod_perl on win32 at first.)

    The downside is that the expansion uses the full path -- including spaces if installed somewhere like "C:\Program Files\Perl". That will still be problematic for people who call system("$^X program.pl arg1 arg2 etc") as the shell is going to have problems as the program name is not quoted. Of course -- that's not really a portable call anyway, so maybe we tackle that problem with documentation.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        I think the safe thing is to always surround the first argument with double-quotes if more than one argument is provided and it doesn't already seem to have double quotes. (If only one argument is provided, we have to assume the user provided a valid command line on the current platform.) So, ignoring error reporting, something like this:

        sub system { $_[0] = qq{"$_[0]"} if @_ > 1 && substr($_[0],0,1) ne q{"}; CORE::system( @_ ); }

        On reflection, that seems simple enough that maybe we can get some p5p person to patch it.

        Some references about how spaces are handled in program names:

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://568075]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-28 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found