For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > strange problem with STDIN- need help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author strange problem with STDIN- need help
Poonam Pahil

2006-01-10, 4:01 am

Hi,

Iam using <STDIN> to accept user input but the script just hangs at that
point.
I type in the value & program is just stuck at that point.

i know its got to do with buffer flushing. when iam using the same piece of
code at the beginning of the script everything works fine.

iam using system() a lot. directing input/output to/from files. can this be
the reason.

This is the piece of code:

print "Do you want to remove this CSpec(y/n):";
my $remove_CS = <STDIN>;
chop($remove_CS);
if($remove_CS =~ m/n/i )

Thanks & Regards
Poonam

Owen Cook

2006-01-10, 4:01 am


On Thu, 29 Dec 2005, Poonam Pahil wrote:
>
> Iam using <STDIN> to accept user input but the script just hangs at that
> point.
> I type in the value & program is just stuck at that point.
>
> i know its got to do with buffer flushing. when iam using the same piece of
> code at the beginning of the script everything works fine.
>
> iam using system() a lot. directing input/output to/from files. can this be
> the reason.
>
> This is the piece of code:
>
> print "Do you want to remove this CSpec(y/n):";
> my $remove_CS = <STDIN>;
> chop($remove_CS);
> if($remove_CS =~ m/n/i )



What happens after the if statement

if($remove_CS =~ m/n/i ){ ??? }


Owen

Poonam Pahil

2006-01-10, 4:01 am

if($remove_CS =~ m/n/i ){ ??? }
it does not reach to this point.

it hangs before this, at
my $remove_CS = <STDIN>;


thanks
poonam

On 12/29/05, Owen Cook <rcook@pcug.org.au> wrote:
>
>
> On Thu, 29 Dec 2005, Poonam Pahil wrote:
> of
> be
>
>
> What happens after the if statement
>
> if($remove_CS =~ m/n/i ){ ??? }
>
>
> Owen
>
>


Paul Lalli

2006-01-10, 4:01 am

Poonam Pahil wrote:
> Iam using <STDIN> to accept user input but the script just hangs at that
> point.
> I type in the value & program is just stuck at that point.
>
> i know its got to do with buffer flushing. when iam using the same piece of
> code at the beginning of the script everything works fine.
>
> iam using system() a lot. directing input/output to/from files. can this be
> the reason.


No.

Search your code for the $/ variable. I'm betting you changed it at
some point, without localizing the changes. Whereever you changed it,
enclose it's change and the subsequent read(s) in a block, and put the
word 'local' before the change:

{
local $/ = "\n\n";
my $paragraph = <STDIN>;
}

Read about what the $/ variable does in:
perldoc perlvar


> This is the piece of code:
>
> print "Do you want to remove this CSpec(y/n):";
> my $remove_CS = <STDIN>;
> chop($remove_CS);
> if($remove_CS =~ m/n/i )


Try running that piece of code on it's own, and you will see that
nothing there is the problem. The problem lies elsewhere in your code.

Paul Lalli

Owen Cook

2006-01-10, 4:01 am


On Thu, 29 Dec 2005, Poonam Pahil wrote:

> if($remove_CS =~ m/n/i ){ ??? }
> it does not reach to this point.
>
> it hangs before this, at
> my $remove_CS = <STDIN>;
>
>



Well this works for me
------------------------cut----------------------------
#!/usr/bin/perl -w

use strict;

print "Do you want to remove this CSpec(y/n):";
my $remove_CS = <STDIN>;
chop($remove_CS);
if($remove_CS =~ m/n/i ){print "\n\n OK - will not remove\n"} else {print
"\n\n OK - will remove\n"}

------------------------cut----------------------------

owen@ubuntu:~/Perlscripts$ perl -c stdin.pl
stdin.pl syntax OK
owen@ubuntu:~/Perlscripts$ perl stdin.pl
Do you want to remove this CSpec(y/n):n


OK - will not remove
owen@ubuntu:~/Perlscripts$ perl stdin.pl
Do you want to remove this CSpec(y/n):y


OK - will remove
owen@ubuntu:~/Perlscripts$





Owen

Dr.Ruud

2006-01-10, 4:01 am

Poonam Pahil schreef:

> Iam using <STDIN> to accept user input but the script just hangs at
> that point.


perldoc -q waiting

--
Affijn, Ruud

"Gewoon is een tijger."

Timothy Johnson

2006-01-10, 4:02 am


Try adding this line to the top of your script:

$| =3D 1;

This sets STDOUT to autoflush the buffer. You may be trying to print
something that just isn't showing up.

-----Original Message-----
From: Poonam Pahil [mailto:poonam.pahil@gmail.com]=20
Sent: Thursday, December 29, 2005 2:23 AM
To: beginners@perl.org
Subject: strange problem with STDIN- need help

Hi,

Iam using <STDIN> to accept user input but the script just hangs at that
point.
I type in the value & program is just stuck at that point.

i know its got to do with buffer flushing. when iam using the same piece
of
code at the beginning of the script everything works fine.

iam using system() a lot. directing input/output to/from files. can this
be
the reason.

This is the piece of code:

print "Do you want to remove this CSpec(y/n):";
my $remove_CS =3D <STDIN>;
chop($remove_CS);
if($remove_CS =3D~ m/n/i )

Thanks & Regards
Poonam

Poonam Pahil

2006-01-10, 4:02 am

i managed to figure out the cause.
i was undefining $/ .I wanted to use slurping technique.i.e reading in a
small file in a variable & then making the changes.

Ive now undefined this within a local context.
Its working fine now.
anyone has an idea how this is affecting <STDIN>.
I tried using $|=1 but that did not help.

Thanks
Poonam

On 12/29/05, Timothy Johnson <tjohnson@zone.checkpoint.com> wrote:
>
>
> Try adding this line to the top of your script:
>
> $| = 1;
>
> This sets STDOUT to autoflush the buffer. You may be trying to print
> something that just isn't showing up.
>
> -----Original Message-----
> From: Poonam Pahil [mailto:poonam.pahil@gmail.com]
> Sent: Thursday, December 29, 2005 2:23 AM
> To: beginners@perl.org
> Subject: strange problem with STDIN- need help
>
> Hi,
>
> Iam using <STDIN> to accept user input but the script just hangs at that
> point.
> I type in the value & program is just stuck at that point.
>
> i know its got to do with buffer flushing. when iam using the same piece
> of
> code at the beginning of the script everything works fine.
>
> iam using system() a lot. directing input/output to/from files. can this
> be
> the reason.
>
> This is the piece of code:
>
> print "Do you want to remove this CSpec(y/n):";
> my $remove_CS = <STDIN>;
> chop($remove_CS);
> if($remove_CS =~ m/n/i )
>
> Thanks & Regards
> Poonam
>
>


John W. Krahn

2006-01-10, 4:02 am

Poonam Pahil wrote:
> i managed to figure out the cause.
> i was undefining $/ .I wanted to use slurping technique.i.e reading in a
> small file in a variable & then making the changes.
>
> Ive now undefined this within a local context.
> Its working fine now.
> anyone has an idea how this is affecting <STDIN>.


Yes, I do. readline() is waiting for EOF on the STDIN filehandle.

> I tried using $|=1 but that did not help.


That is because setting $| only effects the currently selected filehandle
which is STDOUT by default.



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com