I think this is memory related... The files are big and your keeping all your data in a big string... I would definitely recommend not using the foreach loops to loop thru the file; I think perl will try to initialize the loop values first, which means it will try to read the entire file into a temporary variable... As I understand it the files can be quite large.

I would replace the file reading loops with while(my $line=<DHCPDCONF>) and while(my $line=<LEASES>) respectively. You could even do just while(<DHCPDCONF>) and drop the line$=~ parts from your regex's. This will save a lot of memory and also make the script faster. You could save the information you'll need to rewrite the config file in a hash which is faster and cheaper on your memory.

In the same spirit I would place the print DHCPDCONF line into the loop that generates the file content, and replace the loop with: while(my($device,$ip)=each %devices) and use $ip in the substitution regex. This again saves a lot of memory and is faster. I you really want to handle the existing file content, you could loop thru the file line by line, extract it's $devices key from the line, check to see if it exists in your hash with ip's, and if so perform a substitution on the line. All the while saving to a temporary file, which you later move over the original.

This should be faster, cleaner and ultimately more versatile. (And it might not hang...)


In reply to Re: script hangs up when reading in file by Gilimanjaro
in thread script hangs up when reading in file by tombmbdil

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.