in reply to Merge Algorithm (Combining Sorted Lists)
'A val that is guaranteed not to be present in any list'
ow! Please use out-of-band signaling. Safer, Simpler, Prettier.
Method 1, return value + lvalue param:
my $fetch = sub { my ($fh) = @_; return if eof $fh; $_[1] = scalar <$fh>; return 1; }; ... while ($next->(my $item)) { print "$item\n"; }
Method 2, undef vs reference:
my $fetch = sub { my ($fh) = @_; return if eof $fh; return \(scalar <$fh>); }; ... while (my $ref = $next->()) { print "$$ref\n"; }
Method 3, empty list vs non-empty list (like each):
my $fetch = sub { my ($fh) = @_; return if eof $fh; return scalar <$fh>; }; ... # RIGHT: while (my ($item) = $next->()) # WRONG: while (my $item = $next->()) while (my ($item) = $next->()) { print "$item\n"; }
Update: Added second and third method.
|
|---|