Relying on a paticular line to be at a particular position in a data file is a bad thing. Never trust hard-coded values.

I was playing around and came up with this little snippet to find out what line the ServerName directive is located on in the httpd.conf file, as well as it's value:


use strict;

open (FILE, 'httpd.conf') or die "Painfully";
my @a = <FILE>;
close FILE;

my $line_no;
my $name;

for (0..$#a) {
    if ($a$_ =~ /^ServerName\s+(.+)\s*$/) {
        $line_no = $_ + 1;
        $name = $1;
        last;
    }
}

print "Line = $line_no\nName = $name\n";

I am sure this can be condensed elegantly, I tried it with grep, but couldn't get it to work. I am sure someone in the Monastery will improve this, especially my lazy use of .+ (if Dot Star is dead, can we still use Dot Plus?)

Jeff


L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)


In reply to (jeffa) Re: Apache IP-Autoupdater by jeffa
in thread Apache IP-Autoupdater by Nimster

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.