ybiC has asked for the wisdom of the Perl Monks concerning the following question:
my %file = ( in => 'switches.txt', ); @ARGV = $file{in}; chomp (@ARGV = <>); foreach my $nonsane (@ARGV) { chomp $nonsane; unless ($nonsane =~ (/^(\w|-|\.)+$/)) { print " Ack - $nonsane is an improperly formatted device name +.\n"; print " Only alphanumeric, underscore, dash and dot allowed.\ +n"; exit; } } print (" Specified target names all valid.\n");
But when this chunk is in place, it doesn't seem to properly read $file{in} into @targets, when called as script.pl --infile switches.txt.
use Getopt::Long; GetOptions( 'infile|i=s' => \$opt_infile, ); if (defined $opt_infile) { $file{in} = $opt_infile; (@targets = $file{in}); } foreach my $nonsane(@targets) { chomp $nonsane; unless ($nonsane =~ (/^(\w|-|\.)+$/)) { print " Ack - $nonsane is an improperly formatted device name +.\n"; print " Only alphanumeric, underscore, dash and dot allowed.\ +n"; exit; } } print (" Specified target names all valid.\n");
The latter fails, and gives the *filename* as $nonsane in the 'Ack' error.
They're both reading the same input file, which looks somewhat like this: (plain-text file, one entry per line, IP address, hostname, or FQDN)
172.31.1.1
192.168.1.1
switchname
router.domain
I tried chomp'ing a few different ways, with no success. Fellow monks, what might I be overlooking, misunderstanding, or misapplying?
Perl 5.00503 on Debian Linux
#!/usr/bin/perl -w
use strict;
cheers,
Don
striving toward Perl Adept
(it's pronounced "why-bick")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: replace @ARGV args with Getopt::Long switches
by geektron (Curate) on Apr 12, 2001 at 01:40 UTC | |
by ybiC (Prior) on Apr 12, 2001 at 02:39 UTC | |
by jeroenes (Priest) on Apr 12, 2001 at 10:27 UTC |