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

I found that on Windows (using ActiveState Perl 5.10) the command

perl -e "system('type myfile.txt')"
result in a "File not found" message, while
perl -e "system('cmd /c type myfile.txt')" perl -e "system('cat myfile.txt')"
- the latter being the Windows port of the Gnu utilities - work as expected.

My first explanation was that cmd.exe is bypassed in the first case, and since type is an internal command, it can't be executed that way. This explanation, however, can't be correct, because Windows doesn't complain about 'type' not being found, it complains about myfile.txt. Also,

perl -e "system('dir ...')"
works fine, although dir is an internal command as well. Does anybody know why type is so special? I'm asking this out of curiosity, because in a real application, I would not shell out to type a file, but do it directly from Perl.

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Windows: system("type ...") vs. system("cmd /c type ...")
by syphilis (Archbishop) on Nov 17, 2008 at 11:50 UTC
    I reckon I've been bitten by this before, though it was a while ago, and I'm a little hazy on the details. But, if I've got it right, there's a 'type.exe' in your path that's being invoked (instead of the shell's 'type' command). To make sure you get the shell's 'type' command I think you can also run something like:
    perl -e "system('type myfile.txt') <NUL"
    Does that produce expected results ?

    Cheers,
    Rob
    Update: The above command might not be quite correct. In order to invoke the shell's 'type' command, I think it should have been written as:
    perl -e "system('type myfile.txt <NUL')"
    Of course, on most systems (such as mine), it won't matter - as the shell's 'type' command gets invoked, anyway.
      if I've got it right, there's a 'type.exe' in your path that's being invoked

      Of course, that's it!!!! Ahhh, stupid me, I've stumbled over this already a few months ago, and completely forgotten!!!!!!! Arrrrrrgh, did the same mistake twice! Thanks for helping!

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Windows: system("type ...") vs. system("cmd /c type ...")
by BrowserUk (Patriarch) on Nov 17, 2008 at 11:56 UTC

    Which perl are you using? Because I cannot reproduce this with AS1002.

    Best guess: you are using cygwin which uses (c|z|ba|other)sh as the shell, but provides an executable or alias for dir?

    c:\test>\Perl510\bin\perl.exe -v This is perl, v5.10.0 built for MSWin32-x86-multi-thread (with 3 registered patches, see perl -V for more detail) Copyright 1987-2007, Larry Wall Binary build 1002 [283697] provided by ActiveState http://www.ActiveSt +ate.com Built Jan 10 2008 11:00:53 [SNIP} c:\test>\Perl510\bin\perl.exe -wle"print $]; system q[type junk.pl]";; 5.010000 use threads; use strict; use warnings; print $], ' ', $threads::VERSION, "\n"; my $sub1 = sub { my $a=[ 1 .. 10000] }; my $m = 0; for (;;) { printf "\r%d\t", $m++; my $thr = threads->new( $sub1 )->join; undef $thr; } __END__ c:\test>

    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: Windows: system("type ...") vs. system("cmd /c type ...")
by massa (Hermit) on Nov 17, 2008 at 11:40 UTC
    I would bet on system, alone, not taking you to the directory where myfile.txt is...
    []s, HTH, Massa (κς,πμ,πλ)

      Hmmm... I *am* already in that directory, so no need to cd somewhere.

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Windows: system("type ...") vs. system("cmd /c type ...")
by xiaoyafeng (Deacon) on Nov 18, 2008 at 07:36 UTC
    Try this:
    perl -e "system(q(type myfile.txt))"

    You could visit here to learn more.

    UPDATE:

    perldoc -f system
    is also helpful.
    Sorry, I made a mistake.

    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

      Are you saying that perl -e "system(q(type myfile.txt))"

      is somehow different to perl -e "system('type myfile.txt')"?

      I tried following your link to see if it clarified things, but it seemed to just be an essentially random page on the AS website with nothing that clarified what point you were trying to make?


      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.