in reply to Re: Re: Searching data file
in thread Searching data file
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:
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.sub readrec { my $rec = ''; while ( <FH> ) { $rec .= $_ if m|<rec>| .. m|</rec>|; last if m|</rec>|; } $rec; }
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.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. |