I'm replacing @ARGV arguments with command-line switches in an existing program.   When the script contains this chunk, it works fine called as script.pl
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")


In reply to replace @ARGV args with Getopt::Long switches by ybiC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.