Your code looks fine to me. "Elegance" often gets translated to "succinctness", which can often lead to decreasing legibility. (Verboseness can also be hard to read, but your code doesn't suffer from this.)

That said, if you want to save a couple of keystrokes without getting too obscure, you could try

my $content = do { local $/; local @ARGV = 'vhosts.conf'; <> };
(Actually, that probably didn't save any keystrokes; just avoided creating a new handle.) Also, if you want to use $start and $finish to build your regexp, I would use qr on them:
my $start = qr{<VirtualHost \*:80>}; my $finish = qr{</VirtualHost>};
And, in this case, I see nothing wrong with putting all your printing in one line:
print OUT "<VirtualHost *:80>$vhost</VirtualHost>";
Lastly, if you want to be excruciatingly correct, you should check the return value of all system calls, and that includes not just open but also close. (But, in this case, maybe you should avoid the ARGV trick above, and check the return value of that open as well).

But most of these are just personal preferences; whatever improvement they bring to the code, either in terms of readability or "elegance", is marginal at best.

the lowliest monk


In reply to Re: Clean up httpd.conf virtual host mess with perl by tlm
in thread Clean up httpd.conf virtual host mess with perl by redhotpenguin

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.