in reply to updating to a file
This may be a spurious comment, but in my experience I found that in Perl you seldom need flag variables like that. Not to say that this is "wrong", but sounds suspect...sub setManagers() { my $getInputFlag = 1; my $writeFileFlag = 1; my $mCounter = 0;
In modern perls you most often do not want to adopt the & form of sub call unless you really know what you're doing, which doesn't appear to be the case here...my $ans = &promptUser("Do you wish to configure SNMP Managers +on $var? ", "yes");
[n]?if ($ans =~ /^[n]o?/i) {
Here your regex has the form ^(?:/$regex\.){3}/$regex$, so you could increase its readability porting it explicitly into that form. Also, here you have parens for grouping (an alternation), but they're capturing parens, so you may change them intoif ($address =~ /^(([3-9]\d?|[01]\d{0,2}|2\d?|2[0-4]\d|25[0- +5])\.){3}([3-9]\d?|[01]\d{0,2}|2\d?|2[0-4]\d|25[0-5])$/)
instead.(?:[3-9]\d?|[01]\d{0,2}|2\d?|2[0-4]\d|25[0-5])
All in all I'd use an appearently more complex, but IMHO more readable test possibly putting it into a sub. I'd split on /\./\, then check that the return list has four elements, and do a check on each of them. This seems more {portable,maintainable}, especially if -like it seems reasonable- each of them would require a separate range of validity.
Also:$managers[$mCounter]{address} = $address; $managers[$mCounter]{port} = $port; $managers[$mCounter]{version} = $version; $managers[$mCounter]{community} = $community;
But then I wouldn't probably create the intermediate variables at all, if you ask me...@{ $managers[$mCounter] }[qw/address port version community]= ($address $port $version $community);
scalar is pleonastic there, and a rule of thumb is that Perl-style for loops should always be preferred over C-style ones, if they're equivalent, i.e. if the latter doesn't buy you something special, which definitely is not the case here. Thus:my $numOfManagers = scalar(@managers); print FILE "manager.total=$numOfManagers\n"; for (my $i=0; $i<$numOfManagers; $i++) {
But then chances are that doing some modification in the logic above you may just turn it intofor my $i (0..$#managers) { ... }
for my $item (@managers) { ... }
|
|---|