do { local $/ = "fieldname"; <$fh> }; # read through "fieldname"

The local $/ = "fieldname"; statement sets the $/ input record separator special variable (see perlvar) to the literal 'fieldname' string. This sets "paragraph" read mode: (no: see Update below) | This causes <$fh> to read the input stream from the beginning of the file (in this particular case) until the end of the first point at which the 'fieldname' string is encountered. Since the field names are apparently unambiguously known, this reads (almost) all the way through the first field name. The $/ variable is assigned local-ly in a do-block, so it returns to its previous value (the "\n" default in this case) at the end of the block.

my $header_line = "fieldname" . <$fh>; # complete the line

Since we (apparently) know the header line begins with 'fieldname', assign $header_line this initial value and complete reading the line with another <$fh>. This reads through the end of the line because $/ has restored to its original newline value.

Update:

This sets "paragraph" read mode: ...
No, this is not "paragraph" (sometimes called "paragrep") read mode, it is normal read mode. See $/ in perlvar for a discussion of paragraph mode.

In normal read mode, a file is read until just after the sequence of one or more characters in the $/ special variable is encountered (and including that sequence), or until the end of file if the $/ sequence is never encountered. Usually, $/ is a single "\n" (newline) character, but it can be any non-empty string. Whatever non-zero-length sequence of characters it may be, this is normal read mode.


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


In reply to Re^3: skip junk lines in csv before header (updated) by AnomalousMonk
in thread skip junk lines in csv before header by karlberry

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.