in reply to Help Matching Sections of a file
If your going to re-read the file every run, and assuming that the record separator is consistant (which it isn't in your sample data), then it might be more efficient to read the records, record by record by setting $/ to the separator, and only reading as many as needed.
#! perl -slw use strict; sub port_definition{ my( $port, $def ) = shift; #open FILE, '<', .... local $/ = '#-------------------------------------------'; 1 until ($def = <DATA>) =~ m[Port $port]; return $def; } print port_definition 123 ; __DATA__ Port 119 NNTP * Microsoft Ports-Microsoft Exchange supports a News server running at + this port. * RFC977 * RFC1036 #------------------------------------------- Port 120 CFDP (UDP) Coherent File Distribution Protocol (CFDP) * RFC1235 #------------------------------------------- Port 123 NTP (UDP) NTP (Network Time Protocol) * RFC2030 * RFC1129 #-------------------------------------------
|
|---|