Hi! There are a great many things you can improve in the Perl code here.
- Please always use strict and warnings. These are there to help you find your errors.
- Always declare variables with 'my'. There are a few here that you forgot to declare
- When parsing CSV files, use the module for this: Text::CSV. It is much better at parsing CSV files than the naive split approach.
- Calling functions with & is bad practice for the vast majority of cases. That's mostly a hold-over from Perl 4 days. While there are cases where it's proper, it's not proper anywhere you've done it. Call functions simply with parens: some_function()
- When passing variables to a function, there's no need to surround those variables with quotes. call_func("string", $string)
- Do not use BAREWORD file handles.
Since I don't have your module SNMP_util and I can't really test things, I won't try to rewrite your entire script, but here's a good starting point:
#!/usr/bin/env perl
use strict;
use warnings;
use NetAddr::IP;
use SNMP_util;
use Text::CSV ();
my $csv = Text::CSV->new({binary => 1})
or die "Can't use Text::CSV: ".Text::CSV->error_diag();
open my $fh, "<:encoding(utf8)", "formerlyBridges2Input.csv"
or die "Can't open CSV: $!";
while (my $row = $csv->getline($fh)) {
# $row is now an array reference containing the
# CSV's fields.
# If the line is "a,b,c" then we will have:
# $row->[0] "a"
# $row->[1] "b"
# $row->[2] "c"
}
Please take a look at that and try to work in your code using the CSV parser and show us what you get. If you have a specific problem, we'll be more than happy to help a bit more.
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.