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

Hi fellows:

Please show me what's wrong at this code:

$comando = "dir c:\\temp"; @result = `$comando`; foreach $line (@result) { print "Linha.....: $line \n"; }
Running it under WinNT 4.0 SP6 the "print command" shows the results successful. However, under Win95 the command does not show the results. Can You help me?

Thanks in Advance.

Eustaquio - jeustaqi@net.em.com.br
System Engineer - BMP - Brazil.

Edit Masem 2001-07-16 - CODE and formatting fixed

Replies are listed 'Best First'.
(jeffa) Re: Back ticks Win95 vs. WinNT
by jeffa (Bishop) on Jul 16, 2001 at 18:16 UTC
    Hmmm - this problem is indeed baffling, i recommend a different approach - IO::Pipe:
    #!/usr/bin/perl -w use strict; use IO::Pipe; my $pipe = IO::Pipe->new(); $pipe->reader('dir c:\\temp'); my $line; while ($line = <$pipe>) { print "Linha.....: $line \n"; }
    Anytime i need a 'portable' script, i like to use a framework that encapsulates the offending piece of code - such as opening a pipe to a system process.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Back ticks Win95 vs. WinNT
by mexnix (Pilgrim) on Jul 16, 2001 at 19:02 UTC
    I wonder if you should be using globbing hear instead. i.e.

    #!perl -w use strict; my $dir = "c:\\temp\\"; my @results = <$dir*>; foreach (@results) { print "Linha.......: $_ \n"; }

    Just another option! :)

    __________________________________________________
    <moviequote name="The Whole Nine Yards">
    Jimmy T: Oz, we're friends, friends do not engage in sexual congress with each others wives.
    </moviequote>

    %mexnix = (email = > "mexnix@hotmail.com", website => "http://mexnix.perlmonk.org");

Re: Back ticks Win95 vs. WinNT
by Mungbeans (Pilgrim) on Jul 16, 2001 at 19:41 UTC
    You don't need to escape the directory with \ if you use single 's. That turns off the " interpolation which can be annoying.

    Win95 and NT dir output sound as though they behave differently. It may be more portable with opendir and readdir - try and avoid the dir command or other system calls on Win95 - not portable.

    Code adapted from perldoc -f readdir...

    my $some_dir = 'c:/temp'; opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; my @dots = readdir(DIR); closedir DIR; print "Linha: $_\n" for @dots;
    Note that opendir doesn't care about OS specific directory separators.

    "The future will be better tomorrow."