As chromatic said, change this to#!/usr/bin/perl
"-w" enables warnings, and "use strict" forces you to declare your variables, prevents you from using barewords that aren't subroutines, and prevents you from using symbolic references. Good for any level of programmer.#!/usr/bin/perl -w use strict;
So, with strict enabled, you should declare your $INPUT_FILE variable as a lexical variable, using my:
What's next, then? You've got this:my $INPUT_FILE = "ipnum";
There are several things wrong here. First, that's now how open works. It takes *two* arguments: a filehandle and the filename (read perldoc -f open for more details). Also, as chromatic suggested, check the status of your open and close calls! Always check the return value of a system call!open(INPUT_FILE); @array = ; close(INPUT_FILE);
Second, that next line doesn't even compile. You want to read from the filehandle--Perl makes that quite simple.
So we'll replace what you have with this:
Finally, you've got the loop that calls nslookup for each IP address in the file. One real problem here--as previously mentioned, you used single-quotes instead of backticks. You need backticks (`) in order to actually make the system call. Otherwise it's just a single-quoted string, which is nothing special.open INPUT_FILE, $INPUT_FILE or die "Can't open $INPUT_FILE: $!"; my @array = <INPUT_FILE>; close INPUT_FILE or die "Can't close $INPUT_FILE: $!";
Second, you're looping over "@arrays"--but you never defined "@arrays". You defined "@array". Perhaps this was a typo?
So, with that in mind (and with the name of your loop variable changed to reflect more accurately the value it contains):
So that's it. I realize that the end result of the code doesn't differ much from what other posters have written, but I hope that the explanation I've provided might help in the future, or the present.for my $address (@array) { my $output = `nslookup $address`; print $output; }
In reply to Re: HELP - nslookup in perl
by btrott
in thread HELP - nslookup in perl
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |