Hello my esteemed Monks, I have once again come to seek your wisdom in making my Perl more... Perl-ish. I have the following working code that will strip any MX records greater than 8 from a file (tinydns format). However, looking at the code, I feel that there is likely a shorter and more concise way to write it. I suspect some use of map or grep would be more efficient, however, I am not entirely familiar and/or comfortable with those functions. Hopefully through a Monk or two sharing his/her wisdom, I'll be able to write faster, better code (and understand it, too.) Also, I'm curious as to how one would go about editing the file in place, i.e., not having to create a new outfile.
Without further ado, here is the code.
#!/usr/bin/perl -w
use strict;
my $ifile = shift;
open IFILE, '<', "$ifile" || die "Couldn't open file: $!";
open OFILE, '+>', 'new-data' || die "Couldn't open outfile: $!";
while (my $line = <IFILE>) {
# is the line an MX record?
if ($line =~ /^@.*mail(\d+).*\z/xms) {
# is it less than or equal to 8?
if ($1 <= 8) {
print OFILE $line;
}
}
# print everything else to the new file
else { print OFILE $line; }
}
close IFILE;
close OFILE;
The data is in this format:
@*.somedomain.net::mail7.somedomain.net:10:21600
@somedomain.net::mail7.somedomain.net:10:21600
Thanks again, Monks!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.