in reply to Re^3: Loop through array or filehandle
in thread Loop through array or filehandle

Interestingly, data from an HTTP::Request, when split by \n into an array, only loops to the first empty line and then the while loop stops. If the data from the request is written to a file then the while loop works as expected.

The only work around I can find at the moment is to

$data =~ s/^$/ /gsm

Not ideal, but better than working out why an empty line from an HTTP::Request GET signals a termination of a while() loop.

Replies are listed 'Best First'.
Re^5: Loop through array or filehandle
by Laurent_R (Canon) on Sep 30, 2016 at 16:57 UTC
    In Perl, an empty string is deemed to be false, therefore it will stop the while loop.

    The while loop should probably be amended to check for definedness rather than truth. I cannot test now on my mobile device, but will come back with a proposed solution as soon as I get a chance to test it.

Re^5: Loop through array or filehandle
by AnomalousMonk (Archbishop) on Sep 30, 2016 at 17:34 UTC

    I.e.:

    my $iter = create_iter(...); ... while (defined(my $line = $iter->())) { do_something_with($line); }


    Give a man a fish:  <%-{-{-{-<

      Yeah, that's exactly what I was thinking about, but I did not want to suggest anything without testing.