in reply to Continuing While Loop One Iteration After EOF

Hmmmm...

Two possible ways I could think of...

my $i = 1; while ((my $line = <INHANDLE>) || $i--) {}

or, just what has been said here

while (my $line = <INHANDLE>) { } continue { dosomething() if eof(INHANDLE); }

I would prefer the first, as the continue solution wouldn't enable the reuse of the while block.

daniel

Replies are listed 'Best First'.
Re^2: Continuing While Loop One Iteration After EOF
by ikegami (Patriarch) on Dec 21, 2005 at 17:48 UTC
    while (my $line = <INHANDLE>) { ... } continue { ... if eof(INHANDLE); }

    is no different than

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

    (barring reading errors), so it's not very useful. The code will be duplicated, which seems to be what the OP wants to avoid.

      Hmmmm... sure... Actually I spent 2 minutes to understand your post, before I could see the "..." after the while block. Indeed, I keep with the $i-- solution...

      daniel
        Well, in reality, it will be
        while (<INHANDLE>) { dfkahsfkasdkfjhasdklfa; dsfjasd jf; lsdfj asdljf; } continue { if (eof(INHANDLE)) { dfkahsfkasdkfjhasdklfa; dsfjasd jf; lsdfj asdljf; } }

        vs

        while (<INHANDLE>) { dfkahsfkasdkfjhasdklfa; dsfjasd jf; lsdfj asdljf; } dfkahsfkasdkfjhasdklfa; dsfjasd jf; lsdfj asdljf;

        But that's completely misses what I meant to point out. It sounds like the OP wanted to avoid code duplication (or else, he'd would simply have used the second snippet), but your code doesn't do that.