I've got a number of perl scripts, that more or less use the same format as this, but this one script refuses to play nice.
#!/usr/bin/perl use Getopt::Std; my ($miscreants, $reason) = @ARGV; my %opts; getopts ('u', \%opts);
Amongst the inners of this script we have an  unless ($opts{'u'}) ... block that passes, it knows no -u was issued and works without error ./acl.pl 1.2.3.4

Then where I need it to, ./acl.pl -u 1.2.3.4 if ($opts{'u'}) ...

upon executing it errors-u must be IP or IP/CIDR which is the print statement for what happens when it doesnt get the ARGV[0] aka miscreants as numerical

It seems it is seeing -u as ARGV[0] , verified by checking what miscreants returns.

Other scripts happily ignore the switches and use the ARGV[0] for their purposes, and they also work still with or without switches, eg: an addhost script that may have options like -n or -d or neither, then domain = ARGV[0], the same type of thing I'm trying here, it works, it knows -n and -d are switches if used or if not used, it knows ARGV[0] is the non switched argument given, for the life of me I can not figure why this darn script does not.

I thought if perl didnt recognise the -u as a switch, it would pass it through to argv[0] like it is doing here, but as you from my opening lines, its very clearly defined, those lines are copy and paste, and are identical to the scripts that do work.

I've triple checked the spelling and syntax (perl -c scriptname passes as syntax OK)

snip... This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux-thread-multi Anyone see what might be wrong? Thanks


In reply to [RESOLVED] switch being ignored put into ARGV by Nobby

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.