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

use Getopt::Long; my $name="divakar"; my $age="24"; my $option=GetOptions("name"=>\$name,"age"=>\$age); print "the options are $option\n"; if ($option) { print "the option given is divakar\n"; } if ($option) { print "the option given is 24"; }

i want to try the getopt module. when i click the run button in eclipse. it returns following output.

the options are 1 the option given is divakar the option given is 24

How do i run my script in eclipse ide by giving options. e.g., getopt.pl --age

i dont want to get the output as above after clicking the run button

Thank you.

Replies are listed 'Best First'.
Re: How to execute perl script with options using Eclipse IDE?
by Eliya (Vicar) on Nov 20, 2011 at 12:27 UTC

    Irrespective of any potential issues with Eclipse, your code most likely won't do what you want even when run from the normal command line.

    GetOptions returns true if the arguments could successfully be parsed, so that's what you're testing in your subsequent if ($option) { ... }.

    Maybe you meant to test if ($age) { ... } instead?  But then you shouldn't initialize the variable with 24, because that's a true value, too... (likewise for $name, of course)

      use Getopt::Long; my $name; my $age; my $option=GetOptions("name"=>\$name,"age"=>\$age); print "the options are $option\n"; if ("$name") { print "the option given is divakar\n"; } if ("$age") { print "the option given is 24"; }
      I just tried as you said. but getting following error.
      the options are 1 Use of uninitialized value $name in string at C:/Diva Projects/Eclipse +/Scripts_Testing/getopt.pl line 8. Use of uninitialized value $age in string at C:/Diva Projects/Eclipse/ +Scripts_Testing/getopt.pl line 13.

        Just leave off the double quotes around "$name" and "$age" — they're useless here anyway.

        Interpolating an uninitialized variable into a string produces a warning (when warnings are enabled), while simply testing an unitialized variable for truth (i.e. if ($name) ... ) does not.