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!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.