Perl on Windows question

P

pwsteele

I have ActiveState perl installed on my PC and have discovered an odd
behavior when executing a "type" command, e.g.,

system "type c:\\somefile";

This comes back with the error

c:\somefile not found

However, when I use this form

system "cmd /c type c:\\somefile";

it works as expected. I've only seen problem happen with the type
command. Other built in commands appear to work properly. Other users
in the comany do not see this error either, even with the type command.
What would be causing this in my environment?
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
I have ActiveState perl installed on my PC and have discovered an odd
behavior when executing a "type" command, e.g.,

system "type c:\\somefile";

This comes back with the error

c:\somefile not found

Well, the crucial question is whether c:\somefile exists, right?

On the other hand, the error message above does not seem to be from the cmd
built-in. Do you, maybe, have cygwin installed, with the command line
utility type installed in your path?
What would be causing this in my environment?

Great question. Only you can know as we know nothing about your
environment.

Sinan
 
J

jl_post

I have ActiveState perl installed on my PC and have discovered an odd
behavior when executing a "type" command, e.g.,

system "type c:\\somefile";

This comes back with the error

c:\somefile not found

However, when I use this form

system "cmd /c type c:\\somefile";

it works as expected. I've only seen problem happen with the type
command. Other built in commands appear to work properly. Other users
in the comany do not see this error either, even with the type command.
What would be causing this in my environment?


You know, I've seen this error, too. For example, if I have a file
named "file.txt" in my current directory and I'm writing a Perl
one-liner (and I'm too lazy to use the open() function), I'll sometimes
use backquotes to read the text into a variable.

For example, this Perl one-liner fails:

perl -e "print `type theory.txt`"

The error message it gives is:

file.txt not found

But then, as you suggested, I try:

perl -e "print `cmd /c type file.txt`"

then everything works correctly.

Unfortunately, I don't know why in the world the DOS "type" command
is behaving this way. In case anyone is interested, my "perl -v"
output is:


This is perl, v5.8.2 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)
Copyright 1987-2003, Larry Wall
Binary build 808 provided by ActiveState Corp.
http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Dec 9 2003 10:19:40


I don't think any of this information helps you at all, other than
letting you know that you aren't the only one who has experienced this
strange behavior. But if anyone figures out why it's behaving this
way, please let us know!

Thanks.

-- Jean-Luc
 
A

A. Sinan Unur

Unfortunately, I don't know why in the world the DOS "type" command
is behaving this way.

Are sure it is the cmd built-in 'type' that you are calling?

Sinan
 
P

pwsteele

These replies gave me the clue I needed. No, I don't have cygwin
installed, but I do have a bin directory with a bunch of Unix-style
commands, including type.exe. When I run from a command line, the
command shell recognizes type as a built-in command first before
looking in the search path. When running from Perl though, if looks at
the search path first. So, I just got rid of the type.exe command since
I don't need it.

Thanks for the help.

Peter
 
J

jl_post

A. Sinan Unur said:
Are sure it is the cmd built-in 'type' that you are calling?


Wow. I think you just helped me solve my problem, Sinan.

I tried "which type.exe" (I have "which" on Win32 because I
installed a set of Unix Utilities) and discovered that the "type"
program that Perl was using in backticks was the Unix-ish "type"
program, not the DOS built-in "type" command.

I temporarily renamed the "type.exe" Unix-ish program and tried
running the Perl one-liner:

perl -e "print `type file.txt`"

and then it worked! Then I restored the Unix-ish "type.exe" program
and tried the two following commands at the DOS prompt:
type file.txt
full\path\to\Unix\Utils\type file.txt

and discovered that the first command worked like the Unix "cat"
command, but the second line returned the following error:

file.txt not found

which was the same error message the original poster was getting in the
first place.

So, apparently, when I use "type" from the DOS command line, it uses
the built-in DOS command, but if I use it from a Perl backtick (or
system()) call, it uses the one in the Unix Utilities (which is in my
%PATH%).

The work-around to this problem is to replace "type" with "cat"
(which I should have since I installed the Unix Utilities). Of course,
this makes my Perl one-liners less portable across Win32 systems -- but
then, it's not a difficult thing to change, considering that Perl
one-liners are only one line of code (or so).

Anyway, thank you for helping me find the problem, Sinan. I hope my
explanation was clear enough to understand.

-- Jean-Luc
 
A

A. Sinan Unur

....

Anyway, thank you for helping me find the problem, Sinan. I hope my
explanation was clear enough to understand.

You are welcome. I was almost certain the issue was a confusion between the
cmd.exe builtin versus an executable in the path, but needed more
information.

Sinan
 
S

Sisyphus

These replies gave me the clue I needed. No, I don't have cygwin
installed, but I do have a bin directory with a bunch of Unix-style
commands, including type.exe. When I run from a command line, the
command shell recognizes type as a built-in command first before
looking in the search path. When running from Perl though, if looks at
the search path first. So, I just got rid of the type.exe command since
I don't need it.

You could also get the desired result with:
system "type c:\\somefile <NUL";

Any redirects will ensure that the shell built-ins are searched first.

Cheers,
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top