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

Hi Monks!

I've run into a bit of a problem. When I'm trying to use <STDIN> in scalar context my scripts are executed without me being able to give an input. But in list context <STDIN> works just fine. This problem occured very suddently. Programs that worked an houre ago aren't working now. What's going on? Do I have a bug in my computer?

Exampel:

This dosen't work:

#!/usr/bin/perl $a = <STDIN>; print $a;
This does:
#!/usr/bin/perl @a = <STDIN>; print @a;
Greatfull for any suggestions.
Thanks!

update (broquaint): added formatting and changed title (was STDIN)

Replies are listed 'Best First'.
Re: STDIN behaves different according to context
by broquaint (Abbot) on Feb 17, 2003 at 12:38 UTC
    When I'm trying to use <STDIN> in scalar context my scripts are executed without me being able to give an input
    It sounds like something is already waiting on STDIN when you read from it. So when you read from STDIN in a scalar context it will read whatever's waiting but in list context it will continue reading until it receives an EOF. Further information about your system and perl version would go a great way towards finding a solution.
    HTH

    _________
    broquaint

      In such a case, it might be useful to figure out what exactly is coming into STDIN without your knowing about it. Try converting each character to its numeric value and write that:

      my $a = <>; local $, = ' '; # Put some whitesapce between each number print unpack("U*", $a), "\n";

      Above is untested, as always.

      ----
      Invent a rounder wheel.

      Also, it would be useful to know how the script is being called. If you're running it by calling (dumb example):

      $ echo '' | myscript.pl

      then I can see the problem.

      LAI
      :eof

      I figured out what is waiting in STDIN, it is a "new line feed or a new line", the numeric value 10. What system and version am I using? I'm using the perl version 5.6.1 for MSWin32-x86-multi-thread. I'm runing my programs with CYGWIN, a Unix environment for Windows.
Re: STDIN behaves different according to context
by davorg (Chancellor) on Feb 17, 2003 at 12:19 UTC

    This problem isn't being caused by your code. Your code works just fine. The problem must be caused by some external factor.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: STDIN behaves different according to context
by Coruscate (Sexton) on Feb 17, 2003 at 16:27 UTC

    This problem again, eh? It's just an OS-specific 'feature'. No, it's not a bug, it's just the behaviour of some OSes. Simply change your code to print a newline after the output:

    #!/usr/bin/perl $a = <STDIN>; print $a, "\n";


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.