0

The requirement is

perl prg.pl --product --param Product1 --from FilePath

where --product specifies the reference to the function that has to accept the parameter Product1, and the FilePath is the path of the file to be searched for the product.

GetOptions('product=s'=>\&getproduct, 'param=s'=>\$param,'from=s'=>\$from);

In spite of this, while running the Perl program, it gives an error that the values product and from are uninitialized. Can you help me solve this issue?

toolic
  • 57,801
  • 17
  • 75
  • 117
Dhanashri P
  • 111
  • 5
  • 1
    `getproduct` is to be passed as an reference. So it should be `'product=s' => \&getproduct` and also `param` and `from`. – vkk05 Jun 21 '20 at 11:19
  • @vkk05, The `\ ` was there, but hidden by bad formatting. – ikegami Jun 21 '20 at 13:00
  • 1
    In the future, please provide the error messages, and please provide a minimal, runnable demonstration of the problem. – ikegami Jun 21 '20 at 13:03

1 Answers1

2

You indicate that the product option requires a value (=s), so

             Value for --product. (Not an option.)
             |
             |       Doesn't start with `-`, so option parsing ends.
             |       |
          vvvvvvv vvvvvvvv
--product --param Product1 --from FilePath
                  ^^^^^^^^^^^^^^^^^^^^^^^^
                             |
                             Not options, so found in @ARGV.

Fix:

GetOptions(
   'help|h|?' => \&help,
   'product'  => \$product,
   'param=s'  => \$param,
   'from=s'   => \$from,
)
   or usage();

@ARGV == 0
   or usage("Too many arguments.");

See this for a sample implementation of usage and help.

ikegami
  • 367,544
  • 15
  • 269
  • 518