Problems with while loop

J

John Dimper

Hi,

I'm having a problem with a while loop using ActiveState 5.8.7. Basically,
if I pipe from standard in and do the following:

while (<>) {
print "For Example";
}

it does nothing! If I open a file and while (<File>) this works without any
problems.... I've never come across this on Unix/Linux machines. Is this an
Activestate/Win32 problem or user error?

Many thanks,
John
 
A

A. Sinan Unur

I'm having a problem with a while loop using ActiveState 5.8.7.
Basically, if I pipe from standard in and do the following:

while (<>) {
print "For Example";
}

it does nothing! If I open a file and while (<File>) this works
without any problems.... I've never come across this on Unix/Linux
machines. Is this an Activestate/Win32 problem or user error?

See if invoking the script as:

someprog | perl script.pl

solves the problem. If so, I have encountered this before. It seems to
be a Win32 issue.

Sinan
 
J

John Dimper

This is untrue. Strike your ENTER key a couple of times
within your command prompt after invoking your script.

While not sending input, your script will sit there doing nothing,
save for waiting for input.

Appears this is not a Perl issue but rather an operator issue. =)

Thanks for you responses... Sinans someprog | perl script.pl did the trick.
For your info it isn't a problem waiting for input - the same happens if I
try C:\type afile.txt | perlscript.pl. Anyway - all sorted now.

Thanks again,
John
 
J

John Dimper

Previously I wrote,
"Appears this is not a Perl issue but rather an operator issue."

That is true.

Purl Gurl

You've misunderstood.

When I try C:\type afile.txt | someperlscript.pl on win32 the (<>) while
loop doesn't run. Perl *has* been invoked since code in front of the while
loop runs correctly. In order to run it properly I now realize I need to
type c:\type afile.txt | perl someperlscript.pl

My point was that this is not the case on any Unix shell I've ever used. If
I type ./cat afile.txt | someperlscript.pl the same script will run through
without any problems.

John
 
S

Samwyse

Purl said:
while (<>)

What you write is untrue. That syntax waits for STDIN input.

What you write is untrue. Input from <> comes either from standard
input, or from each file listed on the command line.

Apparently, this isn't a Perl issue, but a Purl Gurl issue.
 
A

A. Sinan Unur

John Dimper:


Alternatives:

perlscript.pl < \path\afile.txt

But that, precisely, is the problem on Windows:

D:\Home\asu1\UseNet\clpmisc> cat stupid.pl
#!/usr/bin/perl

use strict;
use warnings;

print while <>;

D:\Home\asu1\UseNet\clpmisc> cat test.txt
test 1
test 2
test 3

D:\Home\asu1\UseNet\clpmisc> stupid.pl < test.txt

D:\Home\asu1\UseNet\clpmisc> perl stupid.pl < test.txt
test 1
test 2
test 3

D:\Home\asu1\UseNet\clpmisc> stupid.pl test.txt
test 1
test 2
test 3

Sinan
 

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

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top