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

I found that with ActiveState Perl 5.10 on Windows, I can do a system("dir myfile"), but I can't do a system("type myfile"). Could someone kindly explain me why? Here the example code to demonstrate this problem:
# This is program omoshiroi.pl use strict; use warnings; sub systemx($) { my $cmd=shift; print "EXECUTING: $cmd\n"; system $cmd; } my $tmpfh=$ARGV[0]; systemx("dir $tmpfh"); systemx("type $tmpfh"); systemx("cmd /c type $tmpfh");
The only purpose of systemx is to show the command being executed. Now create some text file, say: abc.bat, and execute it like this:
omoshiroi.pl abc.bat
On my system, the output looks like this:
H:\tmp>omoshiroi1.pl abc.bat EXECUTING: dir abc.bat Datenträger in Laufwerk H: ist CIFS.HOMEDIR Datenträgernummer: 0000-0000 Verzeichnis von H:\tmp 15.05.2008 14:00 65 abc.bat 1 Datei(en) 65 Bytes 0 Verzeichnis(se), 1.239.298.048 Bytes frei EXECUTING: type abc.bat abc.bat not found EXECUTING: cmd /c type abc.bat pwd . $HOME .bash_profile echo $PATH sview fischron_exp_dv ls -a
As you can see, dir can find the file, cmd /c type can find the file, but type can't find it. Strange, isn't it?
-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Windoze oddity?
by BrowserUk (Patriarch) on May 15, 2008 at 15:49 UTC

    type also works provided you do something else first in the same command, or if you use a redirection:

    system "cd && type file"; ## or system "ver && type file"; ## or system "type file > CON";

    There is code internal to the system function that attempts to decide if it can get away with running the command you've supplied directly as an executable without invoking a copy of the shell. That is, if the command you ask it to run is an executable, then it runs that executable directly, but if it is a cmd.exe built-in command, then it runs cmd.exe with your input as the command line.

    One of the ways it make this determination is to scan the path and look to see if there is an executable with the name of the first token you enter. Ie. When you supply "type file", it goes looking through your path looking for "type.exe". If that isn't found, then it will invoke "cmd /c type file", which will probably work.

    But if like me you have Unxtools somewhere in your path, then it will find the ever useful and wonderfully named type.exe:

    C:\test>U:type.exe -h Usage: U:type.exe -v ==> version U:type.exe -h ==> usage U:type.exe -help ==> help U:type.exe file(s) ==> path to a file(s) C:\test>U:type.exe -help The C:\WINDOWS\System32\UnixTools\type.exe program will display the pa +th to a file if it is in the PATH environmental variable

    If you have the same tool in your path, rename it (or just delete it. the 'which' command is better named and more useful) and the problem will go away.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Windoze oddity?
by ikegami (Patriarch) on May 15, 2008 at 15:28 UTC
    I can't replicate your problem
    >c:\progs\perl588\bin\perl -e"system 'dir'" Volume in drive C is TRIBBLE05a Volume Serial Number is 28AB-0E8B Directory of C:\Documents and Settings\ikegami\test 2008/05/15 11:23 AM <DIR> . 2008/05/15 11:23 AM <DIR> .. 2008/05/15 11:23 AM 7 file 1 File(s) 7 bytes 2 Dir(s) 4,698,382,336 bytes free >c:\progs\perl588\bin\perl -e"system 'type file'" test >c:\progs\perl5100\bin\perl -e"system 'dir'" Volume in drive C is TRIBBLE05a Volume Serial Number is 28AB-0E8B Directory of C:\Documents and Settings\ikegami\test 2008/05/15 11:23 AM <DIR> . 2008/05/15 11:23 AM <DIR> .. 2008/05/15 11:23 AM 7 file 1 File(s) 7 bytes 2 Dir(s) 4,698,382,336 bytes free >c:\progs\perl5100\bin\perl -e"system 'type file'" test
    This is perl, v5.8.8 built for MSWin32-x86-multi-thread Binary build 817 [257965] provided by ActiveState http://www.ActiveSta +te.com This is perl, v5.10.0 built for MSWin32-x86-multi-thread Binary build 1001 [283495] provided by ActiveState http://www.ActiveSt +ate.com

    Update: I tried your program too. Worked fine. By the way, cmd is used for both type and dir even if you don't specify it explicitly since they're cmd builtins, not executables.