BTW, the OP's match statement doesn't look as if it would work at all.

This one: /\<author\>\s*(\D*)$search/i? That should work fine for the examples he gave. It lacks robustness; it probably isn't the best expression of what he is looking for; a tail search doesn't seem very useful; and it certainly isn't how I would write it. But it should work.

By the way, if I were to do it the way you suggested, I wouldn't bother to reinvent the flip-flop operator. I'd write it like this:

sub readrec { my $rec = ''; while ( <FH> ) { $rec .= $_ if m|<rec>| .. m|</rec>|; last if m|</rec>|; } $rec; }
It'd be better to pass the filehandle, of course. Also, your version is rather brittle because of your use of string equality. If there happens to be space between a record's start or end tag and the following newline, yours breaks.

One other thing... in this construct:

while (defined($line = <FH>)) {
that defined() check isn't needed. Usually, including it could be classified as so-called "cargo-cult" programming. Honestly, I too probably still do it on occasion out of old habit. If you wanted your code to run quietly with warnings enabled on 5.004_04, it was a necessity.¹ There's really no reason for it these days though, provided you aren't still supporting 5.004_04. And if you are, it's time to consider upgrading. ;-)

1. I think the practice of using defined() in that manner primarily exists because of that warning emitted by 5.004_04 and not because of a real need. The construct, while ( <FH> ) is somewhat magical and checks for definedness. I think code that included an assignment in the loop, like while ( $line = <FH> ), did not check for definedness until sometime after 5.004_04. It was, however, a minor issue in reality because "\n" and "0\n" are both true values anyway. So, you might've run into an obscure bug if you changed $/ to something like "0" but it wouldn't have affected most code.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Re: Re: Searching data file by sauoq
in thread Searching data file by parisa

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.