Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Getopt question again

by convenientstore (Pilgrim)
on Jan 27, 2008 at 21:02 UTC ( [id://664560]=perlquestion: print w/replies, xml ) Need Help??

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

why does below not work?
cat ./arg_getopt.pl #/usr/bin/perl -w # # use strict; use diagnostics; use Getopt::Long; sub usage { print "this is wrong\n"; exit 0; } sub san_check_arg { usage () if $#ARGV != 0; #return if defined($$server; #return if defined($$proc); #return if defined($$prog); if defined($server) return ($server); } elsif (defined($proc && $prog)) { return ($proc,$prog); } else { usage(); } } my $val1 = eval { GetOptions ( "help" => sub { usage() }, "server=s" => \$server, "process=s" => \$proc, "program=s" => \$prog )}; die usage() if $@; #alone if (!$val1) { usage(); } my ($server, $proc, $prog); my @ar = san_check_arg(); #check users input sanity [root@localhost pra]# perl -c !$ perl -c ./arg_getopt.pl syntax error at ./arg_getopt.pl line 19, near "if defined" Global symbol "$server" requires explicit package name at ./arg_getopt +.pl line 19. Global symbol "$server" requires explicit package name at ./arg_getopt +.pl line 20. syntax error at ./arg_getopt.pl line 21, near "} elsif" ./arg_getopt.pl had compilation errors (#1) (F) Probably means you had a syntax error. Common reasons include +: A keyword is misspelled. A semicolon is missing. A comma is missing. An opening or closing parenthesis is missing. An opening or closing brace is missing. A closing quote is missing. Often there will be another error message associated with the synt +ax error giving more information. (Sometimes it helps to turn on -w. +) The error message itself often tells you where it was in the line +when it decided to give up. Sometimes the actual error is several toke +ns before this, because Perl is good at understanding random input. Occasionally the line number may be misleading, and once in a blue + moon the only way to figure out what's triggering the error is to call perl -c repeatedly, chopping away half the program each time to se +e if the error went away. Sort of the cybernetic version of S<20 questions>. Uncaught exception from user code: syntax error at ./arg_getopt.pl line 19, near "if defined" Global symbol "$server" requires explicit package name at ./arg_getopt +.pl line 19. Global symbol "$server" requires explicit package name at ./arg_getopt +.pl line 20. syntax error at ./arg_getopt.pl line 21, near "} elsif" ./arg_getopt.pl had compilation errors. at ./arg_getopt.pl line 21

Replies are listed 'Best First'.
Re: Getopt question again
by Joost (Canon) on Jan 27, 2008 at 21:20 UTC
    It doesn't work for a lot of reasons.
    if defined($server) return ($server); } ...
    should be
    if (defined($server)) { return ($server); } ...
    As the error message hints, you're missing a brace and two parentheses.

    Also, you're using the variables $server, $proc and $prog, but you don't declare them, which is an error (for good reasons) when you're using strict.

    Also, you're using eval { ... } around GetOptions, which doesn't throw an exception but returns false if your options don't parse, so just check the return value of GetOptions instead:

    GetOptions( ... ) or usage();
      I guess at compile time, they go through from top to bottom to see if things have been decalred... and when it finds those variable not defined, it throws an exception(?)... sa per you guys suggestion, I throw definition on top and it works.. thank you.
      !/usr/bin/perl -w use strict; use diagnostics; use Getopt::Long; my ($server, $proc, $prog); sub usage { print "this is wrong\n"; exit 0; } sub san_check_arg { usage () if $#ARGV != 0; if (defined($server)) { return ($server); } elsif (defined($proc && $prog)) { return ($proc,$prog); } else { usage(); } } my $val1 = eval { GetOptions ( "help" => sub { usage() }, "server=s" => \$server, "process=s" => \$proc, "program=s" => \$prog )}; die usage() if $@; #alone if (!$val1) { usage(); } my @ar = san_check_arg(); #check users input sanity print "@ar\n";
        My code is finished.. Please give me some advise on what I did wrong(it is working code)... or what i didn't do it correctly or efficiently.. thanks guys!
        #!/usr/bin/perl -w use strict; use diagnostics; use Getopt::Long; my @servers = qw(server1 sever2 server3 sever4); my $help = 0; my ($callid,$dn,$inbound); sub usage { print <<"NL"; usage: $0 sip/h323 [-h] -c <callid> -d <dialed number> -i <in +bound id> syntax: --help -h prints this help text and exists --callid -c allows searching in SBC by callid ; will output + of calls and siptool -v --dn -d allows searching in SBC by dialed number; will +output last found call from each server if applicable --inbound -i allows searching in SBC by inbound ID ; will ou +tput last found call from each server if applicable --time -t { Future implementation } allows searching callid/dn/inbound in SBC by ho +ur ; in 00:00 to 23:59 format To search between 2PM and 3PM, 14:00-15:00 example: $0 sip --dn 12125551212 or $0 sip -d 12125551212 $0 h323 --callid 12341234 or $0 h323 -c 12341234 $0 sip --inbound 333333 or $0 sip -i 33333 NL exit 0; } sub san_check_arg { usage () if $#ARGV != 0; usage () if $ARGV[0] !~ /\bsip\b|\bh323\b/i; return ('CALLID', $callid) if defined($callid); return ('DN',$dn) if defined($dn); return ('INBOUND ID',$inbound) if defined($inbound); } sub wr_temp { my $wr_t = shift; open FF, ">/tmp/file_123456"; print FF "$wr_t"; close FF; } sub proc_it { my $proc = shift; my @meth = $_[0] eq 'CALLID' ? 'CALLID' : $_[0] eq 'INBOUND ID' ? 'INBOUND ID' : ''; my $file_f; my ($h323_r,$sip_r); if ("$proc" eq 'h323') { for (@servers) { print "\t going through server $_\n"; if ($meth[0] eq 'INBOUND ID') { $h323_r = `ssh "$_" grep "'|$_[1]|'" /process/hom +e/h323cc.[12]/paccalls | tail -1`; next; } else { $h323_r = `ssh "$_" grep "$_[1]" /process/home/h3 +23cc.[12]/paccalls | tail -1`; } if (($h323_r) && ($meth[0] eq 'CALLID')) { $file_f = wr_temp("$h323_r"); system "/n2p/bin/siptool -v /tmp/file_123456"; last; } elsif ($h323_r) { print "Foudn at least one call at $_\n"; print "$h323_r\n"; next; } } } elsif ("$proc" eq 'sip') { for (@servers) { print "\t going through server $_\n"; if ($meth[0] eq 'INBOUND ID') { #my $yahoo = "\|$_[1]\|"; $sip_r = `ssh "$_" grep "'|$_[1]|'" /process/home +/sipcc.[12]/sipcalls | tail -1`; print "$sip_r\n"; next; } else { $sip_r = `ssh "$_" grep "$_[1]" /process/home/sip +cc.[12]/sipcalls | tail -1`; } if (($sip_r) && ($meth[0] eq 'CALLID')) { $file_f = wr_temp("$sip_r"); system("/n2p/bin/siptool -v /tmp/file_123456"); last; } elsif ($sip_r) { print "Found at least one call at $_\n"; print "$sip_r\n"; } } } else { print "Do no understand your input\n"; usage(); } } my $val1 = eval { GetOptions ( "help" => sub { usage() }, "callid=s" => \$callid, "dn=s" => \$dn, "inbound=s" => \$inbound )}; die usage() if $@; #alone if (!$val1) { usage(); } my @ar = san_check_arg(); #check users input sanity proc_it(@ARGV,@ar);
Re: Getopt question again
by apl (Monsignor) on Jan 27, 2008 at 21:17 UTC
    You haven't defined the variables $server, $proc and $prog. Try adding my statements at the top of your program.
Re: Getopt question again
by metaperl (Curate) on Jan 31, 2008 at 15:25 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://664560]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-24 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found