in reply to Re: Continuing While Loop One Iteration After EOF
in thread Continuing While Loop One Iteration After EOF

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.

Replies are listed 'Best First'.
Re^3: Continuing While Loop One Iteration After EOF
by ruoso (Curate) on Dec 21, 2005 at 18:08 UTC

    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.

        If avoiding code duplication is the goal, put the code that exists within the while loop into a subroutine, and call that sub one more time after the loop. If he wants to get tricky, he could generate a callback routine as a closeure within the loop that gets executed after the loop, though that seems like a lot of complexity that may lack justification.


        Dave

        Hmm? I agree that the "continue" solution is pointless... But is the other solution too?

        my $i = 1; while ((my $line = <INHANDLE>) || $i--) { dfkahsfkasdkfjhasdklfa; dsfjasd jf; lsdfj asdljf; }
        daniel