http://www.perlmonks.org?node_id=1041033

jrvd has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have a problem doing something the smart way. I am new to programming in perl so am unsure what the best solution is.

My problem is this: I have a text file that has many lines. I want to go through it to match lines with /^hostname / and once I find a line that contains this, I want to go through the following lines, do some actions on said lines until I find another line that contains /^hostname / and repeat.

Here is what I did so far:

sub hostname{ print $fd $_; while (<$fh>){ if ($_ =~ /^hostname /){ hostname; }else{ # print $_; $tot2++; } } $tot++; } while(<$fh>){ hostname if ($_ =~ /^hostname /); }

Obviously I open and close the files and declare the necessary variables. However, although this works, it gives me a deep recursion warning so I am fairly certain there is a better way to do this that I just haven't thought of.

Thanks for your help!

jrvd