GetOptions problems

From: Cats (mail_at_catsnest.co.uk)
Date: 09/30/04


Date: Thu, 30 Sep 2004 16:35:07 +0100 (BST)
To: beginners@perl.org, wiggins@danconia.org

ok ill send again from the correct address...

>Actually you were probably 'sure', and it is probably failing for some
'reason'.
...

>Start with a spell checker, use punctuation, work on the grammar. Have
a look at:

yes i know i can not spell, im a dislexic moron
thank you i will read the page from the link.

>
> thank you.
> RichT
>
>
> /scanPoller.cfg.pl==================================================
> more scanPoller.cfg.pl
> #!/usr/local/bin/perl
> #
>
> use strict;
> use warnings;
> use Getopt::Long;
>
> my
>
($inFile,$USAGE,$showKey,$site,$found,$foundKeys,@dataFile,@foundSegments,$value);

Declaring all of your variables up front defeats the purpose of 'strict'
and makes it far less useful. You should declare your variables at first
usage and in the proper scopes.

are i allways beleaved you should declare them like this...
how should i?? some thing like:

my $USAGE = ...

and will this work if the first time the variables is used is in a loop?

> my ($opt_inFile, $opt_listAllFields, $opt_help ) = (0,0,0);
> my $opt_findField = "agentAddress";
> my $opt_showFields = "segment,agentAddress,community";
>
> $USAGE = <<USAGETEXT;
> usage: $0 ipaddress
> the following options are also availble
> [-inFile filename ] input filename
> [-findField fieldName ] this is the search key (default is
> agentAddress)
> [-showFields field names ] feilds to output (default is
> segment,agentAddress,community)
> [-listAllFields ] list avalible fields
> [-help] this help text
> USAGETEXT
>

>The above is ok, but you might consider taking the advice of the
>Getopt::Long docs and using Pod::Usage to generate error messages and
help text via pod.

ill look in to this. i have been using "O'reilly's Programming Perl
/Learning Perl" which say nothing of using Pod

> &GetOptions( "inFile=s",
> "findField=s",
> "showFields=s",
> "listAllFields",
> "help|h|H|?|HELP",
> );
>

>You should drop the C<&> it is not needed in this context. You should
also go back to the docs for Getopt::Long, it does not have the default
$opt_ variables for the options, it instead uses references or a hash. I
assume this is left over from Getopt::Std, and in that case you would
have to declare your $opt_ variables with C<our> instead of C<my>.

> print " listAllFields =$opt_listAllFields\n"; #testing
> print " help =$opt_help\n"; #testing
>

Docs can be found at
http://search.cpan.org/~jv/Getopt-Long-2.34/lib/Getopt/Long.pm

And I have an example template that uses Getopt::Long and incorporates
Pod::Usage available here:

http://danconia.org/cgi-bin/request?handler=Content;content=StaticPage;label=getopt_long_template

<snip>

http://danconia.org

ok i have had a look at the cpan doc and made some adjustments...
it will take me a while to digest the pod bit though

also i am having a problem with the
#Data collections / inputfile part

" foreach $value (@dataFile) { # loop for each line/site in dataFile
                chomp $value ;
                @foundSegments=findVars($searchKey,$value);"

it is only storing the last value should i be doing something like

"@foundSegments[$i]=findVars($searchKey,$value);
$i++"

thank you for your help...

RichT

New code=======================================================
#!/usr/local/bin/perl
#

use strict;
use warnings;
use Getopt::Long;

Getopt::Long::config qw(no_ignore_case);

my
($inFile,$USAGE,$showKey,$site,$found,$foundKeys,@dataFile,@foundSegments,$value);

$USAGE = <<USAGETEXT;
usage: $0 ipaddress
          the following options are also availble
          [-inFile filename ] input filename
          [-findField fieldName ] this is the search key (default is
agentAddress)
          [-showFields field names ] feilds to output (default is
segment,agentAddress,communi
ty)
          [-listAllFields ] list avalible fields
          [-help] this help text
USAGETEXT
my $needHelp = '' ;
my $outputAllFields = '' ;
my $inputFile = '' ;
my $searchKey = "agentAddress";
my $outputFields = "segment,agentAddress,community";

GetOptions( "inFile=s" => \$inputFile,
                "findField=s" => \$searchKey,
                "showFields=s" => \$outputFields,
                "listAllFields" => \$outputAllFields,
                "help|h|H|?|HELP" => \$needHelp
        );

if ($needHelp) {print $USAGE; exit; } # check to see if this is a request
for help, if so prin
t USAGE

############################################################
# Data collections
# if we using an input file?
# else if we have found some values on the cl
# else quit
############################################################

if ($inputFile){ # find results if we have in -inFile

        open DFILE, "$inputFile" # open $inputFile and read in or die
                or die " could not open $inputFile";
        @dataFile = <DFILE>;
        close DFILE;

        foreach $value (@dataFile) { # loop for each line/site in dataFile
                chomp $value ;
                @foundSegments=findVars($searchKey,$value);
        }

} elsif ($ARGV[0]) { # read in value from comandline

        foreach $value ($ARGV[0]) { # loop for each line/site in dataFile
                @foundSegments=findVars($searchKey,$value);
        }

} else {print " could not find any input \n $USAGE "; exit; }

############################################################
# Data output
# if request for keys print all keys
# else print results
############################################################

if ($outputAllFields) {
 for $found ( @foundSegments ) {
        print "\n" ;
        for $foundKeys (keys %$found) {
                print "$foundKeys,";
        }
 }

} else {

 for $found ( @foundSegments ) {
        foreach $showKey (split /,/, $outputFields) {
                print "$found->{$showKey},";
        }
        print "\n";
 }

}

sub findVars {
############################################################
# Function Check Discover Results
# Parameters :
#
# Returns :
#
#
############################################################

my($findKey, $findValue, $segmentFieldKey, $segmentFieldValue,
%segmentFields, $nullVar, @foun
dSegments);

# read in Search Key and Value from parent NOTE make a check for this
$findKey=$_[0] || die "Missing Args $findKey $!" ;
$findValue=$_[1] || die "Missing Args $findValue $!" ;
chomp $findValue;
chomp $findKey;
#my $NH_HOME= $ENV[NH_HOME]; # point to the poller CFG file
my $NH_HOME= "."; # point to the poller CFG file NOTE this is temp for
testing use above line
 in live

local $/ = "}\n"; # set delimiter

open(POLLER, "$NH_HOME/poller.cfg") || die "can not open : $!";

#s/universalPollList \{//g;

while(<POLLER>) {
        next unless /^\s+segment/;
        s/\n\s+\}\n//g;
        s/["{]//g;
        foreach (split(/\n/)) {
                ($nullVar,$segmentFieldKey,$segmentFieldValue) =
split(/\s+/,$_,3);
                $segmentFields{ $segmentFieldKey } = $segmentFieldValue ;
        }
        if ( $segmentFields{$findKey} eq $findValue ) {

                push @foundSegments, {%segmentFields } ;
        }
        undef %segmentFields;
        my %segmentFields;
}
close POLLER;

return (@foundSegments); # return the IP and comunity string to main
ruteen
};



Relevant Pages

  • Re: Is there a real need to use keyword static with functions?
    ... simply don't declare their prototypes in .h file? ... usage of static functions. ... I've been programming for years and I never ... functions in different translation units that you want to be ...
    (comp.lang.c)
  • Re: King of Spices?
    ... But last w/e in Montreal I spent $8 for a handful of Madagascar pepper. ... How to declare? ... By price/availability or usage? ...
    (rec.food.cooking)
  • Re: King of Spices?
    ... But last w/e in Montreal I spent $8 for a handful of Madagascar pepper. ... How to declare? ... By price/availability or usage? ...
    (rec.food.cooking)
  • Re: Generics for Delphi
    ... The flexibility comes from being able to use the declaration without having to refer to a containing class. ... Usage of either my own or other generic types, ... It is the requirement to have to declare a bound type before using it that detracts from the flexibility of being able to bind and declare a variable at the same time, without littering your code with spurious type aliases. ...
    (borland.public.delphi.non-technical)
  • Re: The tense usage about "until recently"
    ... has been in Paris until yesterday." ... Based on what you say would "He has been ill until recently" even be proper ...
    (alt.usage.english)