in reply to Getopt::Long Validation

I took some liberties with your variable and command switch names (too wordy!), i also brought Pod::Usage along for the ride. Hope this helps. :)
use strict; use warnings; use Pod::Usage; use Getopt::Long; our($file,$range,$help); GetOptions( 'file|f=s' => \$file, 'ip|i=s' => \$range, 'help|h' => \$help, ); pod2usage(-verbose=>2) if $help; unless ($file or $range) { $file = shift or pod2usage(-verbose=>1); } if ($file) { open FH, '<', $file or die "can't read $file\n"; chomp($range = <FH>); } print "$range\n"; __END__ =head1 NAME foo.pl - does something with IP range =head1 SYNOPSIS foo.pl [filename] [-file filename] [-ip IPrange] Options: -file -f file to read -ip -i IP range -help -h help message =head1 EXAMPLES Read from file: foo.pl ip.txt foo.pl -f ip.txt foo.pl -file ip.txt Read from STDIN: foo.pl -i 127.0.0.1 foo.pl -ip 127.0.0.1

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: Getopt::Long Validation
by Ninthwave (Chaplain) on Oct 20, 2003 at 20:57 UTC
    Thank you I can follow this one the best. I did use pod2usage I just stripped it out of the code to trim the code down so it was more readable. But thank you for the logic of this. Thank you everyone for all the replies.
Re: Re: Getopt::Long Validation
by Ninthwave (Chaplain) on Oct 21, 2003 at 13:58 UTC

    On the unless statement should that be xor. Because or is validated left to right but if the left expression is true it stops. So if both were true it would still process?

    So this code could accept:

    foo.pl -f ip.txt -i 127.0.0.1
    I am testing the code now but just questioning if my understanding of the logic is correct. I like to process it in my mind before on the screen.

    Update just ran it and it needs to xor to limit it to one of the two input methods.

      Yep, you sure do. I wrote my code such that the file trumps the IP argument. I personally prefer this, after all - why should i care if the user specifies both? I'll just pick one and go - but the xor does do a better job of making those who don't like to RTFM do so. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        I agree I just know the users that will have to use this and I know it will be an issue I would get email about for awhile. But thank you for the main logic it helped greatly.