Home > Archive > PERL Beginners > September 2004 > GetOptions problems
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
GetOptions problems
|
|
|
| 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,$f
oundKeys,@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-L.../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...t_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,$valu
e);"
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,$f
oundKeys,@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,$valu
e);
}
} elsif ($ARGV[0]) { # read in value from comandline
foreach $value ($ARGV[0]) { # loop for each line/site in dataFile
@foundSegments=findVars($searchKey,$valu
e);
}
} 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,$segmentField
Value) =
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
};
| |
| Wiggins d Anconia 2004-09-30, 3:57 pm |
| > ok ill send again from the correct address...
>
> 'reason'.
> ...
>
> a look at:
>
> yes i know i can not spell, im a dislexic moron
> thank you i will read the page from the link.
>
Dyslexia does not make one a moron, though it must make programming
significantly more challenging. I thought something might be up, which
is why my comments were made, but not intentionally hurtful. Sometimes
people really are that lazy....
>
>
>
($inFile,$USAGE,$showKey,$site,$found,$f
oundKeys,@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?
Yes. There is no hard rule, the variable should be declared at the
tightest scope possible. So if the variable is only needed in a loop,
then declare it in the loop. If it needs a less specific scope, then
scope it where you need to. For more on scoping check out:
http://perl.plover.com/FAQs/Namespaces.html
>
>
> 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
>
Yes the first is a reference about language features rather than a good
source of best practices, the second is excellent for teaching, but as
such tends to keep snippets short in the interest of teaching the
specific subject at hand. Both are excellent for their intended purpose,
which is not always developing full programs.
>
> 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>.
>
>
>
>
> Docs can be found at
> http://search.cpan.org/~jv/Getopt-L.../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...t_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
>
They often do, being able to read the docs and pull from the API is
probably the most important skill in Perl (programming in general).
>
> 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,$valu
e);"
>
Generally when looping over a file you should use a C<while> construct
instead to reduce memory overhead.
>
> it is only storing the last value should i be doing something like
>
> " @foundSegments[$i]=findVars($searchKey,$
value);
> $i++"
This would be a very C way of doing it, which is ok, but in Perl we are
provided with the C<push> function, which is signifcantly easier,
perldoc -f push
push @foundSegments, findVars($searchKey, $value);
>
> thank you for your help...
>
> RichT
Keep at it...
http://danconia.org
>
> New code====================================
===================
<snip>
| |
| Chap Harrison 2004-09-30, 3:57 pm |
|
On Sep 30, 2004, at 10:35 AM, cats wrote:
> yes i know i can not spell, im a dislexic moron
>
If you're like other dyslexics I've known, you're probably (a) not a
moron and (b) exceptionally brilliant in other areas.
Thanks for the reminder.
|
|
|
|
|