in reply to Getopt::Long subroutine question

hi brassbin,
*as if the sub was called 3 times, how do i make it so that all 3 values of the option are passed to @_ once with one invocation of the subroutine?

Checking the User-defined subroutines to handle options in Getopt::Long documentation, it says

Ultimate control over what should be done when (actually: each time) an option is encountered on the command line can be achieved by designating a reference to a subroutine (or an anonymous subroutine) as the option destination. When GetOptions() encounters the option, it will call the subroutine with two or three arguments. The first argument is the name of the option. (Actually, it is an object that stringifies to the name of the option.) For a scalar or array destination, the second argument is the value to be stored...

And if I get that right something like this:
use warnings; use strict; use Getopt::Long; GetOptions('test=s{3}'=>\&sub1); sub sub1{ print $_[1]; }
NOTE: If the is less than what is specified, you get a message of Insufficient arguments for option ....

UPDATE: * Re^3: Getopt::Long subroutine question

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^2: Getopt::Long subroutine question
by Loops (Curate) on Jul 23, 2013 at 04:20 UTC
    Hi 2teez,

    From my reading of that documentation the my original answer still is correct. Your code calls sub1 3 times (once for each parameter passed on the command line). Yet the OP asked how to have the sub called just once with all parameters in tow.

      Yes, you are right, however, what I intend to answer, is the OP words ..as if the sub was called 3 times..
      That was why I quoted the documentation, that though the sub is called each time an option is encountered, however it could be shown as though those options were gotten once!! Accessing only the the value to be stored, in this case a,b and c NOT like the OP has it test a, test b and test c.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me