Re: Getopt::Long
Hi, Sorry for one typo. Read GetOptions in place of Getoptions . Regards, Rajesh On Wed, May 16, 2012 at 2:39 PM, Rajesh Saha rajeshsaha...@gmail.comwrote: Hi, In my program, I am using the module Getopt::Long My intension is that if the user chooses --help, the program should show the help message without bothering whether other options has been used or not. My program (say, test.pl) : use Getopt::Long my $abc; my $help; Getoptions ( abc=s = \$abc, help = \$help ); if ($help) { print This is help message\n ; exit ;} if ($abc) { print You have choosen \abc\ as $abc }; If I run this: 1.test.pl -help Output: This is help message 2. test.pl -abc ABC Output : You have choosen abc as ABC 3. test.pl -abc -help Output: You have choosen abc as -help Out the above results 1 2 are fine, but 3 is not as I want. I understand what's happening here [$abc is assigned as -help], but don't have idea how to implement it properly so that in the 3rd it would print like This is help message . Can anybody help please ? Thanks n regards Rajesh Saha
RE: Getopt::Long
Hi, Your problem is that Getopt::Long is consuming your -help as the argument to -abc before it can be recognised. You might be able to do something with the other Getopt::* modules, but the following piece of code will do what you want if you really need it: use List::MoreUtils qw(any); use Getopt::Long; my $abc; my $help; if ( any { $_ eq '-help' } @ARGV ) { $help = 1; } GetOptions ( abc=s = \$abc, help = \$help ); if ( $help ) { print This is help message\n ; exit ;} if ($abc) { print You have choosen \abc\ as $abc }; # == If you don't have List::MoreUtils installed and can't install it, replace the 'if ( any...)' block with: foreach my $arg (@ARGV) { if ( $arg eq '-help' ) { $help = 1; last; } } Cheers, Michael -- Michael Brader Senior Software Engineer - Billing Techops - Softdev Internode http://www.internode.on.net/ From: Rajesh Saha [rajeshsaha...@gmail.com] Sent: Wednesday, 16 May 2012 6:39 PM To: beginners@perl.org Subject: Getopt::Long Hi, In my program, I am using the module Getopt::Long My intension is that if the user chooses --help, the program should show the help message without bothering whether other options has been used or not. My program (say, test.pl) : use Getopt::Long my $abc; my $help; Getoptions ( abc=s = \$abc, help = \$help ); if ($help) { print This is help message\n ; exit ;} if ($abc) { print You have choosen \abc\ as $abc }; If I run this: 1.test.pl -help Output: This is help message 2. test.pl -abc ABC Output : You have choosen abc as ABC 3. test.pl -abc -help Output: You have choosen abc as -help Out the above results 1 2 are fine, but 3 is not as I want. I understand what's happening here [$abc is assigned as -help], but don't have idea how to implement it properly so that in the 3rd it would print like This is help message . Can anybody help please ? Thanks n regards Rajesh Saha -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Getopt::Long
On Wed, May 16, 2012 at 10:56:59AM +, Michael Brader wrote: Hi, Hello: Your problem is that Getopt::Long is consuming your -help as the argument to -abc before it can be recognised. You might be able to do something with the other Getopt::* modules, but the following piece of code will do what you want if you really need it: use List::MoreUtils qw(any); use Getopt::Long; my $abc; my $help; if ( any { $_ eq '-help' } @ARGV ) { $help = 1; Note that this doesn't account for '--help'. :) Besides, what if '--help' happens to be a valid option argument? This would prevent that from working. I think it's best to just keep with the module's behavior and work normally. If a user wants --help then they should generally /just/ say --help. If you want to do some kind of option-specific help then specify --help before --the-option: my-command --help --the-option Or use non-option subcommands: my-command subcommand --help Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Getopt::Long
Hi Rajesh, On Wed, 16 May 2012 16:11:13 +0530 Rajesh Saha rajeshsaha...@gmail.com wrote: Hi, Sorry for one typo. Read GetOptions in place of Getoptions . Regards, Rajesh Here is how I do it: #! /usr/bin/perl use strict; use warnings; use Getopt::Long; my $dry_run = 0; GetOptions('version|V' = sub { version(); exit; }, 'dry-run|n' = \$dry_run, 'help|h' = sub { usage(); }, ) or usage(); exit; sub version { print $0: Version 0.1\n; exit; } sub usage { print END_USAGE Usage: $0 [ Options ] This script does important work Options: -n, --dry-run Simulates rsync commands -V, --version Print version -h, --help This help END_USAGE ; exit; return; } -- Manfred -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Getopt::Long in perl
On 7/29/10 Thu Jul 29, 2010 1:17 AM, Sooraj S soorajspadmanab...@gmail.com scribbled: Hi, I am using Getopt::Long to accept the command line arguments. Logic - I have two mandatory options. 1.mod1_num 2.mod2_num Both of them can accept either a 3digit number or another parameter preserve which inturn accepts a 3digit number. eg: my_script -mod1_num 123 -mod2_num 234 my_script -mod1_num -preserve 123 mod2_num 234 If preserve is used with mod1_num it cannot be used with mod2_num and viceversa. How to do this with GetOptions? Please help me. The logic you want is unusual and will not be supported by Getopt::Long. I believe you have two choices: 1) change the logic, 2) do the processing yourself. 1. For more normal processing, combine the two options '-mod1_num -preserve' into one (e.g., '-mod1_num_preserve') followed by the numerical argument. This follows the normal logic supported by Getopt::Long. 2. If you can't change the logic, then process the command-line arguments yourself with something like this (untested): for( my $i = 0; $i @ARGV; $i++ ) { if( $ARGV[$i] eq '-mod1_num' ) { if( $ARGV[$i+1] eq '-preserve ) { $num = $ARGV[$i+2]; $i += 2; }else{ $num = $ARGV[++$i]; } }elsif( $ARGV[$i] eq '-mod2_num' ) { ... } } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: getopt::long
Hi, use strict; use warnings; use Getopt::Long; my %hash; GetOptions(\%hash, first=s, second|s=s, third=i); print $hash{first}, $hash{second}, $hash{third}, \n; -- Octavian - Original Message - From: Unknown User knowsuperunkn...@gmail.com To: beginners beginners@perl.org Sent: Tuesday, July 06, 2010 7:49 AM Subject: getopt::long um, I am pretty sure getopt::long keeps all input data in a hash, does anybody know whether i can use that hash itself in my code, and if so, how? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ __ Information from ESET NOD32 Antivirus, version of virus signature database 5254 (20100706) __ The message was checked by ESET NOD32 Antivirus. http://www.eset.com __ Information from ESET NOD32 Antivirus, version of virus signature database 5254 (20100706) __ The message was checked by ESET NOD32 Antivirus. http://www.eset.com -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Getopt::Long
On Thu, Jun 17, 2010 at 02:36, Unknown User knowsuperunkn...@gmail.com wrote: I have the following code: GetOptions( n|name=s = \$name, a|age=i = \$age, s|sex=s = \$sex, ) || die Bad options\n;; What i expected this code to do is to die if a bad option was given, say -s without an arguement, as in ./myprog -n name -s -a 20 However, it does not do that. What would be the correct method to die if one of the options is not complete? GetOptions should print a warning and return a false value in that case. What version of Perl and Getopt::Long are you using? Try running the following code. If it does not die, then there is a problem outside of your code. #!/usr/bin/perl use strict; use warnings; use Getopt::Long; for my $args ([qw/-n a/], [-n]) { @ARGV = @$args; GetOptions( n|name=s = \my $name, a|age=s = \my $age, s|sex=s = \my $sex ) or die bad options\n; print [$name] @ARGV\n; } -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Getopt::Long
On 10-06-17 02:36 AM, Unknown User wrote: I have the following code: GetOptions( n|name=s = \$name, a|age=i = \$age, s|sex=s = \$sex, ) || die Bad options\n;; GetOptions( name=s =\$name, age=i = \$age, sex=s = \$sex, ) || die Bad options\n; # GetOptions automatic determines which option by # the minimum leading letters needed to # distinguish them. What i expected this code to do is to die if a bad option was given, say -s without an arguement, as in ./myprog -n name -s -a 20 However, it does not do that. What would be the correct method to die if one of the options is not complete? But they are complete. 'name' is placed in $name, '-a' is placed in $sex, and @ARGV is left with ( '20' ). -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. The secret to great software: Fail early often. Eliminate software piracy: use only FLOSS. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Getopt::Long
On Thu, Jun 17, 2010 at 07:50, Shawn H Corey shawnhco...@gmail.com wrote: On 10-06-17 02:36 AM, Unknown User wrote: I have the following code: GetOptions( n|name=s = \$name, a|age=i = \$age, s|sex=s = \$sex, ) || die Bad options\n;; GetOptions( name=s = \$name, age=i = \$age, sex=s = \$sex, ) || die Bad options\n; # GetOptions automatic determines which option by # the minimum leading letters needed to # distinguish them. What i expected this code to do is to die if a bad option was given, say -s without an arguement, as in ./myprog -n name -s -a 20 However, it does not do that. What would be the correct method to die if one of the options is not complete? But they are complete. 'name' is placed in $name, '-a' is placed in $sex, and @ARGV is left with ( '20' ). snip Good point, try this: #!/usr/bin/perl use strict; use warnings; use Getopt::Long; #to test, run with no args unless (@ARGV) { for my $args ( [ qw/-n name -a 5 -s M/ ], [ qw/-n -a 5 -s M/ ], [ qw/-n name -a a -s M/ ], [ qw/-n name -a -s M/ ], [ qw/-n name -a 5 -s W/ ], [ qw/-n name/ ], ) { print $0 @$args\n; system $^X, $0, @$args; print - x 5, \n\n; } print $0\n; } GetOptions( n|name=s = \(my $name = DEFAULT), a|age=n = \(my $age = DEFAULT), s|sex=s = \(my $sex = DEFAULT), ) or die bad options\n; my $errfmt = qq{Value %s invalid for option %s (%s)\n}; die sprintf $errfmt, $sex, -s, M or F expected if length $sex and $sex =~ /^[^MF]$/; die sprintf $errfmt, $name, -n, first character can't be a hyphen if length $name and $name =~ /^-/; print [$name] [$age] [$sex] @ARGV\n; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Getopt::Long
On Thu, Jun 17, 2010 at 6:50 AM, Shawn H Corey shawnhco...@gmail.comwrote: On 10-06-17 02:36 AM, Unknown User wrote: I have the following code: GetOptions( n|name=s = \$name, a|age=i = \$age, s|sex=s = \$sex, ) || die Bad options\n;; But they are complete. 'name' is placed in $name, '-a' is placed in $sex, and @ARGV is left with ( '20' ). Technically, -s does have a value: -a. GetOptions is treating -a as the value for -s because the string -a immediately follows the option -s. You could add validation on the value of -s. For example... GetOptions( n|name=s = \$name, a|age=i = \$age, s|sex=s = \$sex, ) || die Bad options\n;; if ($sex !~ m/m(ale)?|f(emale)?/i) { die Invalid gender $sex\n; } -- Robert Wohlfarth
Re: Getopt::Long and Log::StdLog problem
Rob Dixon wrote: rafailowski wrote: I have a problem with Getopt::Long and Log::StdLog. An example script, i always have the following error : Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57 level = $cmd_args_ref-{log_level} is always undef(???) but print $cmd_args_ref-{log_level}; return the good value I try to fix this with BEGIN block (around GetOptions) with no success Is there a way to solve this (BEGIN INIT block?) #!/usr/bin/perl use warnings; use strict; use Getopt::Long; my %cmd_args = (); my $cmd_args_ref = \%cmd_args; GetOptions( log-level=s = \$cmd_args{log_level}, ); sub log_format { my ($date, $pid, $level, @message) = @_; return [$date][$level]: . join(q{}, @message); }; use Log::StdLog { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, }; print $cmd_args_ref-{log_level}; Calling GetOptions in a BEGIN block will work fine, but the initialisation of $cmd_args_ref must be done at the same time, otherwise it won't be set up when Log::StdLog comes to be imported. The code below should do what you want. HTH, Rob use strict; use warnings; use Getopt::Long; my (%cmd_args, $cmd_args_ref); BEGIN { GetOptions( 'log-level=s' = \$cmd_args{log_level}, ); $cmd_args_ref = \%cmd_args; } sub log_format { my ($date, $pid, $level, @message) = @_; local $ = q{}; return [$date][$level]: @message; } use Log::StdLog { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, }; print STDLOG user = HELLO\n; Ohhh, it works fine! thanks for your help... rafailow -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
Mr. Shawn H. Corey wrote: On Mon, 2008-08-04 at 20:47 -0400, Mr. Shawn H. Corey wrote: On Tue, 2008-08-05 at 01:33 +0200, rafailowski wrote: Yes, without argument, the error is normal : $ perl test.pl Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. Use of uninitialized value in string at test.pl line 25. But with an argument like this : $ perl test.pl --log-level=info Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. info level = $cmd_args_ref-{log_level} stay undef and print $cmd_args_ref-{log_level}; return the good value. I don't understand why in : use Log::StdLog {...}, the value of $cmd_args_ref-{log_level} stay always undef??? Sorry, I was thinking about the problem correctly. Try: #!/usr/bin/perl use Getopt::Long; use Log::StdLog; my %cmd_args = (); my $cmd_args_ref = \%cmd_args; GetOptions( log-level=s = \$cmd_args{log_level}, ); sub log_format { my ($date, $pid, $level, @message) = @_; return [$date][$level]: . join(q{}, @message); }; Log::Stdlog::import( { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, } ); Oops, try this instead: Log::Stdlog::import { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, }; print $cmd_args_ref-{log_level}; __END__ This re-runs the import function after the program has started. -- Just my 0.0002 million dollars worth, Shawn Where there's duct tape, there's hope. Perl is the duct tape of the Internet. Hassan Schroeder, Sun's first webmaster Thx but adding __END__ return me this error, anyway the problem is solve with a BEGIN block (cf.Rob Dixon). $ perl test.pl --log-level=debug Name main::STDLOG used only once: possible typo at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 51 Thx again. rafailow. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
On Tue, 2008-08-05 at 08:57 +0200, rafailowski wrote: Thx but adding __END__ return me this error, anyway the problem is solve with a BEGIN block (cf.Rob Dixon). $ perl test.pl --log-level=debug Name main::STDLOG used only once: possible typo at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 51 Thx again. rafailow. Adding __END__ will not create this warning. It is because no other statement has STDLOG in it. When you use it, this warning shall go away. Example: print {*STDLOG} info printing to STDLOG\n; -- Just my 0.0002 million dollars worth, Shawn Where there's duct tape, there's hope. Perl is the duct tape of the Internet. Hassan Schroeder, Sun's first webmaster -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
Mr. Shawn H. Corey wrote: On Tue, 2008-08-05 at 08:57 +0200, rafailowski wrote: Thx but adding __END__ return me this error, anyway the problem is solve with a BEGIN block (cf.Rob Dixon). $ perl test.pl --log-level=debug Name main::STDLOG used only once: possible typo at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 51 Thx again. rafailow. Adding __END__ will not create this warning. It is because no other statement has STDLOG in it. When you use it, this warning shall go away. Example: print {*STDLOG} info printing to STDLOG\n; Yes you're right, it' works fine now. Thx for your help. rafailow. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
On Tue, 2008-08-05 at 01:05 +0200, rafailowski wrote: Hi all, I have a problem with Getopt::Long and Log::StdLog. An example script, i always have the following error : Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57 level = $cmd_args_ref-{log_level} is always undef(???) but print $cmd_args_ref-{log_level}; return the good value $cmd_args_ref-{log_level} is undef unless you set with a command-line argument. I try to fix this with BEGIN block (around GetOptions) with no success Is there a way to solve this (BEGIN INIT block?) #!/usr/bin/perl use warnings; use strict; use Getopt::Long; my %cmd_args = (); my $cmd_args_ref = \%cmd_args; GetOptions( log-level=s = \$cmd_args{log_level}, ); sub log_format { my ($date, $pid, $level, @message) = @_; return [$date][$level]: . join(q{}, @message); }; use Log::StdLog { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, level = ( $cmd_args_ref-{log_level} || 'none' ), }; print $cmd_args_ref-{log_level}; Thx in advance, rafailow. -- Just my 0.0002 million dollars worth, Shawn Where there's duct tape, there's hope. Perl is the duct tape of the Internet. Hassan Schroeder, Sun's first webmaster -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
Mr. Shawn H. Corey wrote: On Tue, 2008-08-05 at 01:05 +0200, rafailowski wrote: Hi all, I have a problem with Getopt::Long and Log::StdLog. An example script, i always have the following error : Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57 level = $cmd_args_ref-{log_level} is always undef(???) but print $cmd_args_ref-{log_level}; return the good value $cmd_args_ref-{log_level} is undef unless you set with a command-line argument. I try to fix this with BEGIN block (around GetOptions) with no success Is there a way to solve this (BEGIN INIT block?) #!/usr/bin/perl use warnings; use strict; use Getopt::Long; my %cmd_args = (); my $cmd_args_ref = \%cmd_args; GetOptions( log-level=s = \$cmd_args{log_level}, ); sub log_format { my ($date, $pid, $level, @message) = @_; return [$date][$level]: . join(q{}, @message); }; use Log::StdLog { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, level = ( $cmd_args_ref-{log_level} || 'none' ), }; print $cmd_args_ref-{log_level}; Thx in advance, rafailow. Yes, without argument, the error is normal : $ perl test.pl Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. Use of uninitialized value in string at test.pl line 25. But with an argument like this : $ perl test.pl --log-level=info Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. info level = $cmd_args_ref-{log_level} stay undef and print $cmd_args_ref-{log_level}; return the good value. I don't understand why in : use Log::StdLog {...}, the value of $cmd_args_ref-{log_level} stay always undef??? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
On Tue, 2008-08-05 at 01:33 +0200, rafailowski wrote: Yes, without argument, the error is normal : $ perl test.pl Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. Use of uninitialized value in string at test.pl line 25. But with an argument like this : $ perl test.pl --log-level=info Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. info level = $cmd_args_ref-{log_level} stay undef and print $cmd_args_ref-{log_level}; return the good value. I don't understand why in : use Log::StdLog {...}, the value of $cmd_args_ref-{log_level} stay always undef??? Sorry, I was thinking about the problem correctly. Try: #!/usr/bin/perl use Getopt::Long; use Log::StdLog; my %cmd_args = (); my $cmd_args_ref = \%cmd_args; GetOptions( log-level=s = \$cmd_args{log_level}, ); sub log_format { my ($date, $pid, $level, @message) = @_; return [$date][$level]: . join(q{}, @message); }; Log::Stdlog::import( { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, } ); print $cmd_args_ref-{log_level}; __END__ This re-runs the import function after the program has started. -- Just my 0.0002 million dollars worth, Shawn Where there's duct tape, there's hope. Perl is the duct tape of the Internet. Hassan Schroeder, Sun's first webmaster -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
rafailowski wrote: I have a problem with Getopt::Long and Log::StdLog. An example script, i always have the following error : Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57 level = $cmd_args_ref-{log_level} is always undef(???) but print $cmd_args_ref-{log_level}; return the good value I try to fix this with BEGIN block (around GetOptions) with no success Is there a way to solve this (BEGIN INIT block?) #!/usr/bin/perl use warnings; use strict; use Getopt::Long; my %cmd_args = (); my $cmd_args_ref = \%cmd_args; GetOptions( log-level=s = \$cmd_args{log_level}, ); sub log_format { my ($date, $pid, $level, @message) = @_; return [$date][$level]: . join(q{}, @message); }; use Log::StdLog { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, }; print $cmd_args_ref-{log_level}; Calling GetOptions in a BEGIN block will work fine, but the initialisation of $cmd_args_ref must be done at the same time, otherwise it won't be set up when Log::StdLog comes to be imported. The code below should do what you want. HTH, Rob use strict; use warnings; use Getopt::Long; my (%cmd_args, $cmd_args_ref); BEGIN { GetOptions( 'log-level=s' = \$cmd_args{log_level}, ); $cmd_args_ref = \%cmd_args; } sub log_format { my ($date, $pid, $level, @message) = @_; local $ = q{}; return [$date][$level]: @message; } use Log::StdLog { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, }; print STDLOG user = HELLO\n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long and Log::StdLog problem
On Mon, 2008-08-04 at 20:47 -0400, Mr. Shawn H. Corey wrote: On Tue, 2008-08-05 at 01:33 +0200, rafailowski wrote: Yes, without argument, the error is normal : $ perl test.pl Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. Use of uninitialized value in string at test.pl line 25. But with an argument like this : $ perl test.pl --log-level=info Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57. info level = $cmd_args_ref-{log_level} stay undef and print $cmd_args_ref-{log_level}; return the good value. I don't understand why in : use Log::StdLog {...}, the value of $cmd_args_ref-{log_level} stay always undef??? Sorry, I was thinking about the problem correctly. Try: #!/usr/bin/perl use Getopt::Long; use Log::StdLog; my %cmd_args = (); my $cmd_args_ref = \%cmd_args; GetOptions( log-level=s = \$cmd_args{log_level}, ); sub log_format { my ($date, $pid, $level, @message) = @_; return [$date][$level]: . join(q{}, @message); }; Log::Stdlog::import( { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, } ); Oops, try this instead: Log::Stdlog::import { format = \log_format, file = $0.log, level = $cmd_args_ref-{log_level}, }; print $cmd_args_ref-{log_level}; __END__ This re-runs the import function after the program has started. -- Just my 0.0002 million dollars worth, Shawn Where there's duct tape, there's hope. Perl is the duct tape of the Internet. Hassan Schroeder, Sun's first webmaster -- Just my 0.0002 million dollars worth, Shawn Where there's duct tape, there's hope. Perl is the duct tape of the Internet. Hassan Schroeder, Sun's first webmaster -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Getopt::Long
Chris Knipe wrote: So, this is more of a block question I think, but how I can get the above example to show the help screen FIRST, and THEN complain about the missing value for -s Why? Here's an example of how to do it: #!/usr/bin/perl use strict; use warnings; use File::Basename; use File::Copy 'copy'; my $TmpDir = '/tmp'; # Change to 'C:\TEMP' for MS DOS my $Self = basename( $0 ); my $SaveOutFile = $TmpDir/$Self.$$.out; my $SaveErrFile = $TmpDir/$Self.$$.err; open SAVE_OUT, STDOUT; open SAVE_ERR, STDERR; my $umask = umask 0600; open STDERR, $SaveErrFile; open STDOUT, $SaveOutFile; umask $umask; my $string = ''; unless( GetOptions( 'help' = \ShowHelp, 'b=s' = \$string, )){ open STDOUT, SAVE_OUT; open STDERR, SAVE_ERR; copy( $SaveErrFile, STDERR ); copy( $SaveOutFile, STDOUT ); ShowHelp; } if( $string ){ open STDOUT, SAVE_OUT; open STDERR, SAVE_ERR; copy( $SaveErrFile, STDERR ); copy( $SaveOutFile, STDOUT ); # Place string munging here }else{ open STDOUT, SAVE_OUT; open STDERR, SAVE_ERR; ShowHelp; copy( $SaveErrFile, STDERR ); copy( $SaveOutFile, STDOUT ); } __END__ -- Just my 0.0002 million dollars worth, --- Shawn Probability is now one. Any problems that are left are your own. SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Getopt::Long
Chris Knipe wrote: Hi all, Just a quick question and a couple of lines of really simple code use Getopt::Long; ... GetOptions ('h' = \$h, 'b=s' = \$s ); Sub ShowHelp() { That should be sub ShowHelp { Perl isn't VB :) print this is help } Sub DoSomethingWithString() { ... } If ($s) { DoSomethingWithString(); } else { ShowHelp(); } Right. Now, whilst the above is not perhaps 100% correct, it goes about a generality of Getopt::Long. If I now run the application, ./blah.pl -h - I get the help screen ./blah.pl -s - I get a error, complaining that -s requires a value, and THEN the help screen. ./blah.pl -s s - Everything is fine. So, this is more of a block question I think, but how I can get the above example to show the help screen FIRST, and THEN complain about the missing value for -s Well, this is a little tricky, because the complaint is issued by the GetOptions() call. I would usually do something like: # just show the complaint GetOptions(...blah...) or exit 1; or # show the complaint, then the help GetOptions(...blah...) or ShowHelp(), exit 1; In order to show the help before the complaint, you need to capture the complaint with $SIG{__WARN__} something like this: my $msg = ''; do { local $SIG{__WARN__} = sub { $msg .= shift }; GetOptions('h' = \$h, 'b=s' = \$s ); } or ShowHelp(), die $msg; I would suggest sending the help text to STDERR, or unbuffering STDOUT so that you are sure to get the help text shown before the complaint. Also, you might look at Pod::Usage for an approach to handling the help text. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Getopt::Long
An excerpt from the documentation: my $tag = ''; # option variable with default value GetOptions ('tag=s' = \$tag); In the option specification, the option name is followed by an equals sign = and the letter s. The equals sign indicates that this option requires a value. The letter s indicates that this value is an arbitrary string. Other possible value types are i for integer values... -Original Message- From: Chris Knipe [mailto:[EMAIL PROTECTED] Sent: Monday, November 07, 2005 7:38 PM To: beginners@perl.org Subject: Getopt::Long Hi all, Just a quick question and a couple of lines of really simple code use Getopt::Long; ... GetOptions ('h' = \$h, 'b=s' = \$s ); Sub ShowHelp() { print this is help } Sub DoSomethingWithString() { ... } If ($s) { DoSomethingWithString(); } else { ShowHelp(); } Right. Now, whilst the above is not perhaps 100% correct, it goes about a generality of Getopt::Long. If I now run the application, ./blah.pl -h - I get the help screen ./blah.pl -s - I get a error, complaining that -s requires a value, and THEN the help screen. ./blah.pl -s s - Everything is fine. So, this is more of a block question I think, but how I can get the above example to show the help screen FIRST, and THEN complain about the missing value for -s Thanks, -- Regards, Chris. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: getopt long behaviour
On Fri, 9 Sep 2005, Manish Sapariya wrote: Shouldn't following snippet throw error message when it is called without any parameter? I can't see any error messsage, why? Because this didn't have an error. If you want it to quit, force it to quit. #!/usr/bin/perl use Getopt::Long; GetOptions(verbose = \$verbose, debug= \$debug, output=s = \$output) || print No inputs specified\n; die No output specified unless defined $output; print Executed successfully\n; -- Chris Devers u+ûQ!VévØ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: getopt long behaviour
Oh, my understanding is that when I specify output=s its a mandatory parameter and if not specified on command line, GetOptions should return error. Am i missing something? Thanks, Manish On 09/09/2005 05:56 PM, Chris Devers wrote: On Fri, 9 Sep 2005, Manish Sapariya wrote: Shouldn't following snippet throw error message when it is called without any parameter? I can't see any error messsage, why? Because this didn't have an error. If you want it to quit, force it to quit. #!/usr/bin/perl use Getopt::Long; GetOptions(verbose = \$verbose, debug= \$debug, output=s = \$output) || print No inputs specified\n; die No output specified unless defined $output; print Executed successfully\n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: getopt long behaviour
Oh, my understanding is that when I specify output=s its a mandatory parameter and if not specified on command line, GetOptions should return error. Am i missing something? Thanks, Manish On 09/09/2005 05:56 PM, Chris Devers wrote: On Fri, 9 Sep 2005, Manish Sapariya wrote: Shouldn't following snippet throw error message when it is called without any parameter? I can't see any error messsage, why? Because this didn't have an error. If you want it to quit, force it to quit. #!/usr/bin/perl use Getopt::Long; GetOptions(verbose = \$verbose, debug= \$debug, output=s = \$output) || print No inputs specified\n; die No output specified unless defined $output; print Executed successfully\n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: GetOpt::Long
Hi Wim Hi Bob, You gave me the following answer earlier: sub foo { local @ARGV = @_; GetOptions(...blah...); ... } If I run it as a seperate script, commenting out the (local @ARGV = @_;) line, then it works. But it does not work when called as a subroutine from within the main script. Can you be more specific about does not work and called ... from within the main script? Your code below is not a functional example, so I made some changes (especially inserting use strict and use warnings which alway gives good hints :-), and it works - but maybe in the sense you mentioned that it works for you too: === begin script (test3.pl) === use strict; use warnings; use Getopt::Long; sub notify_email { local @ARGV = @_; my (@recipients, @subject, @body); my %conf=(smtp='whatever', smtpsender='something'); $ENV{NTsendmail} = $conf{'smtp'}; my $sender = $conf{'smtpsender'}; GetOptions (r=s = [EMAIL PROTECTED], s=s = [EMAIL PROTECTED], b=s = [EMAIL PROTECTED]); my $subject= join( , @subject); my $body = join( , @body); foreach my $recipient (@recipients) { print $sender, $recipient, $subject, $body\n; #$mail = new NTsendmail; #$mail-send($sender, $recipient, $subject, $body); } } notify_email (@ARGV); === end script === This prints: something, , sss, I have also tried (my @ARGV = @_;). Any idea why? Here is my code now: = # Notify recipients via SMTP (email) # Usage: notify_email -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -s Subject line -b Message body sub notify_email { local @ARGV = @_; # Get the sub's params into the master param array for GetOpt::Long $ENV{NTsendmail} = $conf{'smtp'}; $sender = $conf{'smtpsender'}; GetOptions (r=s = [EMAIL PROTECTED], # -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] s=s = [EMAIL PROTECTED], # -s This is the subject line... b=s = [EMAIL PROTECTED]);# -b This is the message body... $subject= join( , @subject); $body = join( , @body); foreach $recipient (@recipients) { print $sender, $recipient, $subject, $body\n; $mail = new NTsendmail; $mail-send($sender, $recipient, $subject, $body); } } = ___ ___ Standard Bank Disclaimer and Confidentiality Note [...] hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: GetOpt::Long
Hi Bob, (Offer Kaye, thanks for the reply as well!) I used the method below (local @ARGV = @_;) to get the values of @_ into @ARGV for the use of Getopt::Long. It appears to be working fine like that. I now have another problem with, it appears, syntax. The IF statement is part of a block in the switch statement, but there is nothing wrong with the switch statement itself, as there are many identical if statements there. If I comment out this if statement, I don't get the error anymore. An clues as to the correct syntax??? I run the subroutine as follows: if ($conf{'pnl_check_for_analytics_email'} =~ 'ON') { notify_email -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -s Subject line -b Message body }; But I get the following error when it executes: Bad switch statement (problem in the code block?) Please see the code below: == # Notify recipients via SMTP (email) # Usage: notify_email -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -s Subject line -b Message body sub notify_email { local @ARGV = @_; # Get the sub's params into the master param array for GetOpt::Long $ENV{NTsendmail} = $conf{'smtp'}; $sender = $conf{'smtpsender'}; my @recipients; my @subject; my $body; GetOptions (r=s = [EMAIL PROTECTED], # -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] s=s = $subject, # -s This is the subject line... b=s = $body);# -b This is the message body... [EMAIL PROTECTED] = split(/,/,join(',',@recipients)); #$subject= join( , @subject); #$body = join( , @body); foreach $recipient (@recipients) { print $sender, $recipient, $subject, $body\n; $mail = new NTsendmail; $mail-send($sender, $recipient, $subject, $body); } } == -Original Message- From: Bob Showalter [mailto:[EMAIL PROTECTED] Sent: 20 April 2005 02:31 PM To: Olivier, Wim W; Perl Beginners (E-mail) Subject: RE: GetOpt::Long Olivier, Wim W wrote: Hi all, Is it possible to use GetOpt::Long (or something similar) in a subroutine using @_ instead of in the standard way (using @ARGV)? This should work: sub foo { local @ARGV = @_; GetOptions(...blah...); ... } __ Standard Bank Disclaimer and Confidentiality Note This e-mail, its attachments and any rights attaching hereto are, unless the context clearly indicates otherwise, the property of Standard Bank Group Limited and/or its subsidiaries (the Group). It is confidential, private and intended for the addressee only. Should you not be the addressee and receive this e-mail by mistake, kindly notify the sender, and delete this e-mail, immediately and do not disclose or use same in any manner whatsoever. Views and opinions expressed in this e-mail are those of the sender unless clearly stated as those of the Group. The Group accepts no liability whatsoever for any loss or damages whatsoever and howsoever incurred, or suffered, resulting, or arising, from the use of this email or its attachments. The Group does not warrant the integrity of this e-mail nor that it is free of errors, viruses, interception or interference. Licensed divisions of the Standard Bank Group are authorised financial services providers in terms of the Financial Advisory and Intermediary Services Act, No 37 of 2002 (FAIS). For information about the Standard Bank Group Limited visit our website http://www.standardbank.co.za ___ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: GetOpt::Long
Olivier, Wim W wrote: Hi Bob, Hi. Don't top-post please. If I comment out this if statement, I don't get the error anymore. An clues as to the correct syntax??? I run the subroutine as follows: if ($conf{'pnl_check_for_analytics_email'} =~ 'ON') { notify_email -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -s Subject line -b Message body }; You need to pass your arguments as a proper list, with commas and quotes: notify_email('-r', '[EMAIL PROTECTED]', '-r', '[EMAIL PROTECTED]', '-s', 'Subject Line', '-b', 'Message Body'); You can omit the parens if notify_email has been declared above this code, but I dont' recommend it. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: GetOpt::Long
Hi Bob, You gave me the following answer earlier: sub foo { local @ARGV = @_; GetOptions(...blah...); ... } If I run it as a seperate script, commenting out the (local @ARGV = @_;) line, then it works. But it does not work when called as a subroutine from within the main script. I have also tried (my @ARGV = @_;). Any idea why? Here is my code now: = # Notify recipients via SMTP (email) # Usage: notify_email -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -s Subject line -b Message body sub notify_email { local @ARGV = @_; # Get the sub's params into the master param array for GetOpt::Long $ENV{NTsendmail} = $conf{'smtp'}; $sender = $conf{'smtpsender'}; GetOptions (r=s = [EMAIL PROTECTED], # -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] -r [EMAIL PROTECTED] s=s = [EMAIL PROTECTED], # -s This is the subject line... b=s = [EMAIL PROTECTED]);# -b This is the message body... $subject= join( , @subject); $body = join( , @body); foreach $recipient (@recipients) { print $sender, $recipient, $subject, $body\n; $mail = new NTsendmail; $mail-send($sender, $recipient, $subject, $body); } } = __ Standard Bank Disclaimer and Confidentiality Note This e-mail, its attachments and any rights attaching hereto are, unless the context clearly indicates otherwise, the property of Standard Bank Group Limited and/or its subsidiaries (the Group). It is confidential, private and intended for the addressee only. Should you not be the addressee and receive this e-mail by mistake, kindly notify the sender, and delete this e-mail, immediately and do not disclose or use same in any manner whatsoever. Views and opinions expressed in this e-mail are those of the sender unless clearly stated as those of the Group. The Group accepts no liability whatsoever for any loss or damages whatsoever and howsoever incurred, or suffered, resulting, or arising, from the use of this email or its attachments. The Group does not warrant the integrity of this e-mail nor that it is free of errors, viruses, interception or interference. Licensed divisions of the Standard Bank Group are authorised financial services providers in terms of the Financial Advisory and Intermediary Services Act, No 37 of 2002 (FAIS). For information about the Standard Bank Group Limited visit our website http://www.standardbank.co.za ___ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: GetOpt::Long
How about something like this? It doesn't make it like GetOpt::Long, but it does handle what you want. Or you could just require that people pass an array to your subroutine and save yourself a little work. If there is only one recipient, then it's a one-element array. # my $recipients = '[EMAIL PROTECTED]'; notify_email('[EMAIL PROTECTED]',$recipients,test,this is my body); #my @recipients = qw([EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED]); #notify_email('[EMAIL PROTECTED]',[EMAIL PROTECTED],test,this is the body); sub notify_email{ my ($sender,$rcpt,$subj,$body) = @_; my $recipient; if(ref($rcpt)){ if(ref($rcpt) eq 'ARRAY'){ $recipient = join(',',@{$rcpt}); }elsif(ref($rcpt) eq 'SCALAR'){ $recipient = ${$rcpt}; }else{ die Invalid recipient parameter!\n; } }else{ $recipient = $rcpt; } print $recipient.\n; } # -Original Message- From: Olivier, Wim W [mailto:[EMAIL PROTECTED] Sent: Wednesday, April 20, 2005 12:53 AM To: Perl Beginners (E-mail) Subject: GetOpt::Long Hi all, Is it possible to use GetOpt::Long (or something similar) in a subroutine using @_ instead of in the standard way (using @ARGV)? I want to have the following scenario, but use GetOpt in a subrouting within the main script and pass options (and values) to the subroutine. The values ($recipient, $subject, $body) must be configurable and they must be able to take multiple values, i.e. multiple recipients for a message. # Notify recipients via SMTP (email) sub notify_email { $ENV{NTsendmail} = $smtp; $sender = [EMAIL PROTECTED]; $recipient = [EMAIL PROTECTED]; $subject= ALERT; $body = join , @_; $mail = new NTsendmail; $mail-send($sender, $recipient, $subject, $body); } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: GetOpt::Long
Olivier, Wim W wrote: Hi all, Is it possible to use GetOpt::Long (or something similar) in a subroutine using @_ instead of in the standard way (using @ARGV)? This should work: sub foo { local @ARGV = @_; GetOptions(...blah...); ... } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: GetOpt::Long
On 4/20/05, Olivier, Wim W wrote: Hi all, Is it possible to use GetOpt::Long (or something similar) in a subroutine Getargs::Long - http://search.cpan.org/dist/Getargs-Long/ HTH, -- Offer Kaye -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Getopt::Long , handles the number zero differently..
- Original Message - From: Mike Donnelly [EMAIL PROTECTED] Date: Tuesday, December 21, 2004 10:58 am Subject: Getopt::Long , handles the number zero differently.. Hello Using the example code below, I find that I can Please paste working code use getopt handily to pass all sorts of variables to my script, as long as a value passed is not a 0 (zero) are you sure ?? How to I use getopt::long and be able to pass the number zero as a value? exactly as you coded below. The problem is not in your getopt code, but in your print statment. The OP if $foo, will return false on integer value of 0. Code, and behavior follows Thanks! Mike D . MYTEST #!/usr/local/bin/perl use Getopt::Long; GetOptions(o=\$oflag, string=s=\$stringmandatory, int=i= \$mandatoryinteger, print oflag $oflag\n if $oflag; print stringmandatory $stringmandatory\n if $stringmandatory; print mandatoryinteger $mandatoryinteger\n if $mandatoryinteger; # try this print mandatoryinteger $mandatoryinteger\n if $mandatoryinteger = 0; print Unprocessed by Getopt::Long\n if $ARGV[0]; foreach (@ARGV) { print $_\n; } # ./MYTEST --string=abc stringmandatory abc # ./MYTEST --string=1 stringmandatory 1 # ./MYTEST --string=0 # ./MYTEST --int=0 # ./MYTEST --int=1 mandatoryinteger 1 HTH, Mark G __ Do you Yahoo!? The all-new My Yahoo! - Get yours free! http://my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Getopt::Long , handles the number zero differently..
--- [EMAIL PROTECTED] wrote: - Original Message - From: Mike Donnelly [EMAIL PROTECTED] Date: Tuesday, December 21, 2004 10:58 am Subject: Getopt::Long , handles the number zero differently.. Hello Using the example code below, I find that I can Please paste working code Sorry Bout' that .. chopped a line use getopt handily to pass all sorts of variables to my script, as long as a value passed is not a 0 (zero) are you sure ?? Well, I WAS sure.. How to I use getopt::long and be able to pass the number zero as a value? exactly as you coded below. The problem is not in your getopt code, but in your print statment. The OP if $foo, will return false on integer value of 0. You are correct and I thank you for the help. Code, and behavior follows Thanks! Mike D . MYTEST #!/usr/local/bin/perl use Getopt::Long; GetOptions(o=\$oflag, string=s=\$stringmandatory, int=i= \$mandatoryinteger, print oflag $oflag\n if $oflag; print stringmandatory $stringmandatory\n if $stringmandatory; print mandatoryinteger $mandatoryinteger\n if $mandatoryinteger; # try this print mandatoryinteger $mandatoryinteger\n if $mandatoryinteger = 0; print Unprocessed by Getopt::Long\n if $ARGV[0]; foreach (@ARGV) { print $_\n; } # ./MYTEST --string=abc stringmandatory abc # ./MYTEST --string=1 stringmandatory 1 # ./MYTEST --string=0 # ./MYTEST --int=0 # ./MYTEST --int=1 mandatoryinteger 1 HTH, Mark G __ Do you Yahoo!? The all-new My Yahoo! - Get yours free! http://my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response __ Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses. http://promotions.yahoo.com/new_mail -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Getopt::Long , handles the number zero differently..
Mike Donnelly wrote: Using the example code below, I find that I can use getopt handily to pass all sorts of variables to my script, as long as a value passed is not a 0 (zero) How to I use getopt::long and be able to pass the number zero as a value? Code, and behavior follows . MYTEST #!/usr/local/bin/perl use Getopt::Long; GetOptions(o=\$oflag, string=s=\$stringmandatory, int=i= \$mandatoryinteger, print oflag $oflag\n if $oflag; print stringmandatory $stringmandatory\n if $stringmandatory; print mandatoryinteger $mandatoryinteger\n if $mandatoryinteger; Change the previous three lines to: print oflag $oflag\n if defined $oflag; print stringmandatory $stringmandatory\n if defined $stringmandatory; print mandatoryinteger $mandatoryinteger\n if defined $mandatoryinteger; print Unprocessed by Getopt::Long\n if $ARGV[0]; Change that one to: print Unprocessed by Getopt::Long\n if @ARGV; ($ARGV[0] could also be 0!) foreach (@ARGV) { print $_\n; } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Getopt::Long::Configure('auto_help')
How do I take advantage of this? I turn it on but I do not know how to get it to display a help message. If I pass in -h --help it just ends. I am assuming I need some kind of pod file but I am unknowledgeable on how to write a pod file. Also how do I tell it what pod file to open? Form perldoc Getopt::Long Configuring Getopt::Long Getopt::Long can be configured by calling subroutine Getopt::Long::Configure(). This subroutine takes a list of quoted strings, each specifying a configuration option to be enabled, e.g. ignore_case, or disabled, e.g. no_ignore_case. Case does not matter. Multiple calls to Configure() are possible. auto_help (default:disabled) Automatically provide support for the --help and -? options if the application did not specify a handler for this option itself. Getopt::Long will provide a help message using module Pod::Usage. The message, derived from the SYNOPSIS POD section, will be written to standard output and processing will terminate. auto_help will be enabled if the calling program explicitly specified a version number higher than 2.32 in the use or require statement. The key is in the docs you posted, specifically, will provide a help message using module Pod::Usage, I would suggest you have a look at the docs for that module it should clear things up nicely. Particularly have a look at the section Recommended Use that should help even more. I have had good luck combining the two, though was unaware that this could be handled automagically for me, very cool, I usually just code it in to catch --help and -h and call pod2usage explicitly, god Perl+CPAN rocks... I posted a template that takes advantage of this strategy late last year if you want to check the archives, or e-mail me off list and I will fire you a copy http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Getopt::Long option prefix
Hi David, From what I've seen, Getopt::Long default behavior is to accept either '--opt|-opt'. (Tested on Solaris 2.6 w/ Perl 5.005_03) test.pl #!/usr/local/bin/perl use strict; use diagnostics; use Getopt::Long; my %pagerdest = (); Getopt::Long::Configure('default'); GetOptions( pagerdest=s% = \%pagerdest ); foreach my $keyname (sort keys %pagerdest) { print pagerdest $keyname = $pagerdest{$keyname}\n; } # ./test.pl -pagerdest a=lkfd pagerdest a = lkfd - Original Message - From: david To: [EMAIL PROTECTED] Sent: Monday, August 26, 2002 7:03 PM Subject: Re: Having problems with AppConfig::Getopt module Karl Kaufman wrote: For example: # test.pl -pagerdest [EMAIL PROTECTED] is the above another typo? should it be: test.pl --pagerdest [EMAIL PROTECTED] you seem to have only one '-' in front of pagerdest? Getopt::Long uses '--' not '-' david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: getOpt::long?
From: Michael [EMAIL PROTECTED] I am a true perl newbie. If you continue like this you will stay one. I am supposed to: Write a program that finds lines in an input file and copies them to an output file . The program takes the following arguments: an input file name (a mandatory argument) an output filename (an optional argument, set to STDOUT if omitted), a starting position number that marks the beginning of the offset (an optional argument, set to the beginning of the input file if omitted) an ending position number that marks the end of the offset (an optional argument, set to the end of the input file if omitted) If any mandatory argument is omitted or the command is followed by a help switch, your program prints out a usage on the screen (Hint: use assert.pl to verify the conditions). ... But this is really cool. This guy's even too lazy to at least reword the request, he copies the whole homework assignment over here without changing a single word. I'm surprised he did not leave the name email of his profesor at the end. Listen Michael, we don't do homeworks here. We are here to help people learn Perl, to help people finish their projects, but we are not here to do their work for them. How do you expect to learn anything if someone else does everything for you? Jenda === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain. I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: getOpt::long?
- Original Message - From: Jenda Krynicky [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, March 13, 2002 7:10 AM Subject: Re: getOpt::long? From: Michael [EMAIL PROTECTED] I am a true perl newbie. If you continue like this you will stay one. :-) Thanks for all the feedback. I had intended to send the assignment as an attachment and ask about: 1) Configuring my win2k box so that I could use the command line to run a perl program without having to type 'perl' before the name of the program in order to execute it, and 2) How to access perl documentation. C:\cd perl C:\Perlperldoc -f getopt 'perldoc' is not recognized as an internal or external command, operable program or batch file. C:\Perlperl -v 'perl' is not recognized as an internal or external command, operable program or batch file. I have installed ActiveState Perl 5.6.1 build 631 on this machine, some time ago, and have previously written perl scripts on it ( small ones!). I also have this problem on my laptop. So I installed Mandrake Linux 8.2 last night, and am even able to find the info on the getopt module. Now I just have to buy a supported printer so I can print it out! Sorry to have bothered. -michael- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: getOpt::long?
From: michael [EMAIL PROTECTED] :-) Thanks for all the feedback. I had intended to send the assignment as an attachment and ask about: 1) Configuring my win2k box so that I could use the command line to run a perl program without having to type 'perl' before the name of the program in order to execute it, and Add .pl and whatever other extensions you want to the PATHEXT system variable (Rightclick My Computer and select Properties\Advanced\Environment Variables) You might need to set up the file extension mapping as well. 2) How to access perl documentation. C:\cd perl C:\Perlperldoc -f getopt 'perldoc' is not recognized as an internal or external command, operable program or batch file. C:\Perlperl -v 'perl' is not recognized as an internal or external command, operable program or batch file. The perldoc.bat and perl.exe are in c:\perl\bin. You should have this path in your PATH system variable. Jenda === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain. I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: getOpt::long?
-Original Message- From: michael [mailto:[EMAIL PROTECTED]] Subject: Re: getOpt::long? I am a true perl newbie. If you continue like this you will stay one. :-) Thanks for all the feedback. I had intended to send the assignment as an attachment and ask about: 1) Configuring my win2k box so that I could use the command line to run a perl program without having to type 'perl' before the name of the program in order to execute it, and All the information you should need about the AS install of Perl should be on your start menu as: Start | Programs | ActiveState ActivePerl 5.6 | Documentation. To answer your specific question, you can open the html file: file:///C:/Perl/html/faq/Windows/ActivePerl-Winfaq4.html and select How do I associate Perl scripts with perl? I'd detail the answer for you, but it's already been done, so it'll be easier for both of us if you read the documentation. 2) How to access perl documentation. a) Start | Programs | ActiveState ActivePerl 5.6 | Documentation b) You can also go to C:\Perl\bin, and run perldoc from there (it is a batch file that emulates perldoc on Unix) c) http://www.perldoc.com d) Install cygwin C:\cd perl C:\Perlperldoc -f getopt 'perldoc' is not recognized as an internal or external command, operable program or batch file. Need to be in C:\Perl\bin directory C:\Perlperl -v 'perl' is not recognized as an internal or external command, operable program or batch file. Need to be in C:\Perl\bin directory I have installed ActiveState Perl 5.6.1 build 631 on this machine, some time ago, and have previously written perl scripts on it ( small ones!). I also have this problem on my laptop. The aforementioned answers should resolve the issue on your laptop as well. So I installed Mandrake Linux 8.2 last night, and am even able to find the info on the getopt module. Now I just have to buy a supported printer so I can print it out! Sorry to have bothered. Understand that we are here to help. The feedback you got was based on the information that was sent. It was not meant to be degrading, but you have to look at it from our perspective. Now that you've re-posted, I hope this answers your questions, though running perldoc -f getopt didn't return anything for me, so you might have to check some of the other sources. Hope this helps... Jason CONFIDENTIALITY NOTICE: The information contained in this ELECTRONIC MAIL transmission is confidential. It may also be privileged work product or proprietary information. This information is intended for the exclusive use of the addressee(s). If you are not the intended recipient, you are hereby notified that any use, disclosure, dissemination, distribution [other than to the addressee(s)], copying or taking of any action because of this information is strictly prohibited.
RE: getOpt::long?
try $ perldoc Getopt No documentation found for Getopt. However, try perldoc Getopt::Long perldoc Getopt::Std perldoc Getopt::Long.html perldoc Getopt::Std.html -Original Message- From: Jason Larson [mailto:[EMAIL PROTECTED]] Sent: Wednesday, March 13, 2002 11:28 AM To: 'michael' Cc: [EMAIL PROTECTED] Subject: RE: getOpt::long? answers your questions, though running perldoc -f getopt didn't return anything for me, so you might have to check some of the other sources. Hope this helps... Jason CONFIDENTIALITY NOTICE: The information contained in this ELECTRONIC MAIL transmission is confidential. It may also be privileged work product or proprietary information. This information is intended for the exclusive use of the addressee(s). If you are not the intended recipient, you are hereby notified that any use, disclosure, dissemination, distribution [other than to the addressee(s)], copying or taking of any action because of this information is strictly prohibited. The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: getOpt::long?
On Tue, 2002-03-12 at 21:50, Michael wrote: I am a true perl newbie. I am supposed to: snip reason=homework/test / -- -it does make a difference- -michael- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Okay, so what is the question? -- Today is Boomtime the 72nd day of Chaos in the YOLD 3168 Hail Eris! Missile Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getopt::Long problem (Allegedly)
On Wed, 30 Jan 2002 21:47:15 -, Angus Laycock wrote: --=_NextPart_000_01D4_01C1A9D7.AEC826A0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Hi, I wrote this today on UNIX . When I tested the script, calling it with all the options (f s D P U S e q ) the -s option would not return the value from the command line. All the other variables got populated. I then change the s:s to f:s and that worked. I am trying to replace some software which uses -s as an option and need to handle the options the same in the PERL replacement. Is there a bug with s:s? By default, Getopt::Long is case insensitive. Try adding the bundling and ignore_case options. cf perldoc Getopt::Long (Configuring Getopt::Long) #!/usr/bin/perl -w use strict; use Getopt::Long qw(GetOptions); Getopt::Long::Configure (bundling, ignore_case); my($DATABASE, $PASSWORD, $USER, $SERVER, $ERR, $SPROC, $QUERY, $TT) =('')x8; GetOptions( 'f:s' = \$TT, 's:s' = \$SPROC, 'D:s' = \$DATABASE, 'P:s' = \$PASSWORD, 'U:s' = \$USER, 'S:s' = \$SERVER, 'e' = \$ERR, 'q:s' = \$QUERY ) || die Options incorrect\n; print _EOT_; DATABASE = $DATABASE PASSWORD = $PASSWORD USER = $USER SERVER = $SERVER ERR = $ERR SPROC= $SPROC QUERY= $QUERY TT = $TT _EOT_ __END__ -- briac dynamic .sig on strike, we apologize for the inconvenience -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: GetOpt::Long - Usage question
On Wed, Aug 15, 2001 at 02:49:34PM -0700, pn wrote: #/usr/bin/perl -w use strict; # Forward declarations my $opt_help; This is your problem right here. Getopt::Long sets the package global $main::opt_help, but now that you've declared the variable lexical, anytime you access it unqualifed in your program you'll get the lexical variable. The solution is to either declare it global, with our or use vars, or pass in a variable to GetOptions. E.g. our $opt_help; GetOptions('help|h'); OR use vars qw($opt_help); GetOptions('help|h'); OR GetOptions('help|h' = \$opt_help); OR (my preference) GetOptions(\%opts, 'help|h'); # now available as $opts{'help'} Some further commentary is below. sub GetCmdLine; # Begin of Main Body GetCmdLine(); # End of Main Body ## Sub GetCmdLine ## sub GetCmdLine { use Getopt::Long; It's generally better to place use lines near the beginning of the file; that's, conceptually, when they get executed, regardless of their position in the code, and it provides an easy way for someone to determine the dependencies your code has. $Getopt::Long::ignorecase; $Getopt::Long::autoabbrev; I don't know what you intend to accomplish with these. You're simply mentioning the variables, you aren't assigning any values to them. GetOptions(help|h); # Print the value of $opt_help print The value of \$opt_help is : , $opt_help, \n; if (defined($opt_help) ($opt_help ==1)) { It's easier to test simply for truth: if ($opt_help) { print \n\nUsage: $0 [options] \n; print [optins]:\n; print \t\t-h Prints this help message header.\n; } } Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Getopt::Long
* Bicknell, Frank ([EMAIL PROTECTED]) [21 Jul 2001 08:12]: [...] Using Getopt::Long, it's possible to watch for negated options: -nostuff ... and it's possible to alias options stuff|such allows -stuff to be the same as -such. [...] $ stuff -nosuch Using Perl Version 5.00404 Unknown option: nosuch Use of uninitialized value at stuff line 6. Stuff: $ Anyone seen this before? (a) You really should be using --nosuch rather than -nosuch. Getopt::Long is just being smart and noting that there aren't any short opts. (b) It works fine with perl 5.6 and Getopt::Long 2.25 (do the following to determine what version you have:) perl -MGetopt::Long -l -e'print $Getopt::Long::VERSION' (c) You are doing the right thing. Doing some research yields: http://search.cpan.org/doc/JV/Getopt-Long-2.25/CHANGES # Changes in version 2.22 # --- # # * Fixes a bug in the combination of aliases and negation. # # Old: foo|bar! allowed negation on foo, but not on bar. # New: foo|bar! allows negation on foo and bar. # # Caveat: foo|f!, with bundling, issues the warning that negation on # a short option is ignored. To obtain the desired behaviour, use # # foo! = \$opt_foo, f = \$opt_foo # or # foo|f = \$opt_foo, nofoo = sub { $opt_foo = 0 } # # Remember that this is _only_ required when bundling is in effect. So it was a bug, and is now fixed. You may want to upgrade your Perl (the newer Getopt::Long is standard in 5.6.1) or just your Getopt::Long (using CPAN; 2.24 seems to only need Perl 5.4 or later). So, all you need is version 2.22 or later (current 2.25). cheers, -- iain. http://eh.org/~koschei/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]