Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Iterator to parse multiline string with \\n terminator

by three18ti (Scribe)
on Oct 06, 2013 at 09:20 UTC ( #1057134=note: print w/replies, xml ) Need Help??


in reply to Re: Iterator to parse multiline string with \\n terminator
in thread Iterator to parse multiline string with \\n terminator

WRT your edit

What do you think about a return instead of an if? e.g.:

sub fh_iterator { my $fh = shift; my $line = $fh->getline(); return $line unless $line; $line .= $fh->getline() while $line =~ m{\\$}; return $line; }

I don't think there's any functional difference, but one may be more readable than the other...

Thanks for your help!

Replies are listed 'Best First'.
Re^3: Iterator to parse multiline string with \\n terminator
by tinita (Parson) on Oct 06, 2013 at 12:46 UTC
    return $line unless $line;
    make that:
    return $line unless defined $line;
    theoretically the last line could be missing the newline and only contain "0". then $line would be false and ignored with your code.
Re^3: Iterator to parse multiline string with \\n terminator
by Athanasius (Chancellor) on Oct 06, 2013 at 16:28 UTC

    ++tinita for highlighting the important difference between testing for definedness and testing for truth (see perlsyn#Truth-and-Falsehood).

    But even with the correction I prefer my version. Readability is in the eye of the programmer, but the first high-level programming subject I took at Uni (in Pascal!) emphasised structured programming, and this has remained with me. I prefer a function to have a single exit point (at the end) where possible. In Perl this is not always optimum, so I’ve had to learn to be flexible. But when — as in this case — the structured version is as straightforward as the non-structured one, I prefer the former. YMMV.

    As always in Perl, TMTOWTDI.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057134]
help
Chatterbox?
[ambrus]: and the electronics gets reselled almost new, but it has to be sold at half price because otherwise everyone chooses to buy the new product which has fewer risk of selling damaged products labelled as almost new.
[ambrus]: You can actually get a lot of useful cheap really almost new products that way, with only a little risk of scams.
[ambrus]: That's what some of the "Black Friday" sales are about.
[Corion]: ambrus: Well, usually, these people don't have in their description "mail me at dodgy_reseller # g m a i l | co m" , replace the "#" by "@" :)
[Corion]: Oh, and the "o" in "com" is a zero
choroba orders a camera from Ole Scæmmer
[ambrus]: Corion: ah. that's different. the ones I mean are selling at reputable sites like ebay that usually filters scammers out pretty quickly (as well as filters a lot of legitimate users who then get annoyed that the biggest providers exclude them)

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2017-11-21 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (304 votes). Check out past polls.

    Notices?