First off, the best code is code that works. As your code functions, everything else is necessarily subjective.

That said, I would modify your slurp function to look like:

sub slurp { my $file = shift; local $/; open my $fh, '<', $file or die "Can't open $file: $!"; return <$fh>; }

The big changes here are swapping to indirect filehandles and 3 argument open. Indirect filehandles are guaranteed not to collide with a previously existing file handle and will automatically close out once the variable goes out of scope. See Indirect Filehandles in perlopentut for more virtues. 3-argument open doesn't really matter in this case, but it protects you from some malicious vectors so it's usually considered a good habit to get into. I removed the = undef from your $/ localization, since that is redundant. I explicitly named the input parameter and explicitly returned to make intent more obvious to the casual reader.

Since you are outputting CSV, I would also likely use an explicit CSV module, such as Text::CSV. Again, it doesn't matter in this case, but it handles escaping which may matter to you in the future.

Finally, rather than using a while with a long multiline regex, I'd probably either do a streaming parser or a split. The longer the regex, the easier it is to break and the harder it is to fix. But, as far as they go, yours is pretty clean.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

In reply to Re: Multiline RegExp. A Better Way? by kennethk
in thread Multiline RegExp. A Better Way? by shoness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.