in reply to What is clear code ?

It's not necessarily the best teaching example due to the complexity of it all, but it does show how Perl can work. The "chained" function system is often very useful in data-translation exercises, such as this.

It might be a little tidier as such:
sub Clear_MakePtag { return join ("\r\n", map { "<p>$_</p>" } grep { /\S/ } split ("\r\n", my ($fix) = @_) ); } sub MakePtag { my ($fix) = @_; $fix =~ s!\r\n$!!s; $fix =~ s!\r\n!</p>\r\n<p>!g; return "<p>$fixme</p>"; }
Cosmetic mostly, but since this is about clarity. The chomp call seems kind of strange since it takes off $/, which is OS specific, right? Better to just say what you mean, I would think.

Replies are listed 'Best First'.
Re: What is clear code ?
by mandog (Curate) on Apr 29, 2002 at 03:14 UTC

    update: In the original node, I'm probably not as clear I should be that much of the second sub is ovid's

    Yes, while the chomp better than the no chomp, it is not perfect. If the string is something like "asadf\r\n" and the OS is \r\n orriented, the chomp gives us "<p>asdf</p>" instead of "<p>asdf</p><p></p>". If we are processing the string on a \n orriented OS, We are out of luck...

    If I understand  perldoc perlre the "/s" or "!s" at the end of the regular expression means "Treat string as single line. "

    However, in tadman's node, I'm not getting how s/\r\n//s ( or s!\r\n!!s ) means "take off the \r\n at the end of the string" ???



    email: mandog
      tadman said:
      $fix =~ s!\r\n$!!s;
      Notice the '$' mark in the line: it anchors the '\r\n' to the end of string. Meaning, replace \r\n only if it appears as the last thing in the string.