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

I'm trying to start another program from my script using exec() and a variable which contains the PATHNAME to the program to run.

I'm on a Windows OS. I've tried untainting the string as outlined in the Camel book, p.562, with a regex. I've tried all combinations of arguments, quotes, no quotes- nothing seems to work. The best I've done is:

exec {$args[0]} @args
which produces the message: Cant exec PATHNAME: Permisson denied.

It will work if I hard code the PATHNAME after exec(),e.g.

exec "C:\\Eudora\\eudora.exe"
But if $string = "C:\\Eudora\\ eudora.exe", then exec $string or exec "$string" won't work.

Win32::Process won't work either passing $string in as the command. The reason I'm doing it this way is because the PATHNAME stored in $string has to change depending on choice of program to run.

update (broquaint): much formatting added and a 'then' in the second to last paragraph

Replies are listed 'Best First'.
Re: Using a variable for PATHNAME in exec()
by pg (Canon) on Nov 16, 2002 at 20:30 UTC
    Better post your code, but put the command you want in a qw, and try something like:
    exec qw(perl -w "c:\\examples\\perl\\a.pl");
Re: Using a variable for PATHNAME in exec()
by AcidHawk (Vicar) on Nov 18, 2002 at 07:45 UTC

    I have this working with both Win32::Process and backticks.. my code looks a lilttle like this...

    Win32::Process::Create($ProcObj, "$xcall_path/bin/$prog", "$prog \"$type\" \"$file\" \"$call_num\"", 0, # Don't inherit. NORMAL_PRIORITY_CLASS, ".") or &Update_Log("DEBUG - Cannot Launch the Service Desk Pr +ocess process in the Log Call Routine: $!\n$prog\"$type\" \"$file\" \ +"$call_num\"\n"); if ($ProcObj->Wait (180*1000)){ # execution of the process is successfully. $ProcObj->Kill(0); } else { # process has hung up for some reason $ProcObj->Kill(255); $rc = 0; }
    to get the path (ie $xcall_path I had to do something like so..
    my $path = "$ENV{'MYSYSENV'}\\X-CallLogger"; unless ($xcall_path = Win32::GetShortPathName($path)) { &Event(EVENTLOG_ERROR_TYPE, "$0 - Path Problems in Setup for $ +path: $^E\n"); return(0); } $xcall_path =~s /\\/\//g; #Substitute backslashes with +frontslashes $prog = $ARGV[0];
    As I said I have had no problems with this. Perhaps look at the substitues of \'s with /'s

    -----
    Of all the things I've lost in my life, its my mind I miss the most.