CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Working with null values in Perl

 



askan
Novice

Oct 14, 2009, 9:29 AM

Post #1 of 7 (612 views)
Working with null values in Perl Can't Post

Hey guys,

I am trying to build some command line options into my word count program. I've been working with the $#ARGV and $ARGV variables to tease out command options, but have a few questions.

Presently, when the argument '-help' is passed, the program displays my help documentation. How do I get the program to display the help documentation if there are no command line arguments?

The code presently looks like...


Code
 
sub help_doc_sr {
print "blah blah blah...\n";
exit(0);
}

while ($count <= $#ARGV) {
SWITCH: {
$ARGV[$count] =~ m/(^-h$|^-help$)/ && do
{help_doc_sr; last SWITCH;};
}
$count++;
}


Another question is, since I am using things like -flag to find user supplied values, I want to create a graceful error message saying that a value isn't provided if the flag has been invoked. For instance...

$perl sourse_code.pl -option value

Should use 'value' against -option.

If the user accidentally types...

$perl source_code.pl -option

I want the program to say something like "You forgot to give me a value when you invoked '-option'!"

Currently, I am using something like $ARGV[$count+1] to see what comes after the flag. But in the simulated mistake above, $ARGV[$count+1] does not exist, so perl gives me errors...


toolic
User

Oct 14, 2009, 10:00 AM

Post #2 of 7 (608 views)
Re: [askan] Working with null values in Perl [In reply to] Can't Post

If you don't want to re-invent this wheel, just use the Core module:
http://perldoc.perl.org/Getopt/Long.html

along with:
http://perldoc.perl.org/Pod/Usage.html


askan
Novice

Oct 14, 2009, 10:09 AM

Post #3 of 7 (606 views)
Re: [toolic] Working with null values in Perl [In reply to] Can't Post

Well, I am trying to avoid using modules (personal challenge). I'm sure there is another way, as this was always quite easy with csh.


toolic
User

Oct 14, 2009, 10:16 AM

Post #4 of 7 (604 views)
Re: [askan] Working with null values in Perl [In reply to] Can't Post

You don't have to use Getopt::Long, but you can always look at it's source code to see how to do what you are trying to do:


Code
perldoc -m Getopt::Long



askan
Novice

Oct 15, 2009, 12:03 PM

Post #5 of 7 (594 views)
Re: [toolic] Working with null values in Perl [In reply to] Can't Post

Alright, so I have acquiesced and have started using Getopt::Long.

I'm still trying to get the basics down...

Essentially, I have a command line option called 'punc' that can take the strings 'keep' or 'ignore' as arguments (default is 'ignore').

I am using GetOptions to look for -punc, and would like to call on a subroutine that will execute one block of code if -punc=keep, another when -punc=ignore and a warning when -punc is neither. I can get the program to enter the punc subroutine, but it does not work with the command line argument, just the default. I've read the perldoc and using GetOptions you can assign a variable ( => \$punc ) or call on a subroutine ( => \&punc_sub ) but seemingly not carry the value of $punc to the subroutine (where I thought I'd use some if statements).

My code...


Code
#!/usr/bin/perl 

use strict;
use warnings;

use Getopt::Long;

my $punc='ignore';

sub punc_sr {
if ($punc eq 'keep') {
print "$punc\n";
}
if ($punc eq 'ignore') {
print "$punc\n";
}
else {
print "\'$punc\' is not an appropriate value for -punc.\n";
print "See -help, check your arguments and try again!\n";
exit(1);
}
}

GetOptions(
'punc=s' => \&punc_sr,
);



toolic
User

Oct 15, 2009, 12:31 PM

Post #6 of 7 (590 views)
Re: [askan] Working with null values in Perl [In reply to] Can't Post

Interesting. I was not even aware of Getopt's capability to execute a subroutine like that. So, you already know more about this that I do!

This is what I usually do:


Code
use strict; 
use warnings;
use Getopt::Long;
use Pod::Usage;

my ($help, $punc);
GetOptions(
'punc=s' => \$punc,
'help' => \$help
) or pod2usage();

$help and pod2usage(-verbose => 2);

$punc = 'ignore' unless defined $punc;
unless (($punc eq 'ignore') or ($punc eq 'keep')) {
pod2usage("Error: illegal punc: $punc")
}

print "$punc\n";


=head1 NAME

B<myprog> - whatever

=head1 SYNOPSIS

myprog [options]

Options:
-help verbose help
-punc string [ignore|keep]

=head1 DESCRIPTION

foo bar...

=head1 OPTIONS

All options can be abbreviated.

=over 4

=item punc

blah blah blah...

=item help

Show verbose usage information.

=back

=cut


Here is what happens when you run it with an illegal punc value:


Code
> myprog -punc foo 
Error: illegal punc: foo
Usage:
myprog [options]

Options:
-help verbose help
-punc string [ignore|keep]



askan
Novice

Oct 16, 2009, 6:33 AM

Post #7 of 7 (577 views)
Re: [toolic] Working with null values in Perl [In reply to] Can't Post

Well, what I think I will do is simply save in the options' arguments to variables and just call on the subroutines after the GetOptions block, passing those parameters to the subroutines. It just perplexes me that I cannot do it all in the same line inside the GetOptions block.

Also, as far as displaying the help message if there are no command line arguments, I'll just use a simple if statement to make sure $#ARGV > 0 early on in the program.

Thanks for the Getopt::Long find, I am slowly warming up the idea of using modules. I still haven't embraced Pod::Usage;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives