Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1089661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:15:22+00:00 2026-05-16T23:15:22+00:00

I usually loop through lines in a file using the following code: open my

  • 0

I usually loop through lines in a file using the following code:

open my $fh, '<', $file or die "Could not open file $file for reading: $!\n";
while ( my $line = <$fh> ) {
  ...
}

However, in answering another question, Evan Carroll edited my answer, changing my while statement to:

while ( defined( my $line = <$fh> ) ) {
  ...
}

His rationale was that if you have a line that’s 0 (it’d have to be the last line, else it would have a carriage return) then your while would exit prematurely if you used my statement ($line would be set to "0", and the return value from the assignment would thus also be "0" which gets evaluated to false). If you check for defined-ness, then you don’t run into this problem. It makes perfect sense.

So I tried it. I created a textfile whose last line is 0 with no carriage return on it. I ran it through my loop and the loop did not exit prematurely.

I then thought, “Aha, maybe the value isn’t actually 0, maybe there’s something else there that’s screwing things up!” So I used Dump() from Devel::Peek and this is what it gave me:

SV = PV(0x635088) at 0x92f0e8
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  PV = 0X962600 "0"\0
  CUR = 1
  LEN = 80

That seems to tell me that the value is actually the string "0", as I get a similar result if I call Dump() on a scalar I’ve explicitly set to "0" (the only difference is in the LEN field — from the file LEN is 80, whereas from the scalar LEN is 8).

So what’s the deal? Why doesn’t my while() loop exit prematurely if I pass it a line that’s only "0" with no carriage return? Is Evan’s loop actually more defensive, or does Perl do something crazy internally that means you don’t need to worry about these things and while() actually only does exit when you hit eof?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:15:23+00:00Added an answer on May 16, 2026 at 11:15 pm

    Because

     while (my $line = <$fh>) { ... }
    

    actually compiles down to

     while (defined( my $line = <$fh> ) ) { ... }
    

    It may have been necessary in a very old version of perl, but not any more! You can see this from running B::Deparse on your script:

    >perl -MO=Deparse
    open my $fh, '<', $file or die "Could not open file $file for reading: $!\n";
    while ( my $line = <$fh> ) {
      ...
    }
    
    ^D
    die "Could not open file $file for reading: $!\n" unless open my $fh, '<', $file;
    while (defined(my $line = <$fh>)) {
        do {
            die 'Unimplemented'
        };
    }
    - syntax OK
    

    So you’re already good to go!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually write (smallish) games in Java using a main 'while' loop somewhat like
I am using the Visio API to loop through each Microsoft.Office.Interop.Visio.Master object in a
In java I usually make a for-loop like following: for (int i = 0;
I am not too familiar with objects, i usually convert them to arrays using
Usually I'd add a for each loop for a mysql database entry in PHP,
Usually, as my code base grows, the functions recieve more and more arguments and
Usually (if not always), when jQuery allows you to add a callback to some
I've been going back through all my old code and trying to optimize it.
I have a for-loop that will run a fixed number of times, usually in
I want to learn more C++... Usually I make a for loop to parse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.