Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: <> oddity ?

by hdb (Monsignor)
on Mar 27, 2013 at 11:32 UTC ( #1025682=note: print w/replies, xml ) Need Help??


in reply to Re: <> oddity ?
in thread <> oddity ?

This is well spotted! If I modify my example to include this extra <>:

while(<>) { print; } my $line = <>; print "END\n";

then only

perl stdin.pl stdin.pl

waits for more input, while the other two versions finished immediately. That is still strange...

UPDATE: I guess Perl will do unshift(@ARGV, '-') unless @ARGV; (copied from LanX above) and "re-open" STDIN because @ARGV was exhausted in the while loop already. When using pipes or redirections this logic will not be applied and <> will only return EOF or similar.

Replies are listed 'Best First'.
Re^3: <> oddity ?
by pvaldes (Chaplain) on Mar 27, 2013 at 11:42 UTC

    It is explained in the post of Lanx

    In the first case the program is waiting for the user to type a value for the variable $line; in the second and third cases a default value is provided instead, $line = '-';

Re^3: <> oddity ?
by LanX (Bishop) on Mar 27, 2013 at 11:44 UTC
    > That is still strange...

    Is it really?

    I mean what is this code actually supposed to do?

    Let's say it clearly: Reading from an exhausted filehandle is nonsense!

    Just the fact that this filehandle is magic leads to undefined behavior, because interactice input can't be exhausted.

    Using either STDIN or ARGV here to is not only better, it explicitly demonstrates the intention of this line.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025682]
help
Chatterbox?
[ambrus]: No, you're not doing anything wrong. It's just that our automatic spam filters confuse you with the spammers who post advertisments for online shops of counterfeit branded clothing.
[ambrus]: Or unless you left the title field empty or entered a very short title for the node, but then you'd get a message saying that.
[ambrus]: I hope Corion or some other admin is here and can check the logs to see what the problem is.
[mz2255]: I wish I had an online shop but sadly no. The title field definitly wasn't short, had a perl module in the title with 5-6 additional words.
[ambrus]: you can also try to just post again in case it was some intermittent error

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-10-19 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (255 votes). Check out past polls.

    Notices?