Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Re: Re: reading linesby BrowserUk (Patriarch) |
on Dec 07, 2003 at 04:38 UTC ( [id://312855]=note: print w/replies, xml ) | Need Help?? |
A couple of comments on what you have. As you are reading line-by-line, you will only ever get one newline character which will be at the ned of the line. Therefore, the /gs options on s/\r|\n//gs; are redundant. The /g because there can be only one \n (\r is most unlikely to occur!). The \s because unless your regex contains one or .'s, /s does nothing. In any case, what I assume you are trying to do is remove the trailing newline, in which case, see chomp which is design for doing exactly this. With your 3 if statements, all 3 conditions are being tested for every line you read. As only one of them can ever match a given line, you would be better coding that as an if(...){}elsif(...){}elsif(...){}else{} cascade. That said, the problem with processing multi-line records line-wise, is that if forces you to retain state. Ie. You have to remember what the last thing you parsed was in order to know how to handle the next line.
Whilst that does the job, I'm not fond of flag variables and try and avoid them where I can. In this case, perl can remember the state for you -- which is almost as bad in some ways, but it's there, so you might as well make use of it. As the output from ipconfig is unlikely to be much more that a hundred or so bytes, there is no reason not to read the whole thing into scalar, and then use the regex engines "memory" (/gc options) of where it has got to in processing the string, to allow you to nibble away at it a piece at a time.
Note: That can just as easily be structured using 'normal' if statements
Except now I have to pre-declare the results variables and, to my eyes, the purpose of the code has become obscured by the structure. In the previous version is was very obvious that the three blocks of code produced 3 sets of results and what they were called. In this version, I have to look much more closely to see what is going on...but that's me:)
In Section
Seekers of Perl Wisdom
|
|