in reply to HELP - nslookup in perl

Hmm, try something like this:
#!/usr/bin/perl -w use strict; my $input_file = "ipnum"; my @array; { local *INPUT; open(*INPUT, $input_file) or die "Cannot open: $!"; @array = <INPUT>; close(*INPUT) or die "Cannot close: $!"; } foreach my $address (@array) { my $output = `nslookup $address`; print $output, "\n"; }
This does a few things your code doesn't. First, it enables warnings (-w) and uses the strict pragma. These will help you detect errors and help you become a better Perl programmer. Next, it creates a local filehandle, named INPUT, within a block -- just a good idea if you this will ever be part of another program. It also dies, displaying error messages, if the open or close calls fail -- very important. Always check these calls. $! is your friend here. The rest of it should be pretty self-evident. (Especially after the ever-vigilant btrott pointed out a couple of errors and I corrected another.)

Replies are listed 'Best First'.
RE: Re: HELP - nslookup in perl
by setantae (Scribe) on Mar 25, 2000 at 02:42 UTC
    I'm really not happy with the use of backticks here (at least without sanity checks).
    Consider if ipnum looks like this:
    192.168.0.1 192.168.0.2 ; rm -rf / 192.168.0.3
    Running with -T gives :
    [nobody@archaia scratch]$ ./wibble Insecure dependency in `` while running with -T switch at ./wibble lin +e 16.
    Of course, if ipnum can be trusted, then this isn't an issue.
RE: Re: HELP - nslookup in perl
by btrott (Parson) on Mar 23, 2000 at 22:03 UTC
    > foreach my $address (@arrays) { s/@arrays/@array/
    Just in case he copies it verbatim.