Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

[RESOLVED] switch being ignored put into ARGV

by Nobby (Novice)
on Oct 16, 2022 at 10:51 UTC ( #11147461=perlquestion: print w/replies, xml ) Need Help??

Nobby has asked for the wisdom of the Perl Monks concerning the following question:

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 ./

Then where I need it to, ./ -u 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

Replies are listed 'Best First'.
Re: switch being ignored put into ARGV
by Corion (Patriarch) on Oct 16, 2022 at 12:31 UTC

    The order in which you assign stuff is wrong. Getopt::Std removes items from @ARGV. Try

    #!perl use 5.020; use Getopt::Std; @ARGV = ("-u", "my miscreants", "Some reason"); my %opts; getopts ('u', \%opts); my ($miscreants, $reason) = @ARGV; say "---"; say "Miscreants: $miscreants"; say "Reason: $reason"; say "-u set: " . $opts{'u'}; %opts = (); @ARGV = ("-u", "my miscreants", "Some reason"); ($miscreants, $reason) = @ARGV; getopts ('u', \%opts); say "---"; say "Miscreants: $miscreants"; say "Reason: $reason"; say "-u set: " . $opts{'u'};
Re: switch being ignored put into ARGV
by davies (Prior) on Oct 16, 2022 at 11:50 UTC

    Perl itself can never "recognise the -u as a switch". Getopt::Std might, but @ARGV is where all command line arguments are held. Without a SSCCE ( it's hard to tell, but your assignment to $miscreants and $reason is position sensitive (they are the first two command line arguments) while the Getopt family (AFAIK) is insensitive, not caring whether the -u is first, last or somewhere in the middle. You are therefore using a mixture and may very well find some things work because of the order of arguments in the invocation. Try moving everything to positional or Getopt-friendly format and you may find your problems easier to solve, even if they don't go away.

    I also recommend looking at The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!. Both modules are core.


    John Davies

Re: switch being ignored put into ARGV
by Nobby (Novice) on Oct 17, 2022 at 02:53 UTC
    Thanks to you both, yes, that was it! In my other scripts, the addhost, etc and related scripts that do work, domain = argv0 is defined much lower in the code, reordering as suggested here has fixed it and it is now playing nice :) Thanks again guys!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11147461]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2023-05-29 13:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found