Not walking together with Getopt Module

Not walking together with Getopt Module

am 23.08.2011 23:53:08 von Emeka

--000e0cd32fc6a6302604ab3338b0
Content-Type: text/plain; charset=ISO-8859-1

Hello All,

Why shouldn't this work?

use Getopt::Long;

#$debug = 0;



$result = GetOptions("age=i" => $age);

if ($age) {print "Input age is $age years"; }

Emeka


--
*Satajanus Nig. Ltd


*

--000e0cd32fc6a6302604ab3338b0--

Re: Not walking together with Getopt Module

am 24.08.2011 00:22:47 von Brandon McCaig

On Tue, Aug 23, 2011 at 5:53 PM, Emeka wrote:
> Hello All,
>
> Why shouldn't this work?
*snip*
> $result = GetOptions("age=i" => $age);

You should always try to explain the results you expect and what you
are getting instead. I had a hard time understanding what your problem
was just from reading your message. :)

It looks like your problem is passing the value of $age instead of a
reference to $age. In order for the Getopt::Long module to modify your
$age variable it needs a reference to it:

use strict;
use warnings;

use Getopt::Long;

my $age;

my %opts = ('age=i' => \$age);

GetOptions(%opts) or exit 1;

if(defined $age)
{
print "You specified '$age' years";
}

__END__

You can see perlref and perlreftut to learn about references. You
should also be using 'strict' and 'warnings' (to possibly save Shlomi
a breath ;).


--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/