develooper Front page | perl.beginners | Postings from January 2002

system "dir"; works fine, but system "dir foo.txt" gets an error

Thread Next
From:
Dave Benware
Date:
January 31, 2002 08:35
Subject:
system "dir"; works fine, but system "dir foo.txt" gets an error
Message ID:
3C597245.D55CF50@Townisp.com
Running the script (below) from Bob Showalter brought a problem to
surface on this win98se using ActivePerl 560 build 630.

The line system "dir foo.txt"; returned "Bad command or file name". 
 
foo.txt does exist and I can run dir foo.txt within a dos box just 
fine.  Interestingly to me, system "dir"; worked okay.  I guess it 
just won't take an argument.

So, have I installed Perl improperly?

Bompa

















########################################################
> Excuse me, I *do* see the "poop" you refered to in the binmode
> function docs, however, it doesn't seem to be that way in reality.

A challenge, eh! OK, run the following program on a Windows machine:

   # create a string
   $s = "foo\n";
   print "length is ", length($s), "\n";

   # write it to a file
   open F, ">foo.txt" or die $!;
   print F $s;
   close F;

   # see size of file
   system "dir foo.txt";

   # read back from file
   open F, "<foo.txt" or die $!;
   $s = <F>;
   close F;
   print "length is ", length($s), "\n";

   # read from file in binary mode
   open F, "<foo.txt" or die $!;
   binmode F;
   $s = <F>;
   close F;
   print "length is ", length($s), "\n";

When I run it on my Win98 PC, I get the following output:

   length is 4

    Volume in drive C has no label
    Volume Serial Number is 07D1-031B
    Directory of C:\Temp

   FOO      TXT             5  01-31-02  9:00a foo.txt
            1 file(s)              5 bytes
            0 dir(s)        7,104.59 MB free
   length is 4
   length is 5

So you can see that the string is 4 bytes long. When I write it to the file,
the file becomes 5 bytes, because the LF is converted to a CR/LF pair. When
I read it back in, the reverse conversion is made, so the string becomes 4
bytes again. When I call binmode, that conversion is not made, so the 5
bytes
from the file are read in.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About