in reply to Re: Re: Portability question: Is there something like '#ifdef' in Perl?
in thread Portability question: Is there something like '#ifdef' in Perl?

If you use the scalar args form of system

... system( "command \\path\\with spaces\\file" ); ...

you'll may have problems with the spaces.

If you use the list form of the system function.

... system( '\path\to\command.exe', '\path\with spaces\file' ); ...

Note the use of 's rather than "s, to avoid the need to escape the backslashes.

cmd.exe will be bypassed and the spaces in the filename won't cause a problem. However, you will need to specify the full path of the command as well.

You can also avoid the need for using short filenames by quoting the filepath.

... system( q[command "\path\with spaces\file" ] ); ...

Which allows the shell to resolve the location of the command for you and forces it to see the quoted path as a single argument.

Note though, if your in an open environment, the risk entailed in allowing your shell to do this for you.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: Re: Re: Portability question: Is there something like '#ifdef' in Perl?
by sureshr (Beadle) on Sep 29, 2003 at 19:32 UTC
    1) Use of single quotes: This is not possible, as I am generating the path at runtime based on some env vars and I think use of single-quotes would not expand the variables inside it.

    2) I am not quite comfortable using 'command', because its like relying too much on a system file's existence (for example, lets say, MS decides to change the command.com's name, though very rare it maybe. I am very reluctant to use of such tied code, unless there is some pressing need). A function like 'system' is rather more guaranteed to not change over time or gets itself (functionality) modified in versions to come, if need be.

    Anyhow, thanks for the pointers!

    -sureshr

      You misunderstand. My use of the word "command" was simply as a placeholder for whatever command you are using system to invoke, ... NOT a reference to the old 16-bit windows shell, "command.com". Ie.

      system( "command \\path\\with spaces\\file" );

      could be

      system( "notepad \\path\\with spaces\\file" );

      or

      system( "xcopy \\path\\with spaces\\file" );

      The same applies to all other uses of the text 'command' in the original post.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

        Something like below, does not work either on WinXP/SP1 or Win2K/SP4. I tried it out from the command shell and perl is "This is perl, v5.6.0 built for MSWin32-x86"
        [C:/tmp] cat x.pl my $ret = system ("D:\\Program Files\\bin\\t.pl"); if ($? !=0 ) { print "ERROR: ret=[$?], err msg [$!]\n"; } [C:/tmp] ./x.pl 'D:\Program' is not recognized as an internal or external command, operable program or batch file. ERROR: ret=[256], err msg [No such file or directory]
        The file t.pl is just a line with 'print "hello world!\n";' and one of the soutions I see is to use the Win32::GetShortPathName function.
        -sureshr