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

This seems simple, but I can't figure it out.
Why do @dirA and @dirB contain different elements in the code below? @dirA is just a list, it doesn't contain date, time and size like @dirB does.
This is on XP, ActiveState Perl 5.8.7
Thanks for any help.

use strict; my @dirA; @dirA = `dir \\\\rancaajelloxp\\c\$`; print @dirA; print "\n\n"; my @dirB; `dir \\\\rancaajelloxp\\c\$ > dir.txt`; open DIRB, 'dir.txt'; for (<DIRB>){ push @dirB, $_; } close DIRB; print @dirB;

Replies are listed 'Best First'.
Re: differences in dir? (cmd)
by tye (Sage) on May 02, 2007 at 16:33 UTC

    My guess is that 'dir' is a program in your path somewhere and the first instance is able to run 'dir' directly while the second instance, having ">" in the command string, goes directly to passing the command to cmd.exe, which uses its built-in "dir" command.

    Note that if you didn't have a 'dir' program in your $ENV{PATH}, then the first instance would try to run 'dir' directly, fail, and then resort to passing the command to cmd.exe, giving the same output as your second version.

    - tye        

Re: differences in dir?
by BrowserUk (Patriarch) on May 02, 2007 at 16:31 UTC

    Because the second command creates (or modifies) a file within the current directory. Ie. dir.txt.

    BTW. Depending what you want to do with it, this is probably a really bad way to get a list of files.


    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.
      @dirA is just a list, it doesn't contain date, time and size like @dirB does.

      I think perhaps you didn't read a key part of the problem description.

        Hmm. I tried it (on XP), and I get exactly the same output, including data and time, in the file as I get to the screen:

        [0] Perl> `dir >junk.txt`;; [0] Perl> @d2 = do{ local @ARGV = 'junk.txt'; <> };; [0] Perl> print for @d2[ 0 .. 10 ];; Volume in drive C has no label. Volume Serial Number is BCCA-B4CC Directory of c:\test 2007-05-02 16:31 <DIR> . 2007-05-02 16:31 <DIR> .. 2005-06-24 13:45 721 12balls.pl 2005-11-12 16:16 1,142 12days-enc.pl 2005-11-13 02:26 1,688 12days.pl 2004-07-28 00:37 124,592,128 1Mx4096.db

        As far as I am aware, the only way to get a raw list of files without the data and time is to use the /b switch on the dir command:

        [0] Perl> `dir /b >junk.txt`;; [0] Perl> @d2 = do{ local @ARGV = 'junk.txt'; <> };; [0] Perl> print for @d2[ 0 .. 10 ];; 12balls.pl 12days-enc.pl 12days.pl 1Mx4096.db 2.log 200083.pl 216398.pl 250802.pl 275606.pl 27x2.wav 282393.pl

        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: differences in dir?
by akho (Hermit) on May 02, 2007 at 16:08 UTC
    This is just a wild guess (ain't got no windows), but it is possible dir has a different output when redirected to a file, like ls does on Linux.