LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2011, 03:09 PM   #16
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908

Quote:
Originally Posted by anomie View Post
Sorry, but this is getting silly. So the expectation was that reading in a 0 should have ended the while() loop? Then how the fudge would you ever read a 0 from a file?
Well, in the context of the OP's code/question, the value would still be read. Only the matter of terminating the loop was in question. Two different matters.
I think we now all understand what was originally in question, and the explanation for the apparent paradox.
--- rod.
 
Old 08-30-2011, 09:36 PM   #17
password636
Member
 
Registered: Jun 2006
Location: Beijing
Posts: 60

Original Poster
Rep: Reputation: 1
Finally I think I've found the right answer to my question. And sorry guys I don't think you all get the right point

@theNbomr
Empty string, "0", '0' and 0 (integer) are all evaluated False in the while conditional. It's not about forcing the integer context, it's a scalar context.

@David the H.
You are close. But there is no such "the whole expression being evaluated" thing. Just like C, such kind of conditional depends on the value of the expression, not the expression itself.

Post the right answer here so that we can all learn something

Code:
       The following lines are equivalent:

           while (defined($_ = <STDIN>)) { print; }
           while ($_ = <STDIN>) { print; }
           while (<STDIN>) { print; }
           for (;<STDIN>;) { print; }
           print while defined($_ = <STDIN>);
           print while ($_ = <STDIN>);
           print while <STDIN>;

       This also behaves similarly, but avoids $_ :

           while (my $line = <STDIN>) { print $line }

       In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see
       whether it is defined.  The defined test avoids problems where line has a string value that would be treated as false
       by Perl, for example a "" or a "0" with no trailing newline.  If you really mean for such values to terminate the
       loop, they should be tested for explicitly:

           while (($_ = <STDIN>) ne '0') { ... }
           while (<STDIN>) { last unless $_; ... }

       In other boolean contexts, "<filehandle>" without an explicit "defined" test or comparison elicit a warning if the
       "use warnings" pragma or the -w command-line switch (the $^W variable) is in effect.
This could be found in perldoc perlop - I/O operators.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression in Perl tarken Programming 11 05-12-2010 08:10 PM
regular expression with perl MissEileen Programming 3 03-11-2008 01:26 PM
Perl Regular Expression abdul_zu Linux - Software 2 08-20-2007 03:57 AM
Perl Regular Expression rch Programming 14 07-11-2003 11:00 PM
perl expression Q bobjones Programming 4 07-24-2002 12:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration